有时,某些文件会出现在工作树上。他们无人追踪,
其中一些文件我们希望丢弃并且不暂存它们,而另一些文件我们可能想要暂存它们。
我的问题是:是否有某种交互命令可以用来逐步选择我们想要保留的文件以及我们想要忽略的文件?
希望我的问题很清楚。
您可以交互地使用git add -i暂存文件。
$ git status
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# a
# b
# c
# d
nothing added to commit but untracked files present (use "git add" to track)
$ git add -i
staged unstaged path
*** Commands ***
1: status 2: update 3: revert 4: add untracked
5: patch 6: diff 7: quit 8: help
What now> a
1: a
2: b
3: c
4: d
Add untracked>> 2
1: a
* 2: b
3: c
4: d
Add untracked>> 4
1: a
* 2: b
3: c
* 4: d
Add untracked>>
added 2 paths
*** Commands ***
1: status 2: update 3: revert 4: add untracked
5: patch 6: diff 7: quit 8: help
What now> s
staged unstaged path
1: +0/-0 nothing b
2: +0/-0 nothing d
*** Commands ***
1: status 2: update 3: revert 4: add untracked
5: patch 6: diff 7: quit 8: help
What now> q
Bye.
$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: b
# new file: d
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# a
# c
Run Code Online (Sandbox Code Playgroud)
虽然有点偏离主题,但我只是想添加以下信息:
git add -i比上面的用例强大得多。例如,如果您只想添加文件中的部分更改,则可以使用该patch模式。
我已经使用了补丁模式,很多次当我意识到我想将单个文件中的更改分解为两个单独的提交时,但我不想手动创建文件的副本,编辑原始文件、暂存、提交并为下一次提交再次执行相同操作。
在补丁模式下,git 为您提供了将更改分解为更小的补丁并仅暂存这些块的选项。如果 git 没有自动给你最好的块,你可以在编辑器提示中手动分解块。