简体   繁体   中英

Merge two commits where commits are between

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

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 .

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM