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
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.
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
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.
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.
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
.
# 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With