Home > front end >  Git merge doesn't work as expected after a revert?
Git merge doesn't work as expected after a revert?

Time:11-03

I've created a simplification of a scenario in which I'm trying to understand why it works like that.

Here is the timeline of commits:

enter image description here

At first I've created a new branch named dev feature - both had one commit:

Let's look at dev :

enter image description here

Then added another line and committed :

enter image description here

Then another one :

enter image description here

And another one :

enter image description here

Then I switched to feature branch and added "bad code" :

enter image description here

Then I've switched back to dev and merged feature to dev :

enter image description here

So now I decided to revert the merge commit :

git revert -m 1 2c618a9

(parent 1 is the dev branch) and the revert did work as expected.

However, When I try ( only for testing purposes) to re-merge the feature branch to dev , nothing seems to happen: and I do not see the bad commits again :

enter image description here

Question

As you can see, the bad commits are not entered again. Why is that ? What if I wanted to merge it again?

CodePudding user response:

git merge first looks at how commits are linked one to another :

if you look at your repo's history graph (the blue and red dots on the left of your screenshot), you can see that the result of git revert will :

  • keep the existing history (including the previous git merge feature)
  • add a new commit on top of that

So the next time you try to run git merge feature, git will say "oh, but feature is already merged into dev", and will do nothing more.


One possibility to "re merge" feature is to use git cherry-pick to select the commit(s) that you want to replay on dev :

git cherry-pick <bad commit>

# if there were several commits to 're merge' :
git cherry-pick <commit1> <commit2> ...
  •  Tags:  
  • git
  • Related