Book a Consultation

A Quick Comment on Git Stash

TLDR:

You can specify a custom message when stashing changes in Git.


$ git stash save trying out a new pagination gem
$ git stash list
stash@{0}: On test_branch: trying out a new pagination gem
stash@{1}: WIP on test_branch: 2401cf1 Update company_intro_to_qa.md
stash@{2}: WIP on test_branch: 2401cf1 Update company_intro_to_qa.md
stash@{3}: WIP on master: 0e1e289 Update company_intro_to_qa.md

 


 

Hello. The default git stash information is a bit unwieldy. If you use stash regularly and create multiple stashes, viewing them with git stash list can end up being rather unuseful.


git stash list


stash@{0}: WIP on test_branch: 2401cf1 Update company_intro_to_qa.md 
stash@{1}: WIP on test_branch: 2401cf1 Update company_intro_to_qa.md

This is the default information provided about a stash:

  • Its stash reference number

  • That it’s a Work in Progress

  • Which branch it’s against

  • The most recent commit it’s working against

As a prolific stash-er, I would regularly have work stashed against different branches (easy to find in the stash list!) but if I had multiple stashes against a single branch, or worse, against a single commit, there was no way to tell which was which.


stash@{0}: WIP on test_branch: 2401cf1 Update company_intro_to_qa.md 
stash@{1}: WIP on test_branch: 2401cf1 Update company_intro_to_qa.md 
stash@{2}: WIP on master: 0e1e289 Update company_intro_to_qa.md

But surely, they’re in reverse chronological order?

This is only true until I git stash pop stash@{1} (or worse, git stash apply stash@{1}) in which case, I'd end up with this:


stash@{0}: WIP on test_branch: 2401cf1 Update company_intro_to_qa.md 
stash@{1}: WIP on test_branch: 2401cf1 Update company_intro_to_qa.md 
stash@{2}: WIP on test_branch: 2401cf1 Update company_intro_to_qa.md 
stash@{3}: WIP on master: 0e1e289 Update company_intro_to_qa.md

stash@{0} and stash@{2} are actually identical, while stash@{1} is different, but I have no way to know that.

I COULD go through and view the diff's of each stash, but it'd be much easier if I could tell at a glance which stash was which.

Enter git stash save add your message here


git stash save trying out a new pagination gem


stash@{0}: On test_branch: trying out a new pagination gem 
stash@{1}: WIP on test_branch: 2401cf1 Update company_intro_to_qa.md 
stash@{2}: WIP on test_branch: 2401cf1 Update company_intro_to_qa.md 
stash@{3}: WIP on master: 0e1e289 Update company_intro_to_qa.md

Much better. The WIP is removed as it's unnecessary but the branch I was working on remains. I don't need to know which commit it was against and instead have my custom message, making scanning through git stash list much easier.

 


 

Previously from our Engineering Team:

Avoiding N+1 queries in Rails GraphQL APIs by Andy West