Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete a branch and all the objects it referenced

Tags:

git

The issue is that after your delete and push branch it isn't lost forever and it is still in repository. I've deleted branch with big amount of needless files, but as long it is still somewhere in git repository, git clone command duration is too big.

For now only way I see is to delete whole repository and recreate it but without needless branch.

like image 574
Andriy Kopachevskyy Avatar asked May 12 '11 12:05

Andriy Kopachevskyy


People also ask

How do I completely delete a 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.

How do I delete a branch in bulk?

To delete many branches based on a specified pattern do the following: Open the terminal, or equivalent. Type in git branch | grep "<pattern>" for a preview of the branches that will be deleted. Type in git branch | grep "<pattern>" | xargs git branch -D.

Does deleting a branch delete all 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.


2 Answers

I believe git gc --prune=now will do what you want: clean up unnecessary files from your repository.

By default git gc removes unreachable commits older than 2 weeks, so adding --prune=now is what you need.

like image 190
CharlesB Avatar answered Oct 07 '22 12:10

CharlesB


You need to remove files completely by using git filter-branch

http://git-scm.com/docs/git-filter-branch

You can do a lot of magic with this command, the following will remove filename from all commits:

git filter-branch --tree-filter 'rm -f filename' HEAD
like image 37
ralphtheninja Avatar answered Oct 07 '22 13:10

ralphtheninja