git-将更改存储到特定文件
我有一个大的git项目,我愚蠢地导入了eclipse并运行了自动格式化。 现在,项目中的每个文件都显示为已修改。 与其提交我的格式化文件,不如还原所有我仅格式化但没有其他更改的文件。 例如:
$ git status
# On branch master
# Changes not staged for commit:
# (use "git add ..." to update what will be committed)
# (use "git checkout -- ..." 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
我知道file2.cpp、file2.h和file3.cpp已使用内容(即,不仅是格式化的)进行了修改。 我想存储对这三个文件的更改,然后签出旧的修订版,以便以后可以将更改重新应用于这些文件。 我宁愿避免这样的事情:
$ cp file2.cpp ~/tmp
$ git checkout blahblahblah
$ cp ~/tmp/file2.cpp .
如果有一种显而易见的方法不涉及隐藏,请告诉我。 一切都能完成工作。
ewok asked 2020-08-01T08:17:59Z
5个解决方案
45 votes
我知道git stash、stash push -p和--已使用内容(即,不仅是格式化的)进行了修改。
我想存储对这三个文件的更改,然后签出旧的修订版,以便以后可以将更改重新应用于这些文件。
使用Git 2.13(2017年第二季度),git stash将正式有一种方式来存储对特定文件的更改
git stash push [--] [...]
请参阅Thomas Gummerer(git stash)的提交9e14090,提交1ada502,提交df6bba0(2017年2月28日)和提交9ca6326,提交6f5ccd4,提交f5727e2(2017年2月19日)。
(由Junio C Hamano合并-git stash-在commit 44c3f09中,2017年3月10日)
如现在所述:
为了快速制作快照,您可以省略“推送”。
在这种模式下,不允许使用非选项参数来防止拼写错误 产生不必要的隐藏的子命令。
这两个例外是git stash,它是stash push -p和pathspec的别名,在使用双连字符--消除歧义后可以使用它们。
将pathspec赋予'git stash'时,新存储记录了 修改状态仅适用于与pathspec匹配的文件。
然后将索引条目和工作树文件回滚到其中的状态 git stash也仅适用于这些文件,而留下与 pathspec完好无损。
请注意,正如medmunds在评论中指出的那样,git stash将使用相对于git repo根文件夹的路径。
VonC answered 2020-08-01T08:19:51Z
38 votes
您可以在git stash push中保留要更改的文件,然后在file2.h中保留其余文件并清除存储:
git add file2.cpp file2.h file3.cpp
git stash --keep-index
至此,您已经隐藏了不必要的更改。 如果您想永久摆脱它们,请运行:
git stash drop
现在,您有git stash push、file2.h和file3.cpp进行提交。 如果然后您想隐藏这些文件(而不是提交它们):
git reset
git stash
现在,您将处于之前的提交状态,仅保存了这三个文件。
更新:
正如VonC在他的回答中所解释的那样,Git 2.13及更高版本包括一种使用git stash push存储特定文件的更直接的方法。
redhotvengeance answered 2020-08-01T08:18:42Z
14 votes
一个不错的选择是使用交互式隐藏模式。
git stash --patch
它的工作原理与交互式添加模式非常相似:您将看到一系列差异,这些差异显示了您在工作树中所做的更改,并且您必须选择要隐藏的文件(或仅文件的某些部分!)。 ,其余的将保持不变。
来自man git-stash:
使用--patch,您可以从HEAD和要隐藏的工作树之间的差异中交互选择块。 存储项的构造应使其索引状态与存储库的索引状态相同,并且其工作树仅包含交互式选择的更改。 然后,所选更改将从工作树中回滚。 请参阅git-add(1)的“交互模式”部分,以了解如何操作--patch模式。
在您的情况下,您将能够看到仅格式化的块并将其单独存储,而不会丢失有意义的更改。
Diego V answered 2020-08-01T08:20:30Z
6 votes
您也可以使用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
Kashan answered 2020-08-01T08:20:54Z
3 votes
这对于IMO git diff和--reject是很好的用法:
git diff file2.cpp file2.h file3.cpp > ../my-changes.patch
git checkout ...
git apply ../my-changes.patch
--reject之后,您可以检查补丁文件以确保所有更改都在那里。
请注意,您可能需要使用--reject选项来应用,以防补丁无法完全应用。 另请参见手册页以进行申请。
robinst answered 2020-08-01T08:21:24Z