本地删除文件后让git服务器也删除这个文件

在Git 2.0版本之前,本地删除文件时,想让git服务器也删除这个文件,需要使用下面的命令来添加改动:

  • 直接使用 git rm 命令来删除文件,不仅会删除本地文件,还会自动添加改动。
  • 当使用shell自身的rm命令删除文件时,可以执行下面的命令来添加改动:

    • git add -A
    • git add -u
    • 不要执行 git add . 命令

git rm

使用 git rm 命令可以从本地删除文件,同时自动添加被删除文件到git的staged区域,后续直接执行 git commit 即可,不需要先执行 git add 命令:

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

        deleted:    delete_by_git_rm

可以看到,执行 git rm delete_by_git_rm 命令后,用 ls 命令查看,没有再看到 delete_by_git_rm 文件,该文件已经从本地删除。而用 git status 命令查看,删除 delete_by_git_rm 文件的这个改动已经添加到git的staged区域,等待被commit。那么后续执行 git commitgit push 命令后,远端服务器上的同名文件也会被删除。其他人从服务器pull代码,不会再看到这个文件。

git add -A

当使用 shell 自身的 rm 命令删除本地文件时,这个改动不会自动添加到git的staged区域。使用 git status 命令查看,会提示"Changes not staged for commit":

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

        deleted:    delete_by_rm

此时,需要使用 git add 命令来添加改动。
一般常用 git add . 命令来添加本地改动到staged区域,但是针对用shell自身rm命令删除文件的情况来说,git add . 命令不会添加已删除文件到staged区域,执行时会打印如下警告信息:

$ git --version
git version 1.9.1
$ git add .
warning: You ran 'git add' with neither '-A (--all)' or '--ignore-removal',
whose behaviour will change in Git 2.0 with respect to paths you removed.
Paths like 'delete_by_rm' that are
removed from your working tree are ignored with this version of Git.

* 'git add --ignore-removal <pathspec>', which is the current default,
  ignores paths you removed from your working tree.

* 'git add --all <pathspec>' will let you also record the removals.

Run 'git status' to check the paths you removed from your workingtree.

可以看到,在Git 1.9.1版本上,执行 git add . 后,再用 git status 查看,删除的本地文件还是没有添加到git的staged区域。如果我们没有注意到这一点,后续执行 git commitgit push 命令提交到远端服务器,那么远端服务器上的同名文件不会被删除,其他人从服务器pull代码还是会看到那个文件。

即,在Git 1.9.1版本上,用 rm 命令删除本地文件后,要添加这个改动到git的staged区域,然后commit、push,远端服务器才会同步删除这个文件,git add .命令不会把已删除文件添加到git的staged区域。

参考上面执行 git add . 命令时打印的警告信息,可以使用 git add --all 选项来添加已删除文件的改动,--all 也可以写为 -A,这两者是等效的。在Git 1.9.1版本上,查看 man git-add 对 -A 选项的说明如下:

-A --all --no-ignore-removal
Update the index not only where the working tree has a file matching \<pathspec\> but also where the index already has an entry. This adds, modifies, and removes index entries to match the working tree.  
If no \<pathspec\> is given, the current version of Git defaults to "."; in other words, update all files in the current directory and its subdirectories. This default will change in a future version of Git, hence the form without \<pathspec\> should not be used.

git add -u

如果觉得要输入大写的A比较麻烦,也可以使用 -u 选项,该选项同样会添加已删除文件的改动:

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

        deleted:    delete_by_rm

查看Git 1.9.1版本 man git-add 对 -u 选项的说明如下:

-u --update
Update the index just where it already has an entry matching \<pathspec\>. This removes as well as modifies index entries to match the working tree, but adds no new files.  
If no \<pathspec\> is given, the current version of Git defaults to "."; in other words, update all tracked files in the current directory and its subdirectories. This default will change in a future version of Git, hence the form without \<pathspec\> should not be used.

git add -Agit add -u 都可以添加已删除文件的改动,它们的区别在于,-A 选项会添加新增的文件,而 -u 选项不会添加新增的文件。

注意:上面描述了Git 1.9.1版本上 git add . 命令不会添加已删除文件的改动。但是在当前最新的Git 2.23版本上,git add .命令可以添加已删除文件的改动。有一些Linux系统上可能还是使用老版本的git,为了兼容,对于用shell自身的rm命令删除文件的情况,建议都加上 -u 选项。

最后说一个突然发现自己记录的知识已经过时的小故事

我在几年前使用git的时候,记录 man git-add 里面对 -u 选项的说明如下:

-u, --update
Only match \<filepattern\> against already tracked files in the index rather than the working tree. That means that it will never stage new files, but that it will stage modified new contents of tracked files and that it will remove files from the index if the corresponding files in the working tree have been removed.  
If no \<filepattern\> is given, default to "."; in other words, update all tracked files in the current directory and its subdirectories.

这个说明跟上面Git 1.9.1版本 man git-add 里面的说明有所差异,跟当前最新的Git 2.23版本 man git-add 里面的说明更是差异巨大 (这里没有贴出Git 2.23版本的说明)。
同时 git add . 在不同版本上的行为还不一样,顿时有种日新月异、地覆天翻之感。
我不得不多次修改文章内容,添加Git版本号的说明,可以说是三易其稿。
我已经不记得之前使用的git软件版本是多少,感觉像是过时很久的老古董。
经过查找,Git 1.7.1版本对 git add -u 选项的说明跟我的记录一致,其链接是: https://git-scm.com/docs/git-...
我之前用的应该就是Git 1.7.1版本罢。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章