Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to REALLY delete a git branch (i.e. remove all of its objects/commits)?

Tags:

I have a git tree like

                 A---B---C topic                 /            D---E---F---G master     <-- 

I would like to remove topic and all objects on it.

I note the SHA ID of topic, then type:

git branch -D topic git gc                                   #  <-- I also tried prune here... git checkout -b temp <SHA1 ID of topic> 

After the last command I expect to get an error (something like "Non-existent object ID..." or somth. like that). However there is no error and gitk shows the same tree structure as above??

What am I missing - I thought gc/prune are supposed to delete all unreachable objects?

like image 634
Alan Avatar asked May 21 '10 13:05

Alan


People also ask

How do you remove all commits from a branch git?

To remove the last commit from git, you can simply run git reset --hard HEAD^ If you are removing multiple commits from the top, you can run git reset --hard HEAD~2 to remove the last two commits. You can increase the number to remove even more commits.

Does deleting a git branch delete the commits?

What Happens If I Delete a Git Branch? When you delete a branch in Git, you don't delete the commits themselves. That's right: The commits are still there, and you might be able to recover them.

How do you delete a git branch?

Deleting a branch LOCALLY Delete a branch with git branch -d <branch> . The -d option will delete the branch only if it has already been pushed and merged with the remote branch. Use -D instead if you want to force the branch to be deleted, even if it hasn't been pushed or merged yet. The branch is now deleted locally.

What is the git command to delete all the remote branches?

Let's break this command: First we get all remote branches using the git branch -r command. Next, we get the local branches not on the remote using the egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) command, Finally we delete the branches using the xargs git branch -d command.


1 Answers

Just the gc prune is often not enough to get rid of the extra objects in the repo. If commits are still referenced in the reflog, it won't consider those objects unreachable and hence ripe for pruning.

Here's what worked for me:

git reflog expire --expire=now --all git gc --aggressive --prune=now git repack -a -d -l 

This will make some changes to your repo's history, and may well present difficulties if others are depending on the branches you blow away.

You may have to re-clone your repository to actually see a difference in its size.

like image 112
Duke Avatar answered Sep 29 '22 10:09

Duke