存储对特定文件的更改

ewo*_*wok 45 git git-stash

我有一个很大的git项目,我愚蠢地导入到eclipse并运行autoformat.现在,项目中的每个文件都显示为已修改.而不是提交我的格式化文件,我宁愿还原我只格式化的所有文件,而不是其他更改.例如:

$ git status
# On branch master
# 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)
#   (commit or discard the untracked or modified content in submodules)

#     modified: dir/file1.cpp
#     modified: dir/file1.h
#     modified: dir/file2.cpp
#     modified: dir/file2.h
#     modified: dir/file3.cpp
#     modified: dir/file3.h
#     modified: dir/file4.cpp
#     modified: dir/file4.h
Run Code Online (Sandbox Code Playgroud)

我知道file2.cpp,file2.hfile3.cpp已与内容修改(即,不只是格式化).我想隐藏对这三个文件的更改,然后签出一个旧版本,以便我可以在以后重新应用这些文件的更改.我宁愿避免这样的事情:

$ cp file2.cpp ~/tmp
$ git checkout blahblahblah
$ cp ~/tmp/file2.cpp .
Run Code Online (Sandbox Code Playgroud)

如果有一种明显的方法可以做到这一点,不涉及藏匿,请告诉我.无论做什么工作.

Von*_*onC 47

我知道file2.cpp,file2.hfile3.cpp已与内容修改(即,不只是格式化).
我想隐藏对这三个文件的更改,然后签出一个旧版本,以便我可以在以后重新应用这些文件的更改.

使用Git 2.13(2017年第二季度),git stash将正式用于存储特定文件的更改

git stash push [--] [<pathspec>...]
Run Code Online (Sandbox Code Playgroud)

请参阅提交9e14090,提交1ada502,提交df6bba0(2017年2月28日),并提交9ca6326,提交6f5ccd4,提交f5727e2(2017年2月19日)作者:Thomas Gummerer(tgummerer).
(由Junio C gitsterHamano合并- -提交44c3f09,2017年3月10日)

现在记录:

要快速创建快照,可以省略"推送".
在此模式下,不允许使用非选项参数来防止拼写错误的子命令产生不需要的存储.
对此的两个例外是stash -p作为别名stash push -p和pathspecs的行为,在双连字符之后允许--消除歧义.

当pathspec被赋予' git stash push'时,新的存储仅记录与pathspec匹配的文件的修改状态.
然后,索引条目和工作树文件也会回滚到HEAD仅用于这些文件的状态 ,从而保留与pathspec不匹配的文件.

请注意,正如指出的medmunds的评论,即git stash会使用路径相对于git仓库的根文件夹.

  • 请注意,`git stash`的pathspecs是[相对于你的git*项目根目录*](http://stackoverflow.com/a/43969202/647002) - *not*当前的工作目录(就像`git中列出的路径一样) status`). (2认同)

red*_*nce 41

您可以add使用要保留的更改的文件,然后stash是其余文件并清除存储:

git add file2.cpp file2.h file3.cpp
git stash --keep-index
Run Code Online (Sandbox Code Playgroud)

此时,您已经隐藏了不必要的更改.如果你想永久摆脱它们,请运行:

git stash drop
Run Code Online (Sandbox Code Playgroud)

现在你已经file2.cpp,file2.h并且已经file3.cpp上演了提交.如果您想要存储这些文件(而不是提交它们):

git reset
git stash
Run Code Online (Sandbox Code Playgroud)

现在你将处于你之前的提交状态,只有这三个文件被隐藏起来.

更新:

正如VonC在他的回答中解释的那样git stash push,Git 2.13及更高版本包含了一种更直接的方式来存储特定文件.

  • 为什么你需要运行`stash clear`? (9认同)

Die*_*o V 14

一个不错的选择是使用交互式存储模式.

git stash --patch
Run Code Online (Sandbox Code Playgroud)

它的工作方式大多类似于交互式添加模式:您将看到一系列差异,显示您在工作树中所做的更改,并且您必须选择要隐藏的文件(或仅文件的某些部分!) ,其余的将保持不变.

来自man git-stash:

使用--patch,您可以交互式地从HEAD和工作树之间的差异中选择要存储的数据.构建存储条目,使其索引状态与存储库的索引状态相同,并且其工作树仅包含您以交互方式选择的更改.然后,从您的工作树中回滚所选更改.请参阅git-add(1)"交互模式"部分以了解如何操作--patch模式.

在您的情况下,您将能够看到仅格式化的数据并将其单独存储,而不会丢失您有意义的更改.


Kas*_*han 6

您也可以使用git stash -p。这样,您可以选择应将哪些块添加到存储中,也可以选择整个文件。

对于每个大块,系统将提示您一些操作:

y - stash this hunk
n - do not stash this hunk
q - quit; do not stash this hunk or any of the remaining ones
a - stash this hunk and all later hunks in the file
d - do not stash this hunk or any of the later hunks in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
k - leave this hunk undecided, see previous undecided hunk
K - leave this hunk undecided, see previous hunk
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print help
Run Code Online (Sandbox Code Playgroud)