git:如何覆盖合并时的所有本地更改?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2025085/
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: How do I overwrite all local changes on merge?
提问by kubi
Here's the situation. I'm working on the master
branch. I create file1 and commit. I create file2 and commit. Whoops. I may want to use file2, someday, but it's definitely not something that should be put in the master branch. So that I don't lose file2 I use
这是情况。我在master
分行工作。我创建 file1 并提交。我创建 file2 并提交。哎呀。有一天我可能想使用 file2,但这绝对不是应该放在 master 分支中的东西。这样我就不会丢失我使用的 file2
git checkout head~1
git branch new-branch
git checkout new-branch
so that I can continue developing. I add file3 to new-branch
. If you've been paying attention, I've got two branches, master
that contains "file1" and "file2" and new-branch
that contains "file1" and "file3".
以便我可以继续发展。我将 file3 添加到new-branch
. 如果您一直在注意,我有两个分支,master
包含“file1”和“file2”以及new-branch
包含“file1”和“file3”。
Now is the time to get the changes I've made back into the master branch. What's the best way to do this? I definitely want the head of the master
branch to point at the files as they appear in new-branch
, but I also don't want to lose the work I've done in file2 by doing a reset, in case I want to use it.
现在是时候将我所做的更改重新放到 master 分支中了。做到这一点的最佳方法是什么?我绝对希望master
分支的负责人在文件出现时指向它们new-branch
,但我也不想通过重置而丢失我在 file2 中所做的工作,以防万一我想使用它。
Keep in mind this is a simplification. Instead of just three files, I've got a dozen files with tens of lines of code being changed all over the place all with multiple commits. I certainly hope the solution isn't to do a file-by-file merge/checkout, because that would be a huge pain.
请记住,这是一种简化。不是只有三个文件,我有十几个文件,其中包含数十行代码,并且在所有地方都进行了多次提交的更改。我当然希望解决方案不是逐个文件合并/签出,因为那将是一个巨大的痛苦。
Any ideas?
有任何想法吗?
采纳答案by Randal Schwartz
I'm working on the master branch. I create file1 and commit.
我正在主分支上工作。我创建 file1 并提交。
date >file1
git add file1
git commit -m 'added file1'
I create file2 and commit.
我创建 file2 并提交。
date >file2
git add file2
git commit -m 'added file2'
Whoops. I may want to use file2, someday, but it's definitely not something that should be put in the master branch.
哎呀。有一天我可能想使用 file2,但这绝对不是应该放在 master 分支中的东西。
Oops. Very simple. Make a new branch from where you are:
哎呀。很简单。从您所在的位置创建一个新分支:
git checkout -b savingfile2
This will make the file2 change the commit for savingfile2
. Now go back and unwind one step on master
这将使 file2 更改savingfile2
. 现在回去在master上放松一步
git checkout master
git reset --hard HEAD~1
At this point, the commits leading up to master will reflect the addition of file1, and the additional commit between master and savingfile2 will be the addition of file2 to that.
此时,通向 master 的提交将反映 file1 的添加,而 master 和 Savingfile2 之间的附加提交将是 file2 的添加。
If you make more changes to master, and then want to bring file2 back eventually, you'll want to rebase that side-branch onto the new master:
如果您对 master 进行更多更改,然后希望最终将 file2 带回来,您将需要将该侧分支重新定位到新的 master 上:
date >file3
git add file3
git commit -m 'adding file3'
date >file4
git add file4
git commit -m 'adding file4'
And now we finally want file2:
现在我们终于想要 file2:
git checkout savingfile2
git rebase master # might need to fix conflicts here
git checkout master
git merge savingfile2 # will be a fast-forward
git branch -d savingfile2 # no need any more
That should do it.
那应该这样做。
回答by Tomi Ky?stil?
What you should do is what you should have done when you noticed your mistake of commiting file2: undo the commit (instead of creating a new branch):
你应该做的是当你注意到你提交 file2 的错误时你应该做的:撤消提交(而不是创建一个新分支):
git checkout master
git reset HEAD^
This leaves file2 untracked and unharmed and possible modifications uncommited. Then you should (have) stash(ed) the uncommited modifications in case you want to use them later:
这使得 file2 未被跟踪和未受损害,并且未提交可能的修改。然后你应该(有)隐藏(ed)未提交的修改,以防你以后想使用它们:
git stash save "modifications that should not be in the master branch"
Stashing gets rid of any local changes, which allows master
to be made point to new-branch
:
存储摆脱了任何本地更改,这允许master
指向new-branch
:
git merge new-branch
The goal here was to eliminate the divergence between the two branches, i.e. make master
an ancestor of new-branch
. This way no actual merging would have to occur, and the last command would just fast-forwardthe master
branch (provided there are no local changes).
这里的目标是消除两个分支之间的分歧,即创建master
的祖先new-branch
。这样,没有实际的合并将有发生,而且最后一个命令将刚刚快进的master
分支(提供有当地没有变化)。
回答by VonC
Since you didn't follow the optimal workflowdescribed by Tomi Ky?stil?, but also since you didn' publish (push) anything yet, why not switch the two branches?
(provided everything is committed)
由于您没有遵循Tomi Ky?stil描述的最佳工作流程?,而且既然你还没有发布(推送)任何东西,为什么不切换两个分支呢?
(假设一切都已承诺)
master
and new-branch
are just some pointers to some SHA1:
master
并且new-branch
只是指向某些 SHA1 的一些指针:
$ git checkout master #start from master
$ git branch tmp # tmp points on master HEAD
$ git checkout new-branch # switch to new-branch
$ git branch -f master new_branch # master points at new-branch HEAD
$ git checkout tmp # switch back to *former* master branch
$ git branch -f new_branch tmp # new-branch points at former master HEAD
$ git checkout master # go to new master
$ git branch -D tmp # remove tmp pointer
... and you're done.
(disclaimer: not tested yet, so try it with caution ;) )
......你就完成了。
(免责声明:尚未测试,因此请谨慎尝试;))
See:
看:
- a similar approach in the paragraph "Fixing mistakes without
git-reset
" of "Branching and merging with git", - and also in this thread
- “与 git 分支和合并
git-reset
”的“无需修复错误”段落中的类似方法, - 并且也在这个线程中