If you didn’t already know, you can create shortcuts in Git by adding aliases to your .gitconfig file.
I’ve created a bunch of them for operations that I either 1) do again and again or 2) can’t seem to remember the commands.
Here are my .gitconfig aliases.
(TL;DR? Here’s a link to all of them in a file.)
#1: git sync
My git sync alias was an attempt to replicate the sync functionality in Visual Studio’s git tools. It does a “git fetch” followed by a “git pull”. Another way of thinking of this is that I can type “git sync” on the command line and do a “get latest” from git.
[alias] sync = !git fetch && git pull
#2: git currentbranch
This one isn’t 100% useful by itself — I mostly just use it in other aliases. It returns just the name of the current branch. I could type “git status” and get the value but then that value is wrapped in a bunch of other info. This alias gives me just the name with nothing else.
[alias] currentbranch = rev-parse --abbrev-ref HEAD
#3: git scorch
This one is my attempt to recreate a handy command line operation from Team Foundation Version Control (TFVC). It reverts any changes that I have on disk and removes any files that may have been created that aren’t in version control. (This one uses ‘currentbranch’ as part of the alias.)
[alias] scorch = !git fetch origin && git reset --hard origin/$(git currentbranch) && git clean -fd
#4: git branchcleanup
This one removes any local branches that aren’t on the remote server anymore.
[alias] branchcleanup = !git remote prune origin
#5: git remotes
This shows me the list of remote servers for my git repo. This is helpful if I need to get the URL for my git repo for some reason or if I just want to confirm that I’m working in the repo that I think i’m working in.
[alias] remotes = !git remote -v
#6: git info
This one is similar to ‘git remotes’ but this only returns the URLs for the remote repositories. Basically the URLs without any extra stuff. This is helpful for scripting stuff.
[alias] info = !git remote get-url origin
#7: git branches
This lists the branches that I have in my repo. This is one of those aliases that I created because I simply could not remember the actual syntax.
[alias] branches = !git branch -a
#8: git unstage
Let’s say that I’ve made a bunch of changes locally that I haven’t commited yet. I’m not ready to commit those changes either…but I need to make a change to a single file and I want to temporarily get all my pending changes out of the way. Basically, I want to take all those “staged” changes and then “unstage” them.
This alias unstages all my current changes.
[alias] unstage = !git reset --
#9: git sc
This one alias stages all my changes and then initiates the commit process. That’s why it’s “sc” — Stage Commit.
[alias] sc = !git stage . && git commit
#10: git scm
This alias builds off of the “sc” alias and lets me do a stage followed by a commit AND ALSO specify the comment for the commit all in one command line call. Anything to the right of the “git scm” becomes the comment. Why “m”? Because the commit Message it part of the alias. Plus, the command line arg for the message is ‘-m’.
[alias] scm = "!f() { set -x; msg=\"$@\"; git stage .; git commit -m \"$msg\"; }; f"
#11: git scmp
This builds off the “sc” and “scm” and does a stage, a commit with a message, and then does a “git push” to push the changes up to the remote server. Anything to the right of the “git scmp” becomes the commit message.
[alias] scmp = "!f() { set -x; msg=\"$@\"; git stage .; git commit -m \"$msg\"; git push; }; f"
#12: git lazy
This one is probably going to make some heads explode. It builds off of “git scmp” except that it auto-generates a commit message for me. The commit message is “lazy commit:” followed by the current date and time. This is really helpful for when I’m developing Azure DevOps Pipelines scripts or GitHub Actions scripts. Pretty much any time that I’m making a zillion tedious little changes trying to get something to work and I don’t particularly care about having a descriptive commit message.
[alias] lazy = "!f() { msg='lazy commit: '$( date '+%F_%H:%M:%S' ); git stage .; git commit -m \"$msg\"; git push; }; f"
#13: git tagversion
I use this when I want to create a tag in my git repo for a particular version of my code. It automatically creates a tag for me that starts with “creating tag for v”. The idea is that i’d type in a version number and then that tag would become something like “creating tag for v12” just by typing in “git tagversion 12”. This saves me a lot of time when I’m creating new versions of my GitHub Actions.
[alias] tagversion = "!f() { msg=\"creating tag for v${1}\"; git tag -m \"$msg\" v${1}; }; f"
#14: git pushtags
If I’ve created a tag in my local repo and want to push that tag to the remote…well, I could never remember the syntax. This alias pushes any tags I’ve created up to the remote repo.
[alias] pushtags = !git push --follow-tags
#15: git history
I created this one so that I could quickly see a simple list of commits in a repo.
[alias] history = !git log --oneline -n 20
#16: git historylong
This one is similar to “git history” except that I shows me a longer version of the history info.
[alias] historylong = !git log --stat
Summary
That’s my list of aliases that I’ve created in my .gitconfig file. Here’s a link to download all of my gitconfig aliases at once.
I hope they help you out and you find them useful.
-Ben
Leave a Reply