Git How To

Git Stash – Save your changes to retrive later

If you have been using Git for some time now – you might have used “git stash”. Below are some useful tips on this command – I am sure you will find something new you will learn in this article and if you are new to git stash – you will learn some super cool tricks. Let’s jump on to the first command.

  • git stash

This command will stash – save all your uncommitted local changes in your local repo and your working directory will be clean, so you are ready to switch branches or take the latest. Note: when you use the git stash or git save (see latter) git actually creates a git commit object in your repo and saves it with some random name (more on the name in the git list command)

example.

 

Note: In the example above, I used the command Git status – this command is used to check the status on the current branch, it will display the current state of the working directory.

  • git stash save  “your message”

This command is similar to git stash – with git stash save – you can provide some message for the stash you are creating, rather than allowing git to come up with a random name. When you list the stash – you will see this message in the stash list.

example

 

git stash save has one more option

  • git stash save – u  or git stash save –include-untracked

the -u  / –include-untracked option lets you stash untracked (newly added files to the repository).

example

 

  • git stash list

This command will list all the stashes you made, the most recent stash is displayed on the top of the list – Every time you create a stash using the above commands – the stash is added to the top of the stash list with a name.

example

 

  • git stash pop [stash id]

This command will apply the stash to the current repo and delete the stash from the stash list, by default stash@{0} will be applied, unless stash id is specified.

example

  • git stash apply [stash id]

This command is similar to the git pop command, and will apply the stash to the current repo, but will not delete the stash from the stash list, (you can still see the stash using stash list) by default stash@{0} will be applied, unless  a stash id is specified

example

  • git stash drop [stash id]

This command will delete the latest stash from the stack, by default stash@{0} will be applied, unless stash id is specified.

example

  • git stash clear

This command deletes  all the stashes made in the repo.

example

 

-Hussain Patel