文件的暂存

如果我们更改了之前已经被跟踪的main.c,然后执行git status

$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   main.c

main.c会出现在Changes not staged for commit(已更改但未添加到暂存区)的区域中。这表示处于跟踪状态的文件在工作目录下已经被改变,但尚未添加到暂存区。要想添加到暂存区,需要执行git add命令。

git add是一个多功能命令,即可以用来跟踪新文件,也可以用来咱村文件,还可以做一些其他的事情。所以git add理解成“添加内容到下一次提交”比较妥帖。

我们执行git add把main.c添加到暂存区,然后在执行git status

$ git add main.c
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   main.c

现在,被修改后的main.c已暂存,并将进入到下一个提交中。

如果这个时候对main.c再做一下修改,然后再查看文件状态

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   main.c

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   main.c

此时,main.c同时出现在了已暂存和未暂存的列表中。那么如果执行git commit,提交的将是你上一次git add进暂存区的文件。所以,如果对文件做了修改,要及时git add

猜你喜欢

转载自www.cnblogs.com/velscode/p/10584809.html