目录

一、前言

二、基本  

三、git add -u

四、git add -A 

五、git add -i 

5.1、revert子命令

5.2、update子命令

5.3、add untracked子命令

5.4、diff子命令

5.5、status子命令

5.6、quit子命令

 六、帮助 

区别:

git add -u:将文件的修改、文件的删除,添加到暂存区;

git add .:将文件的修改,文件的新建,添加到暂存区。

git add -A:将文件的修改,文件的删除,文件的新建,添加到暂存区。 

git add * :将当前目录下所有文件添加到暂存区,包括文件的修改,文件的新建。

总结:

参考:


一、前言

git add命令主要用于把我们要提交的文件的信息添加到索引库中。当我们使用git commit时,git将依据索引库中的内容来进行文件的提交。 

二、基本  

git add <path>表示 add to index only files created or modified and not those deleted
我通常是通过git add <path>的形式把我们<path>添加到索引库中,<path>可以是文件也可以是目录。
git不仅能判断出<path>中,修改(不包括已删除)的文件,还能判断出新添的文件,并把它们的信息添加到索引库中。

三、git add -u

git add -u 表示 add to index only files modified or deleted and not those created
git add -u [<path>]: 把<path>中所有tracked文件中被修改过或已删除文件的信息添加到索引库。它不会处理untracted的文件。
省略<path>表示.,即当前目录。 

四、git add -A 

git add -A: [<path>]表示把<path>中所有tracked文件中被修改过或已删除文件和所有untracted的文件信息添加到索引库。
省略<path>表示.,即当前目录。

五、git add -i 

我们可以通过git add -i [<path>]命令查看<path>中被所有修改过或已删除文件但没有提交的文件,
并通过其revert子命令可以查看<path>中所有untracted的文件,同时进入一个子命令系统。
比如:

git add -i
           staged     unstaged path
  1:        +0/-0      nothing branch/t.txt
  2:        +0/-0      nothing branch/t2.txt
  3:    unchanged        +1/-0 readme.txt

*** Commands ***
  1: [s]tatus     2: [u]pdate     3: [r]evert     4: [a]dd untracked
  5: [p]atch      6: [d]iff       7: [q]uit       8: [h]elp
What now>

 这里的t.txt和t2.txt表示已经被执行了git add,待提交。即已经添加到索引库中。
readme.txt表示已经处于tracked下,它被修改了,但是还没有被执行了git add。即还没添加到索引库中。


5.1、revert子命令

可以通过git add -i的revert子命令(3: [r]evert)把已经添加到索引库中的文件从索引库中剔除。
(3: [r]evert)表示通过3或r或revert加回车执行该命令。执行该命令后,git会例出索引库中的文件列表.
然后通过数字来选择。输入"1"表示git会例出索引库中的文件列表中的第1个文件。
"1-15"表示git会例出索引库中的文件列表中的第1个文件到第15个文件.回车将执行。
如果我们不输入任何东西,直接回车,将结束revert子命令,返回git add -i的主命令行。

5.2、update子命令

可以通过update子命令(2: [u]pdate)把已经tracked的文件添加到索引库中。其操作和revert子命令类似。

5.3、add untracked子命令

通过add untracked子命令(4: [a]dd untracked)可以把还没被git管理的文件添加到索引库中。其操作和revert子命令类似。

5.4、diff子命令

可以通过diff子命令(6: [d]iff)可以比较索引库中文件和原版本的差异。其操作和revert子命令类似。

5.5、status子命令

status子命令(1: [s]tatus)功能上和git add -i相似 

5.6、quit子命令

quit子命令(7: [q]uit)用于退出git add -i命令系统

 六、帮助 

我们可以通过git add -h命令来看git add命令的帮助文档。

 git add -h
usage: git add [options] [--] <filepattern>...

    -n, --dry-run         dry run
    -v, --verbose         be verbose

    -i, --interactive     interactive picking
    -p, --patch           select hunks interactively
    -e, --edit            edit current diff and apply
    -f, --force           allow adding otherwise ignored files
    -u, --update          update tracked files
    -N, --intent-to-add   record only the fact that the path will be added later
    -A, --all             add changes from all tracked and untracked files
    --refresh             don't add, only refresh the index
    --ignore-errors       just skip files which cannot be added because of errors
    --ignore-missing      check if - even missing - files are ignored in dry run

区别:

git add -u:将文件的修改、文件的删除,添加到暂存区;

  •  仅监控已经被add的文件(即tracked file),他会将被修改的文件提交到暂存区。add -u 不会提交新文件(untracked file)。(git add --update的缩写)

git add .:将文件的修改,文件的新建,添加到暂存区。

  • 它会监控工作区的状态树,使用它会把工作时的所有变化提交到暂存区,包括文件内容修改(modified)以及新文件(new),但不包括被删除的文件

git add -A:将文件的修改,文件的删除,文件的新建,添加到暂存区。 

  • 是上面两个功能的合集(git add --all的缩写),提交所有包括被删除的文件信息,被替换的文件信息、被修改的文件以及新增的文件到暂存区其中删除。修改以及新增文件到缓存区和git add .和git add -u是相同的,同时具有他们的功能,还具有替换的文件的功能,下面我们就只看看替换文件

git add * :将当前目录下所有文件添加到暂存区,包括文件的修改,文件的新建。

  • 此命令类似于 "git add .",区别在于上面几个命令都会根据.gitignore做过滤,但是git add * 会忽略.gitignore把任何文件都加入(不包含删除掉的文件) 

 

-u就是update的意思,只会标记本地有改动(包括删除和修改)的已经追踪的文件

-u, –update

Update the index just where it already has an entry matching . This removes as well as modifies index entries to match the working tree, but adds no new files

If no is given when -u option is used, all tracked files in the entire working tree are updated (old versions of Git used to limit the update to the current directory and its subdirectories).

-A 就是all的意思,会标记本地所有有改动(包括删除和修改)和新增的文件 

-A, –all, –no-ignore-removal

Update the index not only where the working tree has a file matching but also where the index already has an entry. This adds, modifies, and removes index entries to match the working tree.

If no is given when -A option is used, all files in the entire working tree are updated (old versions of Git used to limit the update to the current directory and its subdirectories).

下面看一下git add -A 的文件替换: 

 这里写图片描述

 

总结:

git add -A 提交所有变化

git add -u 提交被修改(modified)和被删除(deleted)文件,不包括新文件(new)

git add . 提交新文件(new)和被修改(modified)文件,不包括被删除(deleted)文件 

git add * 提交新文件(new)和被修改(modified)文件,不包括被删除(deleted)文件忽略.gitignore

参考:

https://blog.csdn.net/csm201314/article/details/78385742 

https://blog.csdn.net/dayewandou/article/details/78513578 

https://blog.csdn.net/weixin_36564655/article/details/89682393 

https://www.cnblogs.com/RobertC/p/3578147.html

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐