7

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
boop
  • 7,413
  • 13
  • 50
  • 94
  • Ok than go for it! What kind of answer do you expect? There are too less information to answer your question. Are these commits located in different branches? If not. You could take `git rebase -i` in concern and reorder the commits and than squash 1 in 6 afterwards. But this is just wild guessing without any furhter information from your side – ckruczek Jun 08 '15 at 08:23
  • @ckruczek: For me, his question is quite clear and the answer is like you said: `rebase -i` + reorder to 162345 + squash 6on1. Could you rewrite the comment as an answer? – quetzalcoatl Jun 08 '15 at 08:26
  • 1
    Ofcourse I could, but the commits could also come out of different branches. But ill do my very best. – ckruczek Jun 08 '15 at 08:27

3 Answers3

3

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.

Community
  • 1
  • 1
Nick Volynkin
  • 14,023
  • 6
  • 43
  • 67
2

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.

ckruczek
  • 2,361
  • 2
  • 20
  • 23
1

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.

maggick
  • 1,362
  • 13
  • 23