Git - Stashes

May 27, 2023

In my last post, we covered the basics of Git branches and merges, so that you can work on a new experiment branch without losing progress on the working code in your master or main branch, which a lot of times mirrors the code that is released to production. Now we'll look at stashes.

Stashes

 

When you're coding on work in progress (WIP), but have code that isn't ready to be committed or pushed up to the remote project in Github, stashes are a way to temporarily save your work to be re-applied at a later time.

 

Branch and Stash Example

 

We're going to build upon the skills we learned in our previous posts in this example, which were Git - The Basics and Git - Branches and Merge.

 

First we're going to add a new file, index.js, to our myproject folder that we've been working on by using the "touch" command.

 

Now that we've created index.js, we're going to use the "sudo nano index.js" command to open the index.js file into the nano command line text editor. The sudo command elevates our privileges so we can open the file in the nano text editor with write permissions. It prompts me to enter my password.

 

 

Now we'll paste in some boilerplate HTML code into the file. This code is trivial and can be any code you like for the example. We save and write the changes to the file with Control+O (write/save) and then command Control+X (exit).

 

Now we'll add the file to track the changes on Git and commit the change with the message "Add index.js".

 

Add Merge to the Example

 

Now we've added index.js to Git tracking and have committed the file, but now we want to work on something else within a new branch we'll call "feature". We can switch to and create a new branch using the following code.

 

 

We can use the "ls" command to list the files in the myproject directory, and we can see that index.js lives in the feature branch as well now. Let's edit the index.js file using the "sudo nano" command we used earlier and modify a part of the code. Here I've added a console.log line to the HTML script tags, to log "Hello,World!" to the console when the webpage is launched.

 

 

Now we'll add the modified index.js file to Git to track, with the new code in the feature branch.

But let's say there is a new update that breaks the production code that's been pushed to production from the master branch of the project, and we need to switch to that branch to do some quick work.  But we don't want to lose our progress with the work in progress (WIP) in the feature branch on the index.js file, but we're also not at a clean point where we can push to that modified file to the remote.

So we stash the change in the feature branch and switch back to the master branch.

 

We'll assume now that the work has been completed in the master branch. Then we can switch back to the feature branch and list the stash or stashes we have in this branch by using "git stash list". Then we'll apply the stash at reference point 0, which is the most recent stash, back to the feature branch, add it, and commit it.

 

 

We'll now checkout the master branch and merge the feature branch into the master branch. And lastly push to the remote using "git push origin master".

In Github, we can now see that the index.js file now has the new console.log("Hello,World!") code.

 

 

Congrats, we've successful learned how to stash work in progress (WIP), switch to another branch on the fly, then come back to that stashed work later and re-apply it, then push it to the remote.

Grab the Code

https://github.com/CodingInGreen/git-stashes