Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git merge into past commit

We have a git history that looks like this. This is already pushed to remote but can I possibly merge the branches at certain commits? Or am I limited to only merging the commit at the very top? Ruined git history

where blue is origin/master and magenta is a feature branch that has been physically copied into the master branch.

like image 845
JReno Avatar asked Mar 16 '18 05:03

JReno


People also ask

Is it possible to revert a merge?

You can use the Git reset command to undo a merge. Firstly, you need to check for the commit hash (or id) so you can use it to go back to the previous commit. To check for the hash, run git log or git reflog . git reflog is a better option because things are more readable with it.

How do I merge a branch to a specific commit?

Merge upto commitgit checkout src git branch temp commitId # create temporary branch at commitId git switch dst git merge temp # merge temp branch to dst branch git branch -d temp # remove temp branch as we dont need it anymore.

How do I merge last two commits?

Just run git reset --soft HEAD~10 , where 10 is the number of commits you want to merge. This can be used if you don't have a remote origin set, and you only have two commits.


2 Answers

Assume the commit history as below, and you want to merge feature branch into commit B:

...---A---B---C---D  master

...---E---F---G---H  feature

Then you can execute below commands:

git checkout commitB
git merge feature --allow-unrelated-histories

Assume the merge commit id commit M as below commit history:

                  C---D  master
                 /
      ...---A---B---M 
                   /
...---E---F---G---H  feature

Then you can execute the commands:

git rebase --onto commitM commitB master
git push origin master -f

And now the commit history will be:

      ...---A---B---M---C'---D'  master 
                   /
...---E---F---G---H  feature
like image 137
Marina Liu Avatar answered Oct 23 '22 13:10

Marina Liu


Being in the master branch then git merge "commit-id" This should do it

like image 34
Monty Avatar answered Oct 23 '22 12:10

Monty