loading...

Teaching Git Some New Tricks

sublimegeek profile image Jonathan Irvin Updated on ・1 min read

First (second) dev.to article! Learn to hit the save button, Jonathan!

You can create aliases both on the repo level and globally just by adding or omitting the --global flag.

Creating aliases is easy. Just type git config --global alias.co checkout to create a global alias for git checkout. Now, all you have to do is type git co develop. Awesome, right?

Some commands have flags you can add. For example, the checkout command allows you to optionally create and checkout a new branch with the -b flag. Git handles these aliases with ease by just surrounding the command with quotes. I like to use cob for that.

git config --global alias.cob "checkout -b"

Now, to create a new branch, I just type git cob new_branch_name.

Here are some of my favorite aliases:

st status
co checkout
cob checkout -b
lol log --oneline

If you want to chain commands together, which begin with git, you have to prefix that with a bang (!).

Say I wanted to stash my changes and do a pull in one command. Let's call this alias "shelve". I would type the following:

git config --global alias.shelve "!git stash && git pull"

I'm liking dev.to so far and I hope to write some more articles. I especially like talking about Git.

Reference: git config

Discussion

pic
Editor guide
Collapse
bbodine1 profile image
Brad Bodine

That is good stuff, the only issue with this is that if you become dependent on these specific commands and you have to use a different environment (someone else's machine) you could forget the actual commands.

I use text expander on osx to do similar things with common git commands.

Just something to keep in mind. 😁

Collapse
qm3ster profile image
Mihail Malo

You could always quickly source your alias config from let's say a gist.

Collapse
sublimegeek profile image
Jonathan Irvin Author

Oh yeah, for sure. I strongly advocate learning the commands before making an alias. But if you switch workspaces as much as I do, I'm creating aliases as much as I'm typing the full command.

Collapse
marsavela profile image
Sergiu Marsavela

I always end up relying on *sh aliases rather than using git's alias. In the end, gs or gst is still better than git st to me.

Collapse
ramlev profile image
Hasse R. Hansen

typo: git log --online should be git log --oneline

Collapse
hoelzro profile image
Rob Hoelz

Haha, I mistype that one so much that I define online as an alias for oneline in my gitconfig!

Collapse
sublimegeek profile image
Jonathan Irvin Author

Haha! Oops! Thanks for catching that!

Collapse
sublimegeek profile image
Collapse
jeklah profile image
Jeklah

One of my favourites is git config --global alias.oops "commit --amend --no-edit"

Collapse
iamchandankr profile image
Kumar Chandan

Thanks for writeup.
It would help if guidelines to undo the global alias set up is given here as well.

Collapse
rounook profile image
Rounook

Great Stuff !! Thanks.

Collapse
qm3ster profile image
Mihail Malo

Interesting that git has this feature.
What is the advantage over just using shell aliases? Does autocompletion work in one case but not the other?