6 Git Aliases to Make Stashing Easier

Post by on April 1, 2013

Stashing in Git is quick and easy, but the syntax is not. To close the gap I have made 6 aliases that make working with stashes significantly easier.

Installation

There are two ways you can "install" these aliases:

Option 1 - Edit your ~/.gitconfig file

sl = stash list
sll = stash-list
slll = stash-list --full
ss = stash save
sa = "!sh -c 'git stash apply stash@{$1}' -"
sd = "!sh -c 'git stash drop stash@{$1}' -"

 Option 2 - Run the following git-config commands

If you're not as comfortable editing the file, you can simply run the following commands:

git config --global alias.sl "stash list"
git config --global alias.sll "stash-list"
git config --global alias.slll "stash-list --full"
git config --global alias.ss "stash save"
git config --global alias.sa '!sh -c "git stash apply stash@{$1}" -'
git config --global alias.sd '!sh -c "git stash drop stash@{$1}" -'

Usage

1. git sl

This alias simply lists your stashes:

$ git sl
stash@{0}: On master: add-notice-to-bash-profile
stash@{1}: On master: update-vimrc-modified-date

2. git sll

This uses the "stash-list" (also on GitHub) Git utility that I wrote and shows the stat of the stash diffs:

$ git sll
stash@{0}
 .bash_profile | 2 ++
 1 file changed, 2 insertions(+)
stash@{1}
 .vimrc | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

3. git slll

This also uses the "stash-list" utility, but gives you the full diff of each stash:

$ git slll
commit 1e56708614c39941fdd91101eb909328d689c38c
Merge: 3395cb2 6a02f94
Author: Joseph D. Purcell <josephdpurcell@gmail.com>
Date:   Mon Apr 1 10:00:22 2013 -0400

    On master: add-notice-to-bash-profile

diff --cc .bash_profile
index 132dfa1,132dfa1..df93831
--- a/.bash_profile
+++ b/.bash_profile
@@@ -1,2 -1,2 +1,4 @@@
  #!/bin/bash
++# NOTE: This file should contain nothing. Put all bash scripts and alises in
++# your ~/.bashrc.
  source ~/.bashrc

commit bd46292065430dbe80d753a3a7d1b41860e0999b
Merge: 3395cb2 85cdc45
Author: Joseph D. Purcell <josephdpurcell@gmail.com>
Date:   Mon Apr 1 09:58:56 2013 -0400

    On master: update-vimrc-modified-date

diff --cc .vimrc
index 937dedd,937dedd..de67801
--- a/.vimrc
+++ b/.vimrc
@@@ -2,7 -2,7 +2,7 @@@
  "
  " @author Joseph D. Purcell <josephdpurcell@gmail.com>
  " @created 1/1/1970
--" @modified 3/14/2013
++" @modified 4/1/2013

  " =========================================================
  " PLUGINS:

4. git ss

This one ties for my favorite with the "sa" and "sd" aliases. This will force you to save the stash with a name. I am always forgetting why I saved a stash, and even though "sll" and "slll" are useful, they can be painful if you have 20+ stashes. This is perhaps the most time saving in the long term. Here is how this works:

$ git ss
You must specify a stash name.

It errors. Then, if you specify a name it works:

$ git ss a-very-descriptive-description-of-my-stash-with-a-task-number-523
Saved working directory and index state On master: a-very-descriptive-description-of-my-stash-with-a-task-number-523
HEAD is now at 3395cb2 Add Vim plugins and fix some aliases.

5. git sa

This one combined with "sd" are perhaps the most time saving in the short term. This will apply a stash for you:

$ git sa
# On branch master
# Changes not staged for commit:
#   (use "git add ..." to update what will be committed)
#   (use "git checkout -- ..." to discard changes in working directory)
#
#	modified:   .bash_profile
#
no changes added to commit (use "git add" and/or "git commit -a")

That applied the stash on top. Now, to apply a previous stash, you can specify the number as so:

$ git sa 1
# On branch master
# Changes not staged for commit:
#   (use "git add ..." to update what will be committed)
#   (use "git checkout -- ..." to discard changes in working directory)
#
#	modified:   .vimrc
#
no changes added to commit (use "git add" and/or "git commit -a")

6. git sd

As mentioned, this one pairs very well with "sa". This will drop a stash for you. If you don't specify a number it will delete the stash on top:

$ git sd
Dropped stash@{} (1e56708614c39941fdd91101eb909328d689c38c)

It is worth noting that drop-ing a stash is not simply deleting the stash. It apply the stash and then deletes the stash if the stash application went smoothly, i.e. if there is a conflict it will not delete the stash.

Now, if you specify a number it will delete that index (remember stashes are zero-indexed, so to drop stash #2 you would give index #1):

$ git sd 1
Dropped stash@{1} (bd46292065430dbe80d753a3a7d1b41860e0999b)
Older Posts »