DEV Community

loading...
Cover image for Aliases git way

Aliases git way

konstantin profile image Konstantin Originally published at konstantin.io Updated on ・2 min read

Often I see developers tweet their git aliases and most of the time they are using standard alias functionality of their shell. Kind of like that:
alias gs = git status

git actually provides you with the ability to make an alias for some git command or even create your own command. You can do it in the [alias] section of your .gitconfig file(which can be found in your user’s home folder).

Aliases

So for instance if we take the previous example, you can write something like:

[alias]
  s = status
Enter fullscreen mode Exit fullscreen mode

and you will be able to use it as following:
git s

Now you might say that it is much longer then «gs». But you can also make an alias for git itself in your shell:

  alias g = git
Enter fullscreen mode Exit fullscreen mode

and so now you can simply call your alias by typing g s.

Custom commands

Apart from aliases you can actually create some really cool custom commands. For instance, we can add an alias to see last 10 branches that you’ve checked out:

[alias]
  recent = for-each-ref --count=10 --sort=-committerdate refs/heads/ --format="%(refname:short)"
Enter fullscreen mode Exit fullscreen mode

Which I shamelessly picked from csswizardry’s article. When you run it(g recent) you’ll see a list of your last ten branches(or less if you haven’t checked out ten).

Using shell within git alias

You can also do «shell evaluate» within git alias:

[alias]
  compare = "!f() { git log --oneline $1..$2; }; f"
Enter fullscreen mode Exit fullscreen mode

Here we define an alias, that contains an anonymous function that is invoked straight away. We can also pass parameters to this function. In this example we pass two revisions or branches so that we get a list of commits that differs them.

You can use this alias as follows:
g compare branch1 branch2

More stuff

You can see the examples above and some other cool stuff in the gitconfig file from my dotfiles repo.

Discussion (1)

pic
Editor guide
Collapse
michaelcurrin profile image
Michael

Thanks for sharing.

I've linked to your post in my tutorial and share of my git aliases.

dev.to/michaelcurrin/dotfiles-git-...