上一节在阐述本地配置的时候简单举了一个文件的例子,本讲将详细描述在git里面文件如何处理,包括增加文件,修改文件,删除文件,文件重命名。
一、文件基本操作
1.新增文件
新增文件需要调用git add 文件名
将文件添加到暂存区并且git
会跟踪次文件。然后调用git commit -m '提交描述'
将暂存区的文件提交到版本库中
zhuchaodeMacBook-Pro-2:learning-git zhuchao$ echo hello git > 2.txt
zhuchaodeMacBook-Pro-2:learning-git zhuchao$ git add 2.txt
zhuchaodeMacBook-Pro-2:learning-git zhuchao$ git commit -m 'add 2.txt'
[master 9433e42] add 2.txt
1 file changed, 1 insertion(+)
create mode 100644 2.txt
当需要添加的文件非常多的时候可以使用git add -A
一次性将所有文件变更添加到暂存区。
2.修改文件
我们可以使用git status
来查看当前git
管理的项目中哪些文件有变动
zhuchaodeMacBook-Pro-2:learning-git zhuchao$ echo hello git2 > 2.txt
zhuchaodeMacBook-Pro-2:learning-git zhuchao$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: 2.txt
no changes added to commit (use "git add" and/or "git commit -a")
zhuchaodeMacBook-Pro-2:learning-git zhuchao$ git commit -am '编辑2.txt文件'
[master c12a3fb] 编辑2.txt文件
1 file changed, 1 insertion(+), 1 deletion(-)
当你确认本次修改需要提交到历史版本库那么可以直接使用git commit -am '提交message'
这个命令会直接将工作区修改的文件直接提交到历史版本库中,但是如果这个文件没有被git
跟踪那么不会成功,也就是新增的文件还是需要git add
命令跟踪文件
3.删除文件
删除文件的时候我们不能使用git add
命令将修改加到暂存区,而是要使用git rm
命令标记文件已被删除,然后执行git commit
那么历史版本库就会将这个文件从历史中删除
zhuchaodeMacBook-Pro-2:learning-git zhuchao$ rm 1.txt
zhuchaodeMacBook-Pro-2:learning-git zhuchao$ git rm 1.txt
rm '1.txt'
zhuchaodeMacBook-Pro-2:learning-git zhuchao$ git commit -m 'delete 1.txt'
[master a932c8d] delete 1.txt
1 file changed, 1 deletion(-)
delete mode 100644 1.txt
4.文件重命名
程序员有一半的时间在起名字
文件名对程序员来说还是很重要的,所以有时我们需要修改文件名,git
采用了Linux
重命名的方式使用git mv
来为文件重命名
zhuchaodeMacBook-Pro-2:learning-git zhuchao$ mv 2.txt 3.txt
zhuchaodeMacBook-Pro-2:learning-git zhuchao$ git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
deleted: 2.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
3.txt
no changes added to commit (use "git add" and/or "git commit -a")
zhuchaodeMacBook-Pro-2:learning-git zhuchao$ git add -A
zhuchaodeMacBook-Pro-2:learning-git zhuchao$ git commit -m ’rename file‘
error: pathspec 'file‘' did not match any file(s) known to git
zhuchaodeMacBook-Pro-2:learning-git zhuchao$ git commit -m 'rename file'
[master 68573ed] rename file
1 file changed, 0 insertions(+), 0 deletions(-)
rename 2.txt => 3.txt (100%)
zhuchaodeMacBook-Pro-2:learning-git zhuchao$ git mv 3.txt 2.txt
zhuchaodeMacBook-Pro-2:learning-git zhuchao$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
renamed: 3.txt -> 2.txt
zhuchaodeMacBook-Pro-2:learning-git zhuchao$ git commit -m 'rename file'
[master 1ee5b2c] rename file
1 file changed, 0 insertions(+), 0 deletions(-)
rename 3.txt => 2.txt (100%)
上面我用了两种方式进行重命名,第一种是常规方法直接用操作系统提供的mv
来重命名文件,然后使用git add -A
和git commit
直接提交。第二种方式是使用git mv
方式重命名,然后直接使用git commit
提交