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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With