-1

How can I combine the last 9 commits in my GitHub repository into one commit?

Ry-
  • 218,210
  • 55
  • 464
  • 476
Nazmul Hasan
  • 10,130
  • 7
  • 50
  • 73

4 Answers4

2
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.

Ry-
  • 218,210
  • 55
  • 464
  • 476
1

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.

jwg
  • 5,547
  • 3
  • 43
  • 57
0

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.

cst1992
  • 3,823
  • 1
  • 29
  • 40
0

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

user4252523
  • 69
  • 2
  • 8