mbells
mbells

Reputation: 3858

How to remove a specific commit in the local git repo?

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.

Upvotes: 1

Views: 8633

Answers (4)

torek
torek

Reputation: 489718

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

Upvotes: 1

ralphtheninja
ralphtheninja

Reputation: 133168

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

Upvotes: 3

Bombe
Bombe

Reputation: 83943

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.

Upvotes: 1

lprsd
lprsd

Reputation: 87205

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'
    

Upvotes: 5

Related Questions