List of Useful Git Commands
Introduction
There are a few advanced things that I want to do with git from time to time and I always forget the commands. So I thought I would write them down here. Here are my gitbits…
Gitbits
Aliases
git config --get-regexp ^alias
I have started to use aliases to make my life easier, saves me having to remember commands that I use regularly. Trouble then is remembering the alias :) This command will list all of the aliases that I have created. It also returns the command.
Remotes
git remote -v
This command will list all of the remotes that are configured for the repository. I use this to check that I have the correct remotes configured or to find out where I put the remote if I’ve forgotten :)
Branches
I’ve found that over time, keeping a local repository tidy can be a bit of a pain. I have a few commands that I use to help me keep things tidy.
git status
Not an uncommon command admittedly, but worth pointing out that if you run this on a feature branch it will inform you of the remote tracking branch that it is associated with, or if the upstream branch has been deleted.
git fetch --prune
Git fetch
by default updates your local repository with all existing remote branches, but does not remove any that have been deleted. This command will remove any remote tracking branches that have been deleted on the remote repository.
git log --branches --not --remotes --no-walk --decorate --oneline
This lists any local branches that have not been pushed to the remote. I use this to check that I have pushed all of my changes. Note that if a branch has been merged remotely, then it’s local counterpart will get listed. I have aliased this to git unpushed
.
git fetch --all -p; git branch -vv | grep ': gone]' | awk '{ print $1 }' | xargs -n 1 git branch -D
Following on from the previous command, this will delete any local branches that have been merged remotely. I use this to keep my local repository tidy. This is super destructive and should be used with caution, I have a blog post to write on what this does and how to use it. I have aliased this to git deleteMerged
albeit I had to jump through some hoops to get this working correctly in my git config file.
More to follow
Will add more as and when I think of them. Any commands you find useful? Please let me know in the comments!