Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
462 views
in Technique[技术] by (71.8m points)

git - 撤消尚未推送的Git合并(Undo a Git merge that hasn't been pushed yet)

Within my master branch, I did a git merge some-other-branch locally, but never pushed the changes to origin master.

(在我的master分支中,我在本地进行了git merge some-other-branch ,但从未将更改推送到原始master。)

I didn't mean to merge, so I'd like to undo it.

(我不是要合并,所以我想撤消它。)

When doing a git status after my merge, I was getting this message:

(合并后执行git status ,收到以下消息:)

# On branch master
# Your branch is ahead of 'origin/master' by 4 commits.

Based upon some instructions I found , I tried running

(根据我发现的一些说明 ,我尝试运行)

git revert HEAD -m 1

but now I'm getting this message with git status :

(但是现在我收到了git status消息:)

# On branch master
# Your branch is ahead of 'origin/master' by 5 commits.

I don't want my branch to be ahead by any number of commits.

(我不希望我的分支领先于任何数量的提交。)

How do I get back to that point?

(我如何回到这一点?)

  ask by Matt Huggins translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

With git reflog check which commit is one prior the merge ( git reflog will be a better option than git log ).

(使用git reflog检查哪个提交是合并之前的提交( git refloggit log更好的选择)。)

Then you can reset it using:

(然后您可以使用以下方法重置它:)

git reset --hard commit_sha

There's also another way:

(还有另一种方法:)

git reset --hard HEAD~1

It will get you back 1 commit.

(它将使您退回1次提交。)

Be aware that any modified and uncommitted/unstashed files will be reset to their unmodified state .

(请注意,任何已修改和未提交/未破坏的文件都将重置为其未修改状态 。)

To keep them either stash changes away or see --merge option below.

(要保留它们,可以--merge更改,或参见下面的--merge选项。)


As @Velmont suggested below in his answer, in this direct case using:

(正如@Velmont在下面的回答中所建议的,在这种直接情况下,使用:)

git reset --hard ORIG_HEAD

might yield better results, as it should preserve your changes.

(可能会产生更好的结果,因为它可以保留您的更改。)

ORIG_HEAD will point to a commit directly before merge has occurred, so you don't have to hunt for it yourself.

(ORIG_HEAD将在合并发生之前直接指向一个提交,因此您不必自己寻找它。)


A further tip is to use the --merge switch instead of --hard since it doesn't reset files unnecessarily:

(另一个提示是使用--merge开关而不是--hard因为它不会不必要地重置文件:)

git reset --merge ORIG_HEAD

--merge

( - 合并)

Resets the index and updates the files in the working tree that are different between <commit> and HEAD, but keeps those which are different between the index and working tree (ie which have changes which have not been added).

(重置索引并更新工作树中<commit>和HEAD之间不同的文件,但保留那些索引和工作树中不同的文件(即,尚未添加的更改)。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...