简体   繁体   English

在提交到当前头部之前合并Git状态

[英]Merge Git state before a commit into current head

How can I merge a state before wrong commits into the current head in Git? 如何在错误提交到Git中的当前头部之前合并状态?

Mistakenly merge by a other person resetted my change on the server, after I fetched and merged the remote master branch, my changes are locally gone. 别人错误地合并了服务器,将我的更改重置了,在我获取并合并了远程主分支后,我的更改在本地消失了。 In the meantime are more correct commits are pushed to the remote server and stay on the current head of the master branch. 同时,更正确的提交被推送到远程服务器,并停留在master分支的当前头上。

I need to merge the state before the wrong merge occured into my current master head. 我需要先合并状态,然后再将错误的合并发生到我当前的主脑袋中。

How can I achive that? 我该如何实现?

I have tried the answer by @Drecker. 我已经尝试过@Drecker的答案

git revert -m 2 <SHA_hash_of_merge_commit>

and I have now my correct file content back, but I loose the correct file content from the second person. 现在我已经恢复了正确的文件内容,但是我从第二个人那里丢失了正确的文件内容。 I committed it, but don't pushed it. 我承诺了,但不推。 I created a new branch restore from that. 我从中创建了一个新的分支restore Now the second person content is correct in the remote master branch and my content is correct in the local restore branch. 现在,第二个人的内容在远程master分支中正确,而我的内容在本地restore分支中正确。

How can I re-merge both branches again, so that I have the correct contents from both branches? 如何重新合并两个分支,以便两个分支都具有正确的内容?

UPDATE: you may read this post: Undo a Git merge that hasn't been pushed yet 更新:您可能读过这篇文章: 撤消尚未推送的Git合并

To "undo" a merge that has been committed (and pushed) you may use 要“撤消”已提交(并推送)的合并,您可以使用

git revert -m <parent_number> <SHA_hash_of_merge_commit>

the parent_number is indexed number of parents of that merge - it is required because git wouldn't know which branch you want to reject (indexed from 1). parent_number是该合并的父级的索引数目-这是必需的,因为git不会知道您要拒绝哪个分支(从1开始索引)。 This way a new commit will be created deleting all changes that was created by rejected branch 这样,将创建一个新的提交,删除被拒绝分支创建的所有更改。

Other (unrecomanded) option is to use 使用其他(不推荐)选项

git rebase -i <SHA_of_commit_to_which_you_want_to_change_histrory>

this way you will interactively choose which commit you want in current HEAD and which not 这样,您将交互式地选择要在当前HEAD中选择的提交,而不是

WARNING: this way you actually change git history! 警告:这样您实际上可以更改git历史记录! You may lose some changes (that wasn't at any other branch) and cause more problems when your coworkers will pull changes 您可能会丢失一些更改(其他任何分支都没有),并且当您的同事提出更改时会引起更多问题

You need to recover the state before the first merge. 您需要在第一次合并之前恢复状态。

You can acchive it by using git reflog + git reset, this way (I am assuming the branch is master): 您可以通过以下方式使用git reflog + git reset来接收它(我假设分支是master):

git reflog master

Will return the history of your local master ref, is an output similar to this: 将返回本地主引用的历史记录,类似于以下输出:

1904ece master@{0}: commit: fixed styles
ba185a3 master@{1}: commit: fade transition for angular pages|
b06d0a5 master@{2}: commit: fixed detail view
792af52 master@{3}: commit (merge): Merge branch 'origin/master' into master
128dd58 master@{4}: commit: fixed save of searchs
...

Note the merge commit, noted with "commit ( merge )", maybe of those is the merge commit you are trying to "undo", to proceed you can use git reset command, this way: 注意合并提交,用“ commit( merge )”表示,也许其中的一个是您试图“撤消”的合并提交,要继续,可以使用git reset命令,这种方式:

git stash # save all local changes on index and WC just in case
git reset --hard master@{4}

Note I am using master@{4} here, because merge commit was found as master@{3} 注意我在这里使用master @ {4},因为发现合并提交为master @ {3}

After doing this, you will need to pull and do the merge again, you can do this as many times as you need to re-do merge operations 完成此操作后,您将需要再次拉并执行合并,您可以根据需要重新执行合并操作多次

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM