Useful git commands to keep in your toolbelt - Part 1

Often when working on projects, it’s always a good idea to have feature branches for each repository. As priorities change while working and bugs are reported, there is a possibility that you might have to work on something while some work was already in progress. I want to share one of the useful git commands today that I use to keep my workflow, testing, and code management clean.

Stashing Changes

As per the official Atlassian documentation,

“git stash temporarily shelves (or stashes) changes you've made to your working copy so you can work on something else, and then come back and re-apply them later on. Stashing is handy if you need to quickly switch context and work on something else, but you're mid-way through a code change and aren't quite ready to commit.”

I use git stash when I am mid-way through a feature branch already on some repository and I have to quickly switch to another branch and work on something else. Doing a git stash before switching the branch makes sure that I don’t run into any conflicts where the same source file is modified both in my working copy and from the remote. This also ensures that I can come back and reapply these changes, and start where I previously left off when I eventually do come back to the original feature branch. A note worth mentioning here – Stashes are local to your git repository and are not pushed to the remote server.

By default, running git stash does not stash changes made to untracked and ignored files.

  • Running git stash -u (or --include-untracked) instructs git to stash changes made to untracked files as well.

  • Running git stash -a (or --all) instructs git to stash changes made to the ignored files (files added to your .gitignore).

Re-applying changes

Once I am done working on the new branch and have pushed the changes, I can reapply changes from the stash by using the git stash pop command. This pops the changes from the temporary stash and applies them to your current working directory.

Partial Stashes

It may be possible that you are modifying a source file and some local changes may be required even after you switch to another branch and work on something else. Git, thus, provides us with the concept of Partial Stashes.

Via partial stashes, git allows us to stash individual changes from within a single file. This way you can choose to stash individual changes from a file except for the ones that you want to carry over to the other feature branch. This can be done by using the git stash -p (or --patch) command. Git will iterate over each changed hunk (changes in files are broken down into smaller individual blocks that git calls hunks) in your working copy and ask whether you want to stash that particular hunk. For each hunk, you can choose one of the most used options or browse through the complete list of options by hitting the ‘?’ option for help.

Stash Clean-Up

If you decide, after coming back to your original feature branch that you no longer need the stash (i.e. a case where a particular approach was not the most efficient or a boundary system changed its data representation or a particular layout for one of the UI pages was changed), you can also clean up your stash and start on the original branch with a clean slate. When you come back to your original feature branch, the changes you stashed earlier are not applied to your working copy unless you do a git stash pop. You can simply do a git stash drop to clear all your stashed changes and start anew.


For more information on how git stash works, you can check out this wonderful illustration.

Did you find this article valuable?

Support Jimil Shah by becoming a sponsor. Any amount is appreciated!