Datascience in Towards Data Science on Medium,

Meet Git Stash: Your Secret Chest of Unfinished Code

10/26/2024 Jesus Santana

Photo by Stefan Steinbauer on Unsplash

Mastering Git

A powerful Git feature for temporarily saving code in progress

Imagine discovering a critical bug that needs an immediate fix while you are working halfway through a code change. Your attempt to switch branch fails because of uncommitted changes in your current feature branch. These changes aren’t ready for a commit but are too valuable to discard. In such a situation where you need to switch contexts quickly, git stash offers an elegant solution to temporarily store your unfinished code safely without committing. In this post, we will explore how to use git stash effectively.

📍 1. Stashing

Imagine we are writing a letter with a pen and a paper, but suddenly we had to write another more urgent letter and send it away. Our desk can hold only one letter. It would be too wasteful to throw our unfinished letter since it took us some time to write what’s written so far. Instead of throwing it away, we can put it away in a secure chest so that we can pick it up and continue once we finish this more time-sensitive letter. This will allow us to get straight on to writing this more urgent letter and send it quickly while saving our work on the other letter. In this analogy, halfway written letter is the uncommitted changes. Stashing is like putting our unfinished code into a secure chest.

The vanilla git stash command will stash away uncommitted (i.e. staged and unstaged) changes in the tracked files:

git stash

We can give stash a description to make it more informative for future reference:

git stash push -m "<stash-description>" # Recommended method 
git stash save "<stash-description>" # Legacy method

With this command, untracked files will remain in your working directory. To stash away untracked files in addition to the modified tracked files, we can use -u:

git stash push -u -m "<stash-description>"

If we want to also stash away ignored files in addition to the modified tracked files and untracked files, we can use -a:

git stash push -a -m "<stash-description>"

In some cases, it’s more useful to be more selective with our stash. Here’s how we can stash specific files:

git stash push -m "<stash-description>" -- <file1-path> <file2-path>

Instead of file paths, we can also specify directory paths.

📍 2. Viewing list of stashes

Continuing on our letter analogy, viewing list of stashes is like opening our secure chest and seeing what half-written letters are inside.

To view existing stashes, use:

git stash list

This command shows a list of all stashes with their index and description. The most recent stash will be on top and named as stash@{0}. Output looks something like this:

stash@{0}: WIP on main: 1234567 Commit message
stash@{1}: WIP on feature-branch: 89abcdef Another commit message
💡 It is good practice to keep only a few stashes to avoid confusion and getting mixed up with too many stashes.

In some situations, it can be useful to see summary of stashes beyond what git stash list shows. We can see a brief overview of the modifications made to files that were stashed away with the following command:

git stash show # Shows most recent stash
git stash show stash@{index} # Shows a stash we specified

By adding -p, we can see the full diff:

git stash show -p stash@{index}

📍 3. Applying a stash

Continuing on our letter analogy, applying a stash is like magically creating a copy of a selected unfinished letter from the secure chest on to our desk. This way we have the unfinished letter on the desk as well as in the secure chest.

To apply back a stashed change to your working directory, use:

git stash apply # Applies most recent stash
git stash apply stash@{<index>} # Applies a stash we specified

Applying back a stash doesn’t delete the stash from the list of stashes.

📍4. Dropping a stash

Continuing on our letter analogy, dropping a stash is like taking out an unfinished letter from the secure chest and throwing it away. Clearing a stash is like clearing the secure chest and throwing everything inside it away.

If we no longer need our stash, we can remove a stash from our list:

git stash drop # Drops most recent stash
git stash drop stash@{<index>} # Drops a stash we specified

To remove all stashes, we can use:

git stash clear

This will empty our stash.

📍5. Popping a stash

Continuing on our letter analogy, popping a stash is like taking out an unfinished letter from the secure chest and put it on our desk. The unfinished letter will no longer exist in the secure chest.

To apply a stash and drop it from the list in one step, we can use pop:

git stash pop # Pops most recent stash
git stash pop stash@{<index>} # Pops a stash we specified

git stash pop is a one-step process for reapplying changes and cleaning up from the stash. It is equivalent to:

git stash apply
git stash drop

📍6. Creating a new branch from a stash

Continuing on our letter analogy, creating a new branch from a stash is like taking out a unfinished letter from the secure chest and put it on the desk but this time the letter will be inside a different envelope. In this analogy, an envelop represents the branch.

Sometimes, it’s useful to create a new branch, switch to it and apply the stash. To create a new branch from a stash, we use:

# Create a new branch from the most recent stash
git stash branch <branch-name>
# Create a new branch from a stash we specified
git stash branch <branch-name> stash@{<index>}

This will drop the stash as well.

📍7. Putting it all together

Now, let’s look at an example how we might use some of the commands we learned in this post in a real-life scenario. Imagine we are working on a productionised repository. We want to improve a feature so we created feature branch off main branch this morning and currently working on this branch. We have edited file1.py, file2.py and file3.py but my changes are not ready for commit as they are not complete and have not been tested yet. We just found out that we need to do a very simple hot fix and deploy it as soon as possible.

git stash # Stash away my uncommitted changes in the three files
git checkout main # Change to main branch
git pull # Ensure we pull most recent changes in main branch just in case
git checkout -b hot_fix # Create a new branch for the hot fix

Now, we can make changes for the hot fix, commit it and push it to remote.
Typically, we will do pull request to get our code change reviewed and merge our code change into develop, sit, uat and finally to main branches. Once we finish our hot fix code on the hot_fix branch, we can continue our work on feature branch:

git switch feature # Switch back to our feature branch
git stash pop

We simply used git stash pop to pop back the most recent stash as we stashed just a few hours ago and haven’t stashed anything since. Now we can finish off our work on file1.py, file2.py and file3.py and commit it when we are ready.

In this example, used git stash to stash away and git stash pop to bring it back. Sometimes you might need to do a bit more before popping your stash if you have multiple stashes and/or it’s been a while since you stashed. In this case, some of the other commands can be helpful.

Voila, that’s it! git stash is a powerful and versatile command that allows you to switch priorities smoothly and multitask without losing any of your hard work. The next time you’re in the middle of coding and need to switch contexts, git stash has got you covered!

Thank you for reading my post. If you are interested, here are links to some of my posts you might also like:
◼️ Introduction to Mermaid graphs in Markdown
◼️ Introduction to Git for Data Science
◼️ Enrich your GitHub profile with these tips
◼️ Create Your Own Stunning Website in Minutes for Free
◼️ Level up your Git knowledge with these 10 useful tips to browse git history

Bye for now 🏃💨


Meet Git Stash: Your Secret Chest of Unfinished Code was originally published in Towards Data Science on Medium, where people are continuing the conversation by highlighting and responding to this story.



from Datascience in Towards Data Science on Medium https://ift.tt/LfbZKkp
via IFTTT

También Podría Gustarte