Reader small image

You're reading from  Git for Programmers

Product typeBook
Published inJun 2021
PublisherPackt
ISBN-139781801075732
Edition1st Edition
Right arrow
Author (1)
Jesse Liberty
Jesse Liberty
author image
Jesse Liberty

Jesse Liberty is a full-time hands-on programmer, specializing in C#, git and .NET MAUI. He hosts the popular Yet Another Podcast and is the author of more than a dozen best-selling programming books. Liberty is a Certified Xamarin Developer, a Xamarin MVP and a Microsoft MVP. He was a Technical Evangelist for Microsoft, Distinguished Software Engineer at AT&T; Software Architect for PBS and Vice President of Information Technology at Citibank, and he was on the teaching staff at Brandeis University. Jesse is a recognized expert and has spoken at conferences world-wide.
Read more about Jesse Liberty

Right arrow

Aliases

Stop working so hard! In this chapter, we will look at Git aliases, which greatly reduce the amount of typing you have to do. Aliases can be very simple, or they can take arguments and flags.

Aliases

Aliases they allow you to create shortcuts to git commands. For example, I have the alias st, which stands for status. Thus, I enter:

git st

and it is exactly as if I had entered:

git status

We'll get to more exciting and useful aliases in just a moment, but first let's look at how these are created. To create an alias:

  • Enter git
  • Enter the keyword config
  • Enter the flag --global
  • Enter the keyword alias followed by a period and then the alias itself
  • Enter the command you are aliasing

This sounds more complicated than it is. For example, to create the st alias, I entered:

git config --global alias.st status

Of course, you don't have to use global. Your alternatives are system and local, but personally, I always use global because I'm the only one on this computer and I want it to always be available.

Here is a slightly more complicated alias that allows you to create a branch and...

Summary

Aliases are a convenient way to shorten otherwise lengthy commands. You create an alias with this sequence:

  • Enter git
  • Enter the keyword config
  • Enter the flag --global
  • Enter the keyword alias followed by a period and then the alias itself
  • Enter the command you are aliasing

You can access the configuration file directly with:

git config --edit --global

Aliases are simple, easy, and incredibly useful when working at the command line.

Challenge

Create an alias that replaces the following command:

git log ––name-only --oneline

Answer

To do this, I will go to the command line and enter:

git config --global alias.nx  "log --name-only --oneline"

The double quotes are needed because you are using two flags on log.

The result of calling this command is shown in Figure 8.2:

Figure 8.2: Our new alias at work

Notice that each commit is there, represented on a single line and with only the SHA and message (except when there is a tag or pointers, as shown on line 1 and line 7 in Figure 8.2).

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Git for Programmers
Published in: Jun 2021Publisher: PacktISBN-13: 9781801075732
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Jesse Liberty

Jesse Liberty is a full-time hands-on programmer, specializing in C#, git and .NET MAUI. He hosts the popular Yet Another Podcast and is the author of more than a dozen best-selling programming books. Liberty is a Certified Xamarin Developer, a Xamarin MVP and a Microsoft MVP. He was a Technical Evangelist for Microsoft, Distinguished Software Engineer at AT&T; Software Architect for PBS and Vice President of Information Technology at Citibank, and he was on the teaching staff at Brandeis University. Jesse is a recognized expert and has spoken at conferences world-wide.
Read more about Jesse Liberty