Here I’ve added a couple of useful tips for working with git. I’ll keep adding to it as I think of more but for now it’s a start.
So we all know it can be dangerous to remove remote branches and tags but once we know we need to do it what can we do?
Delete a Branch Locally:
$ git branch -d branch_name
Delete a remote branch:
$ git push origin :branch_name
This basically pushes nothing to the branch resulting in it being deleted.
Change the path (url) of a remote branch:
git remote set-url remote_name new_path
Delete a Tag Locally:
$ git tag -d tag_name
Delete a remote tag:
$ git push origin :refs/tags/tag_name
This one can be especially useful if you accidentally reset head –soft and want to delete your untracked files. In order to list all untracked files use
$ git-ls -o. You could then for example run
$ git ls-files -o | xargs rm to delete all of these files. Be aware that this command will list all files though. Including those you chose to ignore.
Update: It’s been a while since I’ve updated this but I thought I’d add a few more that I’ve found really useful lately:
Show all branches containing a commit:
$ git branch --contains 3a98e3
Make sure you git fetch before running this locally. If the commit isn’t found then git will probably list all the arguments as if `–contains sha` isn’t a valid option. If the commit is known but not on a branch you’ll see nothing.
You can also check remote branches with:
$ git branch -r --contains 3a98e3
Remove merged branches
Be careful this this one! Make sure you’re on master before you run this too!
git branch --merged | grep -v "\*" | xargs git branch -d
This will do the following:
List all branches that have been merged with the current branch, remove the entry with * as a prefix (the current branch), delete each branch name extracted by the previous grep.