Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge old git commit with HEAD at master

I made some mistakes when merging commits of my colleagues. Now, we have discovered it and we need to apply old commit again, to choose manually the changes in the files. The situation looks like this:

A--\       /--F--\
    C--D--E       H--I
B--/^      \--G--/   ^
    |                |
WRONG MERGE       MASTER

I need git to ask me again to merge B with I, like B had never been in the history. In other words, I need git to ask me to choose the merge for all the files which differs in B and I commits. What's the best approach to do such thing? Can I achieve this with cherry pick? How?

like image 634
Pavel S. Avatar asked Aug 10 '12 16:08

Pavel S.


People also ask

How do I merge changes in a previous commit?

Changing the Last Commit: git commit --amend. The git commit --amend command is a convenient way to modify the most recent commit. It lets you combine staged changes with the previous commit instead of creating an entirely new commit.

Should you squash commits before merging to master?

As a general rule, when merging a pull request from a feature branch with a messy commit history, you should squash your commits. There are exceptions, but in most cases, squashing results in a cleaner Git history that's easier for the team to read.


1 Answers

You could use commit duplication trick:

# make a branch named B started on the commit B
git checkout -b B <sha1 id of the commit B> 
# it creates a branch which starts at a commit
# right before B and has exact copy of commit as B,
# but with a different sha1 id.
git rebase --no-ff HEAD~1

# now we do simple merge
git checkout master
git merge B

It also allows to make the trick with range of commits and preserves dates, authors, commit messages and so on.

In fact your scenario is described in documentation:

--no-ff ... recreates the topic branch with fresh commits so it can be remerged successfully

like image 124
kan Avatar answered Sep 30 '22 20:09

kan