git 在不复制粘贴完整文件路径的情况下检出修改后的文件

git checkout modified file without copy-pasting full file path

git status 显示类似于

git status
# On branch icc-server-send-metric-values
# 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:   java/com/me/cards/preview/preview-card.js

我想签出文件,但不想重新输入或复制粘贴。最接近的命令是 add -i 但这只真正关心索引,而不是检查。这里有什么好的工作流程?

使用几乎每个 shell 都提供的制表符补全。有很多文章可以帮助您进行设置,包括 this one for Bash.

至于纯 Git 解决方案:

如果这是唯一的修改,请使用 git checkout . 放弃所有更改。

如果您想保留一些更改并放弃其他更改,请使用 git checkout -p 以交互方式放弃工作目录中的更改。

当 Git 提示您对要放弃的每个文件进行更改时,请使用 a:

a - discard this hunk and all later hunks in the file

当 Git 提示您要保留每个文件的更改时,请使用 d:

d - do not discard this hunk or any of the later hunks in the file

Git 有一个 "plugin",非常适合这个用例。

它叫做 git number,使您能够通过数字引用更改的文件。


"plugin" 添加了一个 git number 命令,该命令 - 没有任何其他参数 - 等同于 git status 但带有前导数字。

$ git number
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

1       a/b/c/d/e/f/g/test2

nothing added to commit but untracked files present (use "git add" to track)

如果你更喜欢git status -s(就像我一样)git number你有没有覆盖。

$ git number -s
1 ?? a/b/c/d/e/f/g/test2

之后,您可以通过在每个命令前加上 number 来通过编号引用文件。 git add 可能如下所示:

$ git number add 1
git add a/b/c/d/e/f/g/test2

git number 回显执行的命令,因此您可以确保使用了正确的数字。 git add 只是一个示例,您可以使用任何命令并在其前面加上 number

此外它还支持范围;如果你想添加文件 5-10 你可以只使用 git number add 5-10 并且 git number 会理解你。


如有任何其他问题,我建议您直接查看 github repository