在Git 2.0版本以前,本地删除文件时,想让git服务器也删除这个文件,须要使用下面的命令来添加改动:git
git rm
命令来删除文件,不只会删除本地文件,还会自动添加改动。git add .
命令使用 git rm
命令能够从本地删除文件,同时自动添加被删除文件到git的staged区域,后续直接执行 git commit 便可,不须要先执行 git add 命令:shell
$ 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 commit
和 git push
命令后,远端服务器上的同名文件也会被删除。其余人从服务器pull代码,不会再看到这个文件。bash
当使用 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区域,执行时会打印以下警告信息:ui
$ 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 commit
和 git push
命令提交到远端服务器,那么远端服务器上的同名文件不会被删除,其余人从服务器pull代码仍是会看到那个文件。this
即,在Git 1.9.1版本上,用 rm 命令删除本地文件后,要添加这个改动到git的staged区域,而后commit、push,远端服务器才会同步删除这个文件,git add .
命令不会把已删除文件添加到git的staged区域。spa
参考上面执行 git add .
命令时打印的警告信息,可使用 git add --all
选项来添加已删除文件的改动,--all
也能够写为 -A
,这二者是等效的。在Git 1.9.1版本上,查看 man git-add 对 -A 选项的说明以下:code
-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.orm
若是以为要输入大写的A比较麻烦,也可使用 -u
选项,该选项一样会添加已删除文件的改动:rem
$ 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 -A
和 git 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 选项的说明跟个人记录一致,其连接是: git-scm.com/docs/git-ad… 我以前用的应该就是Git 1.7.1版本罢。