1

My local repo contains the following commits:

A ---- B ---- C ---- D ---- E
        \
         1 ---- 2
          \    /
           1.1 

The lettered changes are on the remote repo. Changes 1, 1.1, and 2 are temporary changes in my local repo that i no longer want. These may have been automatically created when i created a stash with uncommitted changes on top of B; that stash has been deleted. I'd like to make the history less complicated, which is why i'd like to get rid of these.

I can clearly get rid of these by blowing away my local repo and cloning the remote repo again, but this seems heavy handed. And there is a local branch not in the remote repo that i want to keep.

From what i've read so far, the discussions have been on how to squash multiple commits into one. I haven't found anything on deleting commit objects. I also tried to "git prune <hash of commit 2>" and it didn't do anything.

How do i get rid of commit objects 1, 1.1, and 2?

Thanks in advance.

mbells
  • 3,668
  • 3
  • 22
  • 21

4 Answers4

5

Just go to the commit that you care about and set it as the HEAD. git garbage collects the 1.1 and 2 at some later point in time.

git checkout sha-of-1
git reset --hard HEAD

An alternative way to go about the same thing would be:

  • Create a new branch from point you care about.
  • delete all other branches.

    git checkout sha-of-1
    git checkout -b new-branch
    git branch -D 'the branches that have 1.1 and 2'
    
lprsd
  • 84,407
  • 47
  • 135
  • 168
3

Assuming you are working on the master branch, just reset it to origin/master (E):

git checkout master
git reset --hard origin/master

This will make your commit objects loose objects and will be cleaned up next time you do git gc

rtn
  • 127,556
  • 20
  • 111
  • 121
1

Is what you described the situation that is shown in gitk? Is it still the same after a complete refresh (Ctrl-F5)? If it stays the same you still have some reference that points to commit “2.” Remove that reference (most probably a branch) and the whole branch will disappear. At a later point the garbage collection will then remove the commit objects.

Bombe
  • 81,643
  • 20
  • 123
  • 127
  • Thanks! F5 (refresh) didn't do it, but Ctrl+F5 (reload) did. I didn't realize that refresh would leave it in a state, well, not so fresh ;-). – mbells Mar 16 '12 at 14:56
1

@Bombe and @Magnus Skog are right (Lakshman Prasad is close but you said you wanted to get rid of 1 as well). But maybe what you mean is: "I've tossed all the visible references (i.e., branch and tag names) to 1, 1.1, and 2, and even done a git gc, and I still have the commits in there because I can see them by SHA and they are taking up too much disk space".

If this is the case, the easiest thing is to just wait: the references that you can't see, that are preventing the space from being gc'ed, are probably in the reflog (.git/logs/). They expire after a while and then those commits can be gc'ed. Or, you can use git reflog --expire-unreachable to force them to expire more quickly. See also git reflog expire and git fsck --unreachable.

Community
  • 1
  • 1
torek
  • 448,244
  • 59
  • 642
  • 775