How can I combine the last 9 commits in my GitHub repository into one commit?
4 Answers
git stash -u # save uncommitted changes
git reset --soft HEAD~9 # move back 9 commits
git commit # recommit them
git stash pop # restore uncommitted changes
To push the result back to GitHub, make sure your local copy was up to date before the operation, and force it: git push -f
.

- 218,210
- 55
- 464
- 476
-
3Why not just do a soft reset? `git reset --soft` – cst1992 Aug 21 '17 at 09:55
-
@jwg: It is, however, non-interactive. You also need the `stash` step to do a rebase anyway, so this is really only three lines. The rebase would be `git stash -u; git rebase -i HEAD~10; git stash pop` plus an edit of all lines to `s`. – Ry- Aug 21 '17 at 09:57
-
@Ryan That's what I'm saying, you don't need the cherry-pick if you do a soft reset. – cst1992 Aug 21 '17 at 09:59
-
@cst1992: Oh yeah sorry. This was worked outwards from the `cherry-pick` =) – Ry- Aug 21 '17 at 10:00
-
I am getting error `! [rejected] development -> development (non-fast-forward) error: failed to push some refs to 'https://github.com/nazmulkp/Love.git'` – Nazmul Hasan Aug 21 '17 at 10:31
-
@NazmulHasan: `push -f` – Ry- Aug 21 '17 at 11:12
-
@Ryan no way if I doing `push -f` then deleted all commit from init – Nazmul Hasan Aug 21 '17 at 12:02
-
@NazmulHasan: It will delete 9 commits and replace them with 1, yes, the 1 being their combination. That’s exactly what you asked for? – Ry- Aug 21 '17 at 12:28
-
@NazmulHasan: Is that *not* what you wanted to do? – Ry- Aug 22 '17 at 09:11
-
@Ryan anyway thanks – Nazmul Hasan Aug 22 '17 at 13:45
Do an interactive rebase:
git rebase -i HEAD~9
You will be put into an editor with a list of the last 9 commits, in chronological order. Each starts with the word 'pick'. You can change any of these to 'squash' or 's', to squash it into the previous commit. You will be given a chance to edit the commit message of the squashed commit. By default this will be the commit message of all the commits concatenated together.
You can also delete commits, replace 'pick' with 'reword' to be given a chance to change the commit message, 'edit' the commit as well as the message, etc.
Interactive rebase is very powerful so it's worth learning to use it.

- 5,547
- 3
- 43
- 57
-
-
To be clear, edit allows you to edit both the file changes in the commit, and the commit message. – jwg Aug 21 '17 at 10:01
Use squashing: http://gitready.com/advanced/2009/02/10/squashing-commits-with-rebase.html
Squashing lets you "squash" multiple commits into one commit. So basically your 9 commits will become one commit.

- 3,823
- 1
- 29
- 40
-
Obviously this'll change the ID's of every involved commit and any others on top, so do this before pushing to the remote repository. – cst1992 Aug 21 '17 at 09:54
-
I think the best way to do it would be to use the git rebase interactive.
git rebase -i HEAD~9
What this would do is open up last nine commits and you can edit them the way you like. You can drop any of the last nine commits. Since you are looking to combine (replace is a bit cofusing, so avoid using that word) all of those nine commits into one, you should be squashing those commits.
Use this website to understand more about git rebase interactive. I found this very useful when I was fairly new to git. This technique is very handy. Git rebase interactive description

- 69
- 2
- 8