DEV Community

Cover image for Moving from beginner to (slightly more) advanced git with aliases.
Scott Spence
Scott Spence

Posted on • Updated on • Originally published at blog.scottspence.me

Moving from beginner to (slightly more) advanced git with aliases.

The more you work with Git the more familiar you become with the commands used in your every day workflow for your projects or your team's projects.

Commands like naming and creating feature branches making pull requests or pushing your changes if you have the requisite permissions.

So still used by me on a daily basis, and everyone else that uses git [I presume] is the git add . command, then git commit -m 'my awesome feature' and git push or git push origin <branch>

In my short time using Git I have always just typed out the full commands [usually with my cheatsheet close to hand] and thought nothing more of it, that is how you use the tool, right?

Well that was what I foolishly presumed until I learned about dotfiles, I learned about . files from listening to the toolsday.io podcast with Chris and Una a great channel for learning about tooling 👍 the podcast was about Git Tools give it a listen it's a great show.

This was a pretty cool learning experience for me and I now have a pretty efficient git workflow 🚀

Let's go over .gitconfig, do you remember having to enter your email address and name when first setting up Git on your computer? That information is stored in your .gitconfig file, your file will be located in your user folder on Windows C:\Users\yourusername\.gitconfig or ~/.gitconfig on Linux/Mac

If you navigate to the file in the text editor of your choice and pop it open you'll see your details under the [user] flag, here's mine:

[user]
  name = spences10
  email = spences10apps@gmail.com
Enter fullscreen mode Exit fullscreen mode

I'm not sure what other configuration options you may have in yours so we're just going to concentrate on the aliases, aliases can be used so that you can shorten the commands [or make them longer if you like] but I'm all for reducing key strokes, even if it is one or two less.

So lets review the common commands I mentioned at the start:

git add .
git commit -m 'my awesome feature'
git push
Enter fullscreen mode Exit fullscreen mode

So with aliases we can shorten these down a bit:

In your .gitconfig file if there's not already one there add in the [aliases] section, I have mine above my user details, then add in some aliases:

[alias]
  a = add .
  c = commit -am
  p = push

[user]
    name = spences10
    email = spences10apps@gmail.com
Enter fullscreen mode Exit fullscreen mode

So now we can shorten down our workflow for adding a change to one of our repos:

git add .
git commit -m 'my awesome feature'
git push
Enter fullscreen mode Exit fullscreen mode

Will become:

git a
git c 'my awesome feature'
git p
Enter fullscreen mode Exit fullscreen mode

It's not a massive reduction in what you're typing but you'll be amazed at how quickly you become accustomed to it and start adding more an more.

Here's my current list of aliases:

[alias]
  a = add .
  b = branch
  c = commit -am
  cl = clone
  co = checkout
  d = diff
  f = fetch
  i = init
  o = open # see: https://github.com/paulirish/git-open ♥
  p = push
  pt = push --tags
  s = status
  t = tag
Enter fullscreen mode Exit fullscreen mode

A new one I have found out whilst making this post is clone --depth 1 which clones only the HEAD of the repository instead of the whole repository, so say if you were cloning react you'd just get the master version rather than the other 38 branches included in the repository. Pretty neat 👍 so that could be aliased into something a lot shorter git cl1d?

You'll no doubt notice the link I have in there for o = open that little gem belongs to Paul Irish it's an npm package that will pop open a browser tab to the current repository you are in, pretty neat right?

I'm sure there are many, many more ways to configure Git if you take a look at Paul Irish's dotfiles repo for his .gitconfig you'll see there is a lot of ways to configure Git, I'm still learning and finding new ways to do things.

If there is anything I have missed, or if you have a better way to dom something then please let me know 👍

Get me on Twitter or Ask Me Anything on GitHub

If you like this post or if it has helped you in any way then please give it a like and don't forget to share it on social media 🙌

Top comments (8)

Collapse
 
maschall profile image
Mark Schall

Nice guide to git aliases.

I will say though, 'git add .' is a bad habit I see a lot of developers do. I often find others adding more than they want or need to a commit.

+100 for leaving a comment in your aliases. When I add an alias to bash from some online source I like to document it too.

Collapse
 
spences10 profile image
Scott Spence

Hey Mark,

thanks for the feedback.

It's a good point yes and I have bee caught out by this in the past.

What I didn't mention is that I use VSCode and it's awesome git interface for adding specific files for what to commit.

I do use git s (git status) quite heavily when not in VSCode.

Collapse
 
maschall profile image
Mark Schall

I use gitx on Mac to do most of my committing just because I want to be sure what's in and what's not in the commit.

I've always done command line aliases (bash and bat), like 's' for 'git status', but I'm starting to think about using git aliases instead to remind myself I'm still in a git frame of mind.

Collapse
 
lt0mm profile image
Tom • Edited

For that reason I use shell alias which shows status after adding something


a = "!f() { git add ${1-.} $2 $3 $4 && git status; }; f"

and also I will put just several aliases which I find very useful, unfortunately some of them won't work on windows

shows aliases it is very convenient for seldom used aliases and when you just have added some new (won't work on windows)

aliases = !git config --list | grep 'alias\\.' | sed 's/alias\\.\\([^=]*\\)=\\(.*\\)/\\1\\\t => \\2/' | sort  | grep -E --color=auto ^[a-z-]+

I really like short form of status


s = status -s

and because of that st is full form


st = status

log can be in your custom format, mine is a little bit complicated because of colors, it shows hash, date, time, branch, author and message in one line

l = log  --format='%h %Cgreen%ad%Creset %C(bold blue)%an%Creset %Cred%d%Creset %s ' --date=format:'%d.%m.%Y %H:%M:%S

log with modified files list


lf = "!git l --name-status"

Get the current branch name (used in other aliases)


branch-name = rev-parse --abbrev-ref HEAD

Push the current branch to the remote "origin", and set it to track the upstream branch


publish = "!git push -u origin $(git branch-name)"

PS sorry for such big comment
PS2 and I've found one of my sources, there are a lot of useful aliases there, you can chose something which is suited you gist.github.com/robmiller/6018582

Collapse
 
rafaelcg profile image
Rafael Corrêa Gomes

Thanks for sharing these all great tips :)

Collapse
 
equimper profile image
Emanuel Quimper

Really good :) Thank you for this guide

Collapse
 
ben profile image
Ben Halpern

Great guide!

Collapse
 
spences10 profile image
Scott Spence

Thanks Ben, I think I'd like to make a discussion for something like this actually. Get other people's opinions.