Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
My Favorite Git Aliases

My Favorite Git Aliases

I use git alot since I prefer the command line for improving developer velocity. A part of that is configuring the tool to do whatever tricks it offers to reduce typing and increase usability. Git is great tool for CVS but sometimes there is a lot to remmeber and a lot to type. Thankfully, git allows you to create aliases for your favorite, most used, or most lengthy commands. Git aliases can even take parameters. So here are few so I don't have to look them up any more.

Git Status Without All the Verbiage (The s Alias)

The git status command produces an aweful lot of output, much of it only interesting to the newbie or under special circumstances (like when you are a bit lost in the code). Observe:

$ git status
On branch master
Your branch is up to date with 'bb/master'.

Untracked files:
  (use "git add ..." to include in what will be committed)

        junk.txt
        junk2.txt

nothing added to commit but untracked files present (use "git add" to track)

So that's a lot of cruft just to show me that I have two untracked files in my repo. Sometimes I want to know the relationship between my local repo copy and a remote or there are just a lot of files -- some untracked, some ready for an add operation, some modified and some to be deleted or moved. I might need the full status then but more likely I just want a quick list of the files I might be concerned about without all the additional "helpful" verbiage. It's git s to the rescue!

$ git s
?? junk.txt
?? junk2.txt

This is really the same thing as git status -s. So lets create a globally available alias so I don't need to remember to type all of that:

git config --global --add alias.s 'status -s'

Git Log Without All the Verbiage (The lg Alias)

Just as git status leads to a lot of extra information that you don't normally need, so git log will do the same.

$ git log
commit 2cec3873f18e077b0a02145aebba476e55929463 (HEAD -> master, bb/master)
Date:   Tue Mar 20 15:38:28 2018 -0400

    Add DonorTrends Intial Donor Transaction File export script

commit d847f01ba371d35556335b22dfe305a9a3411468
Author: chrisbanm 
Date:   Tue Mar 20 14:53:56 2018 -0400

    Add standard exports gitignore

commit 4dd7bea06f283f4d8303dbdc22dc035c0e726462
Author: chrisbanm 
Date:   Tue Mar 20 14:51:52 2018 -0400

    Initial Commit (Empty)

Yeah, that's a lot of verbiage that I don't really need when I am doing a quick check of the logs and it doesn't contain some information I need like where my local repo is in comparison to my remote repo and it's not very pretty. So we can create a very pretty output by creating an alias for git log --oneline --decorate --all --graph. This will produce something like the following:

$ git log --oneline --decorate --all --graph
* 2cec387 (HEAD -> master, bb/master) Add DonorTrends Intial Donor Transaction File export script
* d847f01 Add standard exports gitignore
* 4dd7bea Initial Commit (Empty)

As you can see, this is a lot more helpful and takes up less screen real estate. So we create this alias thusly:

git config --global --add alias.lg 'log --oneline --decorate --all --graph'

Showing the Files Involved in a Commit (The lc Alias)

Once you've performed a commit you might want to see what files were involved in that commit at some later time. The standard way to do this is to issue the git show command. The only problem with this is that the show command by itsef also shows you all of the changes that were involved and that can become quite a long display, especially in a terminal:

$ git show 3ad527b
commit 3ad527b121c6535dafce3b6e1f1477892ef274bf
Author: chrisbanm <chrisb@advancingnativemissions.com>
Date:   Tue Apr 17 11:36:33 2018 -0400

    Update Active Church report for a run against March 2018

diff --git a/sql/ActiveChurchesMonthlyMar2018.sql b/sql/ActiveChurchesMonthlyMar2018.sql
index eb76b44..a0d587b 100644
--- a/sql/ActiveChurchesMonthlyMar2018.sql
+++ b/sql/ActiveChurchesMonthlyMar2018.sql
@@ -3,8 +3,8 @@

 -- We will run it this way, show everyone the list and then work from there to refine the formula
 -- Since this is a monthly report, we will set two variables to run the report (all others will be calculated). They are for the year and month.
-SET @year = '2017';
-SET @month = '01';
+SET @year = '2018';
+SET @month = '03';

 -- Set our Beginning and End date (inclusive) over which to look for our new contacts. These are caluculated for us.
 SET @month_begin_date = STR_TO_DATE(CONCAT(@year,'-',@month,'-01'), '%Y-%m-%d');

If there are many changes or new files, then this display can get unwieldy. If you just want to see some information about the commit itself and then a list of files that were involved in the commit then you would issue a show command with the --name-only option to show the commit header info (Commit Hash, Author, Date, and Message) and then a bare list of files involved in the commit:

$ git show --name-only 3ad527b
commit 3ad527b121c6535dafce3b6e1f1477892ef274bf
Author: chrisbanm <chrisb@advancingnativemissions.com>
Date:   Tue Apr 17 11:36:33 2018 -0400

    Update Active Church report for a run against March 2018

sql/ActiveChurchesMonthlyMar2018.sql

So that's a bit better as it shows some commit info and a list of files. So we create the lc alias thusly:

git config --global --add alias.lc 'show --name-only'

Showing ONLY the Files Involved in a Commit (The lco Alias)

If the lc alias above still shows more than you want to see (i.e. the commit info) and you JUST want to see a list of the files involved in a particular commit then the lco alias is the one you want. To review, the lc alias for git show --name-only will show both the commit info and list of files like this:

$ git show --name-only 3ad527b
commit 3ad527b121c6535dafce3b6e1f1477892ef274bf
Author: chrisbanm <chrisb@advancingnativemissions.com>
Date:   Tue Apr 17 11:36:33 2018 -0400

    Update Active Church report for a run against March 2018

sql/ActiveChurchesMonthlyMar2018.sql

If you add the --pretty="" option then the only thing you will get is a list of files that were involved in the commit:

$ git show --pretty="" --name-only 3ad527b
sql/ActiveChurchesMonthlyMar2018.sql

Now that's what I call compact! So we create that alias thusly:

git config --global --add alias.lco 'show --pretty="" --name-only'

Creating a New Repo with a Starting Empty Commit (The start Alias)

If you are not cloning code from somewhere else, you might consider doing more than just the standard git init. The reasons are various but starting with a first empty commit can make history rewrites using rebase much easier. Anyway, this has become my favorite way to start a git repo for projects I am creating from scratch. It will init a repo, and do an empty commit with the message 'Initial Commit (Empty)'. This means I can always go back to a fresh and new version of that repo when I want to throw everything away except for git goodies. This is what that would look like if I had to do it by hand:

$ git init
Initialized empty Git repository in C:/Projects/Data/Exports/junk/.git/

$ git commit --allow-empty -m 'Initial Commit (Empty)'
[master (root-commit) a27a24c] Initial Commit (Empty)

You can combine those into a single command as git init && git commit --allow-empty -m 'Initial Commit (Empty)' but that's an awful lot to remember and type when you are creating a new repo. Plus you might not remember exactly how you notated the first commit. For simplicity and standardization it's really helpful to put that all together in a simple alias which I call "start".

git config --global --add alias.start '!git init && git commit --allow-empty -m "Initial Commit (Empty)"'

Note : Git aliases only allow one command at a time so if you want to do something fancy then you can indicate you are running the string in a shell by prefixing the aliases string with a bang ('!') which tells git to run this string in the shell.

Adding All Untracked Files to a Git Repo (The add-untracked Alias)

You should probably use this one with care. But sometimes you know that you want to add all untracked files (as opposed to changed files, etc) that aren't being gitignored to a repo. So you can git s and any file or directory with "??' will show you the untracked files and you can pick and choose. But if that's not really what you need, then this sequence will get'em all and add them to staging for a future commit:

git ls-files -o --exclude-standard | xargs git add

The ls-files command is one of those git plumbing commands, the -o option says list "other" files (i.e. untracked), the --exclude-standard option makes sure that files which would be filtered by your .gitignore files won't show up and the | xargs git add pipes the output and calls git add for each of the files listed.

So yeah, you can do it that way but again, are you going to remember that obscure command? Do you want to type that instead of typing git add-untracked? Instead, lets put that in another handy alias:

git config --global --add alias.add-untracked '!git ls-files -o --exclude-standard | xargs git add'

And there you have another nifty quick command to do obscure things that require memory and typing without having to remember all that.

Remembering All of Your Nifty Aliases

So once you've created all these great aliases, how will you remember what does what? A great way is to simply list them:

$ git config -l | grep 'alias'
alias.s=status -s
alias.lg=log --oneline --decorate --all --graph
alias.lc=show --name-only
alias.lco=show --pretty= --name-only
alias.start=!git init && git commit --allow-empty -m "Initial Commit (Empty)"
alias.add-untracked=!git ls-files -o --exclude-standard | xargs git add

So there you have it. Great for a few quick shortcuts to save on brain space and keystrokes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment