git Git撤消仅对特定文件的自动合并,而不是整个分支
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5350278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git undo auto merging on a specific file only, not the whole branch
提问by Dee-M
Is there a way I can undo a auto-merge of one file that has been changed instead of the whole branch in Git?
有没有一种方法可以撤消对一个已更改文件而不是 Git 中的整个分支的自动合并?
I want to undo this merge:
我想撤消此合并:
Auto-merging public/stylesheets/application.css
自动合并 public/stylesheets/application.css
to the previous version and leave the rest at its current state.
到以前的版本,其余部分保持当前状态。
I tried:
我试过:
git reset HEAD~1 public/stylesheet/application.css
but seems like it's not working.
但似乎它不起作用。
回答by Mark Longair
When you do:
当你这样做时:
git reset HEAD~1 public/stylesheet/application.css
... that changes the version of your file in the index (i.e. the staged version) to the version in HEAD~1
- your working copy remains the same. The current documentationexplains this as:
...将索引中文件的版本(即暂存版本)更改为中的版本HEAD~1
- 您的工作副本保持不变。 当前文档将其解释为:
This form resets the index entries for all <paths> to their state at <commit%gt;. (It does not affect the working tree, nor the current branch.)
此表单将所有 <paths> 的索引条目重置为它们在 <commit%gt; 处的状态。(它不影响工作树,也不影响当前分支。)
If you do:
如果你这样做:
git checkout HEAD~1 -- public/stylesheet/application.css
... that will change both the working copy and the staged version to the version from HEAD~1
. The --
is there for safety, just in case you have file name that can also be understood as a commit.
...这会将工作副本和暂存版本都更改为HEAD~1
. 这--
是为了安全起见,以防万一您的文件名也可以理解为提交。
Now that you're got that change staged, you could amend the merge commit with:
现在您已经完成了更改,您可以使用以下内容修改合并提交:
git commit --amend
... which is fair enough as long has you haven't pushed the merge anywhere, since the merge will still just have changes from one branch or the other.
...只要您没有将合并推到任何地方就足够公平了,因为合并仍然只会从一个分支或另一个分支进行更改。
回答by Bombe
You probably want git checkout commit-ish-- file
.
你可能想要.git checkout commit-ish-- file
回答by averell
You cannot reset a single file, because reset only works on whole commits (it basically "forgets" newer commits). Also, you cannot "change" an existing commit - more on that below.
您不能重置单个文件,因为重置仅适用于整个提交(它基本上“忘记”较新的提交)。此外,您不能“更改”现有提交 - 更多内容请参见下文。
In your case, you have several options:
在您的情况下,您有多种选择:
The safest method would be to just checkout the old version of your file, and check-in again
最安全的方法是签出旧版本的文件,然后再次签入
$ git checkout HEAD~1 -- public/stylesheet/application.css
# Back at old version, change already staged
$ git commit -m "Reverted change to public/stylesheet/application.css"
This will simply create a new commit that turns back your accidental changes, and should be your preferred option unless you know exactly what you are doing.
这将简单地创建一个新的提交来回退您的意外更改,并且应该是您的首选选项,除非您确切地知道自己在做什么。
Second, reset the repository as above, and re-check in the commit:
其次,如上重置存储库,并重新检查提交:
$ git reset HEAD~1
# Now restore your file public/stylesheets/application.css
$ git commit -m "Re-checkin of the commit"
# You may use the old checkin message here
This will effectively "change" the existing commit in the way that you will have the same commit in your history, with the same description, minus the modifications to the one file. However, this changes you history, so you must notuse this if have already pushed the original commit to any upstream repository.
这将有效地“更改”现有提交,就像您将在历史记录中具有相同的提交,具有相同的描述,减去对一个文件的修改。但是,这会更改您的历史记录,因此如果已经将原始提交推送到任何上游存储库,则不得使用它。
Git does not allow you to change existing commits in any other way than by throwing away the existing ones and creating new ones. This will always "break" your history and should be only used if you exactly know what this feature does and why you want to do it.
除了丢弃现有提交并创建新提交之外,Git 不允许您以任何其他方式更改现有提交。这将始终“打破”您的历史记录,并且只有在您确切知道此功能的用途以及您想要这样做的原因时才应使用。