Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GIT: How to pull changes from branch after a reverted merge

I hope the title is not very confusing.

History (aka What I did wrong):

Let's say there are two branches, master and feature. Feature is a branch where I keep a longer feature project and from time to time I use git pull origin master on that branch to stay updated with changes in main code.

I accidentally merged and pushed feature into master.

It was nowhere near ready, so I tried to revert it. I tried following: http://sethrobertson.github.io/GitFixUm/fixup.html#pushed_new_merge But I guess I did it wrong. I used: git revert then commited the revert and pushed it. Problem seemed solved - the feature changes where no longer on master.

Now the problem is that when I try to use git pull origin master on feature branch it keeps deleting my files and reverting all changes, as it is merging with the revert-commit. The head of master is way past that revert and I need to pull the changes from newer commits to continue with feature branch.

I've googled the topic and the most promising answer is this: https://metlos.wordpress.com/2012/01/13/git-merging-after-a-revert/ but here the merge goes other way than I want to (feature to master, i need master to feature).

I am thinking of creating clone branch of master, then applying the instructions from the link on that branch so I can get the results I need, but I am afraid it's a temporary solution I would need to apply anytime I want to pull from master.

I tried best for this story not to be chaotic, If something is not understandable I will clarify.

like image 693
ef. Avatar asked Aug 21 '15 11:08

ef.


People also ask

What happens if you revert from merge?

Using git reset to Undo a Merge This means that any local changes in your Working Copy will be discarded; if you have valuable uncommitted changes, be sure to use git stash before.


1 Answers

If you have not pushed master branch to a team/public repository and you have no commit after the pull on master, you may do

git reset --hard **the-commit-before-pulling-feature-branch**

on the master branch. This will effectively wipe out the mistake from history and gives a clean history.

otherwise, (i.e. if you have already pushed master to a repo or done more commits on master), you may do

git revert **the-revert-commit**

on the feature branch after pulling the master, effectively reverting the revert commit.

like image 150
behzad.nouri Avatar answered Oct 07 '22 09:10

behzad.nouri