修改⽂件之git
修改⽂件
Git ⽐其他版本控制系统设计得优秀,因为 Git 跟踪并管理的是修改,⽽⾮⽂件。
什么是修改?⽐如你新增了⼀⾏,这就是⼀个修改,删除了⼀⾏,也是⼀个修改,更改了某些字符,也是⼀个修改,删了⼀些⼜加了⼀些,也是⼀个修改,甚⾄创建⼀个新⽂件,也算⼀个修改。
让我们将 ReadMe ⽂件进⾏⼀次修改:
hyb@139-159-150-152:~/gitcode$ cat ReadMe
hello bit
hello git
hello world
此时,仓库中的 ReadMe 和我们⼯作区的 ReadMe 是不同的,如何查看当前仓库的状态呢? git status 命令⽤于查看在你上次提交之后是否有对⽂件进⾏再次修改。
1 hyb@139-159-150-152:~/gitcode$ git status
2 On branch master
3 Changes not staged for commit:
4 (use "git add <file>..." to update what will be committed)
5 (use "git restore <file>..." to discard changes in working directory)
6 modified: ReadMe
7
8 no changes added to commit (use "git add" and/or "git commit -a")
上⾯的结果告诉我们,ReadMe 被修改过了,但还没有完成添加与提交。
⽬前,我们只知道⽂件被修改了,如果能知道具体哪些地⽅被修改了,就更好了。有同学会说,我刚改的我知道呀!可是,你还记得你三天前写了什么代码吗?或者没写?
1 hyb@139-159-150-152:~/gitcode$ git diff ReadMe
2 diff --git a/ReadMe b/ReadMe
3 index 9c9e1f0..4a97140 100644
4 --- a/ReadMe
5 +++ b/ReadMe
6 @@ -1,2 +1,3 @@
7 hello bit
8 -hello bit
9 +hello git
10 +hello world
git diff [file] 命令⽤来显⽰暂存区和⼯作区⽂件的差异,显⽰的格式正是Unix通⽤的diff格式。也可以使⽤ git diff HEAD – [file] 命令来查看版本库和⼯作区⽂件的区别。
知道了对 ReadMe 做了什么修改后,再把它提交到本地仓库就放⼼多了。
1 hyb@139-159-150-152:~/gitcode$ git add ReadMe
2 hyb@139-159-150-152:~/gitcode$ git status
3 On branch master
4 Changes to be committed:
5 (use "git restore --staged <file>..." to unstage)
6 modified: ReadMe
git add 之后,就没有看到上⾯ no changes added to commit (use “git add” and/or “git commit -a”) 的消息了。接下来让我们继续 git commit 即可:
hyb@139-159-150-152:~/gitcode$ git commit -m "add modify ReadMe file"
[master 94da695] add modify ReadMe file1 file changed, 2 insertions(+), 1 deletion(-)
hyb@139-159-150-152:~/gitcode$ git status
On branch master
nothing to commit, working tree clean