Save Work in Progress in a Git Stash

Git allows us to temporarily save the changes we are working on in a branch without needing to include them in the staging area, and we can do this using the git stash command, which is equivalent to git stash push.

Saving Changes in a Stash

To save changes in a stash, use:

				
					git stash

				
			

A stash is similar to a draft; it’s a quick or temporary way to preserve changes in a local space that we can continue working on later.

We can have multiple drafts of our changes, which we can list using the command:

				
					git stash list

				
			

We can also save a stash with a descriptive message to differentiate it in case we have several items in the stash list:

				
					git stash save "descriptive message"
				
			

Recovering Changes from a Stash

To recover the last changes from the stash, we can use:

				
					git stash pop

				
			

Or:

				
					git stash apply
				
			

Both commands allow us to recover changes from a specific stash:

				
					git stash pop stash@{<stash_index>}
git stash apply stash@{<stash_index>}
				
			

Showing Differences in a Stash

We can see the differences of a stash briefly with:

				
					git stash show
				
			

Or use the -p (or --patch) option to see all differences in a stash:

				
					git stash show -p
				
			

When recovering changes from a stash, it does not automatically delete the stash, so it is advisable to do it manually.

Deleting Changes

To delete recent changes, use:

				
					git stash drop
				
			

We can also specify the index of a specific stash:

				
					git stash drop stash@{<stash_index>}

				
			

To delete all changes, use:

				
					git stash clear
				
			
Facebook
Twitter
LinkedIn