2

I want to clean up my commit history by removing all the previous commits and just keeping the latest one. How can I do that in git ? I did try

git rebase -i ..but that seems to end in blood where I end up detaching my Head .. git prune --expire doesn't seem to work either.

so if I have commits 1 to 10. I would just like to keep the 10th one with me now. All of this is local. So that when I do decide to push I am only pushing 10.

ashutosh raina
  • 9,228
  • 12
  • 44
  • 80
  • You don't explain what you did and what the results were. "that seems to end in blood" and "doesn't seem to work" are not problem descriptions. – melpomene Dec 27 '12 at 12:09
  • @melpomene I did When i clearly wrote that I wanted to delete all the previous commits from history.. I opened up gitbash ..tried the two commands I mentioned which are supposed to do this and found that they were not doing it the way I expected them ..googled..scratched my head ..torched my repository for a day ..and finally asked the question.. – ashutosh raina Dec 27 '12 at 12:12
  • I followed this and several other answers on SO http://ncona.com/2011/07/how-to-delete-a-commit-in-git-local-and-remote/ – ashutosh raina Dec 27 '12 at 12:13
  • 1
    "found that they were not doing it they way I expected" is still not a problem description. 1) What did you expect to happen? 2) What actually happened? – melpomene Dec 27 '12 at 12:14

1 Answers1

3

The procedure is described in the git book:

Squashing Commits

It’s also possible to take a series of commits and squash them down into a single commit with the interactive rebasing tool. The script puts helpful instructions in the rebase message:

#
# Commands:
#  p, pick = use commit
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#

If, instead of "pick" or "edit", you specify "squash", Git applies both that change and the change directly before it and makes you merge the commit messages together. So, if you want to make a single commit from these three commits, you make the script look like this:

pick f7f3f6d changed my name a bit
squash 310154e updated README formatting and added blame
squash a5f4a0d added cat-file

If you don't want to squash seceral commits into just one, but actually remove commits completely, then do what the script tells you to do:

# If you remove a line here THAT COMMIT WILL BE LOST.
Community
  • 1
  • 1
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • 3
    say for example I have 100 commits and I need only one commit to be picked and drop the other commits. Can you help me on this – Aravind Dec 27 '17 at 05:35
  • After squash will it also remove footprint of GitHub issue references if they take place in the README.md? – alper Aug 21 '21 at 12:24
  • It's more robust to include the steps directly in the SO answer rather than point to external resources, because links die over time, such as the one above now pointing to a completely different chapter :/. – Dwayne Robinson Jul 14 '22 at 22:44