Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge two commits where commits are between

Tags:

git

git-merge

I want to merge two commits - there are commits between these two commits. The commits between don't change files edited by commit 1 and commit 6. The commits are not pushed yet.

Is this possible?

f68ffb5 commit 6
...
d294fac commit 1
like image 615
boop Avatar asked Jun 08 '15 08:06

boop


People also ask

How do I merge two commits in Git?

If you're currently on your "commit 1", and the commit you want to merge, "commit 2", is the previous commit, you can run git rebase -i HEAD~2 and change the first word of the second line from "pick" to "squash". Then write your file, and quit.

What is a merge commit in a standard merge?

A standard merge will take each commit in the branch being merged and add them to the history of the base branch based on the timestamp of when they were created. It will also create a merge commit, a special type of “empty” commit that indicates when the merge occurred

How do I push my first commit into my second commit?

Git will squash your first commit into your second last commit. Note that this process rewrites the history of your branch. If you are pushing your code somewhere, you'll have to git push -f and anybody sharing your code will have to jump through some hoops to pull your changes.

Why can't I See my merged commits in the timeline?

By navigating back before the merge commit, we won't get any of the individual merged commits in our history. It's only after that merge commit that they are included in the timeline.


3 Answers

As I already mentioned in my comment. You could do the following:

git rebase -i and than reorder the commits like 162345 or 612345 and than squash them together to get a history like (1+6)2345.

like image 172
ckruczek Avatar answered Sep 30 '22 14:09

ckruczek


For commits in the same branch

# make sure you're on the proper branch
git checkout branch-with-commits

This is important: you have to give reference to a commit before the one that you want to squash to. Here we make the "before" reference with ^.

# rebase to the commit before commit 1
git rebase -i d294fac^

An editor window opens. It's probably vim, and if you're not familiar with it, checkout How to exit the Vim editor?

In this list the commits go in reverse order (not as in git log): from the earliest to the latest.

Rearrange lines in the following way:

pick d294fac commit 1
squash f68ffb5 commit 6
pick <sha1> commit 2
pick <sha1> commit 3
pick <sha1> commit 4
pick <sha1> commit 5

# and if there are commits later that commit 6:
pick <sha1> commit 7
...

Save the document. Git now opens a new editor window for the message of new commit 1&6. Save it too. The rebase is done.

like image 27
Nick Volynkin Avatar answered Sep 30 '22 15:09

Nick Volynkin


you can use git rebase, considering that commit 1 is 6 commit beahind head:

git rebase -i HEAD~6

a git windows will open with the 6 commits and explain you what the options are. You want to combine commit 6 and commit 1, so squash commit 1 into commit 6:

pick f68ffb5 commit 6
...
squash d294fac commit 1

an other git window will open for you to edit the commit message.

like image 41
maggick Avatar answered Sep 30 '22 13:09

maggick