git git恢复在合并期间删除的单个文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2096509/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 04:02:46  来源:igfitidea点击:

git recover single file that was deleted during a merge

gitrepositorybranch

提问by tybro0103

I'm currently in branch 'foo'. I just ran git merge master. Only problem is there was a certain file in foo that I wanted to keep. Is there a way to get it back but keep all the other changes from merge master?

我目前在分支“foo”。我刚跑git merge master。唯一的问题是我想保留 foo 中的某个文件。有没有办法恢复它但保留来自合并主的所有其他更改?

回答by Bartek

Try something like this:

尝试这样的事情:

git checkout HEAD -- filename

This will roll your fileback one commit. If you want to go back further to a specific commit, you can use a commit hash or append ^N to the end of the HEAD keyword, e.g. HEAD^2.

这将使您的文件回滚一次提交。如果您想进一步返回到特定提交,您可以使用提交哈希或将 ^N 附加到 HEAD 关键字的末尾,例如HEAD^2.

回答by Gattster

I'm not sure how to correct the problem from the current situation, but you may want to look at git merge -s ours. The docs are here.

我不确定如何从当前情况纠正问题,但您可能想查看git merge -s ours. 文档在这里

A workflow would be

工作流程将是

  1. Create branch afrom master
  2. Do a custom change in branch athat you will not want to merge back into master
  3. Check out masterand git merge -s ours a
  4. Check out aand continue working and committing.
  1. a从创建分支master
  2. a您不想合并回的分支中进行自定义更改master
  3. 退房mastergit merge -s ours a
  4. 检查a并继续工作和提交。

Now when you merge with master, the custom changes in step 2 will be ignored.

现在,当您与 master 合并时,步骤 2 中的自定义更改将被忽略。