有没有办法在git中签出除1或2之外的所有文件?

aso*_*erg 4 git

有时我发现自己处于我想要在工作树中转储所有内容的位置,除了1或2个文件.是否有捷径可寻?因为它是我手动输入git checkout ....对于我想要从索引中检出的所有文件,并且不包括我想要保留的文件,但这非常费力.

我想到这样做的另一种方法是存储我要保留的1或2个文件,然后"结帐".然后恢复藏匿.这是一个很好的方法吗?

xai*_*zek 9

我想偶尔做同样的事情,并且通常使用以下命令集(假设索引中没有任何内容):

git add file-to-preserve-1 file-to-preserve-2
git stash save --keep-index "Changes discarded because ..."
# git stash drop # think twice before running this command
git reset
Run Code Online (Sandbox Code Playgroud)

用语言:

  1. 将您需要保留的文件放入索引.
  2. 存储所有内容(可能包含有意义的消息),但保持索引不变.
  3. 删除存储的更改(当您确定它们没用时).
  4. 重置索引.

我能想到的另一种方式(实际上没有尝试过,第一种方式适合我):

git update-index --skip-worktree file-to-preserve-1 file-to-preserve-2
git checkout .
git update-index --no-skip-worktree file-to-preserve-1 file-to-preserve-2
Run Code Online (Sandbox Code Playgroud)

解释:

  1. 命令git忽略一些文件.
  2. 签出当前目录中的所有内容.
  3. 告诉git 1再次照常处理文件.