How do you set code aside with Git?

NicolasBrondinBernard

Author
@NicolasBrondinBernard

Need to save some code you're working on for later? Here's the magic command!

Article published on 19/10/2021, last updated on 10/08/2026

Switching from one Git branch to another when the work is done, that's easy. But then how do you save work in progress, without making a "junk" commit or risking losing code already written?

Two solutions

Solution 1: git stash (recommended)

The "stash" is a list of modifications saved outside the classic workflow, and can be restored later, like this:

##unsaved code
git stash ##code set aside
git stash pop ##code restored
git commit ...
##saved code

The pop command restores the last item saved in the stash, but you can list all the items in the stash with the command below:

git stash list ##List of "archives" present in the stash

There are many "sub-commands" for git stash, I invite you to check out the official documentation to discover them.

Solution 2: git commit --amend

I introduced you to the --amend option of git commit so you could rename your last commits in a previous article, but this option is actually much more powerful!

The --amend option actually lets you completely modify your last commit, which means you can rename it, but also change its content. As you've probably guessed, as long as you don't push it to a remote repository, your last commit can serve as an "archive" for your code:

##unsaved code
git commit -am "[WIP] - signup feature not working" ##code saved locally
## ... work on another branch then come back ...
git commit --amend -am "[FEAT] - signup feature working" ##commit updated
git push origin ...
##code sent

Unlike git stash, your work in progress remains tied to your current branch (the stash is global to the repository), and you can even add a message to indicate the nature of the work in progress (your temporary commit message).

But be careful, the risk is sending incomplete code to the remote repository in case of a mistake!

Conclusion

The git stash and git stash pop commands were created precisely for this purpose, to set aside code that's being written in order to reapply it later!


Steve Johnson sur Unsplash

Finished reading this article?
Our newsletter

No spam. Only free content, news, and ever more resources to level up your skills!

Join +1500 developers

Comments (0)

to leave a comment

No comments yet