Techno Blender
Digitally Yours.

Mastering Git: “git stash”. How to use git stash to store changes… | by Philip Wilkinson | Sep, 2022

0 64


How to use git stash to store changes you aren’t ready to commit

Photo by Praveen Thirumurugan on Unsplash

Git is a tool that all Data Scientists and Software Engineers should know how to use. Whether you work on a project alone, or work as part of a large distributed team, understanding how to use Git can save you a lot of time in the long run. While git is useful for clearly seeing a history of changes that you’ve made to your repository, in some cases you aren’t ready to commit changes you’ve made to the history. This can be a bit of a challenge when git is telling you you need to commit or you will lose your changes. Git often does this when you want to change branches or pull changes from the remote repository. So what can you do? git stash to the rescue! The stash command allows you to “stash” changes that you aren’t ready to commit yet, so that you can save the changes without having to add them to the history. This will allow you to easily change branches or pull from the remote repository without having to worry about losing your changes. So how does it work?

Git Stash

The normal workflow of git would be editing files, staging the changes and then making commits. This workflow would create a nice clean commit history where work clearly builds on each other. It allows you to get into the flow of the feature you are developing, make clear changes and make your usual amazing code contributions in one coding session. Of course, this is the ideal workflow but in the reality of developing in a working environment as part of a team means that you often have to switch tasks or responsibilities throughout the day. This can be frustrating when you are halfway through a code change such that you aren’t yet ready to commit but at the same time you don’t want to lose your work. You may be asked to change what you are working on because a bug has appeared that needs to be resolved as soon as possible, another developer has asked you to take a look at another branch, new commits have been made to the branch you are working on pr you just want a break. At this point, before you can do anything else, git may be telling you to do something about the changes you’ve made.

If you don’t want to create a new commit as of yet (although you can always go back and edit the commit or the message), you can use git stash to save the uncommited changes. This process allows you to work on other tasks in the repository without losing the changes you’ve made so far. Then, when you want to come back to those changes, you can simple reapply those changes without affecting the commit history. Awesome! How does it work?

Working with Git Stash

By default, when you call git stash all the changes that you have made to tracked files will be moved to a stash. At this point, your current working directory will prevent back to the stage it was after the last commit. This means that all changes you have made after then will be stored in a stash that you will be able to access later.

The stash you’ve made will then contain:

  • Changes that have been made to staged files
  • Changes that have been made to tracked files (but that are unstaged)

But you need to be aware that it will not automatically contain:

  • New files in your working directory that have not been staged yet
  • Files that are purposefully ignored

This is something that you need to be aware of, especially if you working on a change that involves a new file. In order to include this, you can use the flags -u , which will include untracked files, and -a that will also include ignored filed.

Once you have created the stash, done the changes that you needed to do elsewhere and then come back to the original branch you were working on, you will be wondering how can you get those stashed changed back? Well, there are two mains this can be done:

  • git stash apply — will take the changes that you have stored in a stash, apply them to the working directory of the currently checked-out branch, and will keep the stash intact. This is useful when you are pulling the changes into a different branch than originally developed or you are editing the changes but you may want to keep the original changes just in case. Importantly, the stash will still be there in the future until it is cleaned up, meaning that if something goes wrong you can convert back to the original stash.
  • git stash pop — Will also apply the changes stored in the stash to the current working directory, but this will delete the stash after the changes have been applied. This can be used when you don’t care about keeping the stash for later or if you are planning on making changes to the stash and won’t want to revert to the original stash at any point.

This is incredibly useful especially for storing changes that you aren’t ready to share with the rest of the team.

It must be noted however that this is not a substitute for commit your changes to the repository. Stashing should only be performed in moderation. This is because it can become hard to keep track of changes, especially where there are multiple stashes, because their history and what they contain is not as clear as commits. This means that they should be used sparingly and only when needed, otherwise the changes should be in a commit!

Additional functionality

Beyond being used relatively simply as above, git stash is a very powerful tool that can be modified in a variety of ways.

  • git stash --patch (or using the -p flag) will open up an editor that will allow you to interactively select which changes you would like to stash and which you want to keep in your working directory. This is useful when you don’t want to stash all your changes because you may want to commit some of your changes.
  • git stash save "message" will allow you to add a message to your stashes. This is especially useful when you have multiple stashes and want to clearly tell what the changes contain or why they have been stashed. This is good practice if you use stash often and it will make your life much easier when you go back to applying the stashed changes.
  • git stash show will show you the contents of your most recent stash (the number of files change, number of additions and the number of removals). This is useful to roughly see what the latest stash contains, but if you want to see all the changes in the files you can pass -p which will essentially show the same output as git diff
  • If you’ve made more than one stash git stash list will show a list of all the stashes that have been made. This will show which branch the stash comes from and their stash index (how you can select between stashes). By default, all stashes are identified as “WIP” (work in progress) on top of the branch and the commit that you created the stash from.
  • git stash show stash@<index> can be used to show the changes from that specific stash, for example git stash show stash@{2} will show the changes in the third stash. This can extend to the previous commands such as git stash pop stash@{2} which will pop the third stash, as opposed to the default which will be the last stashed changed.
  • git stash drop can be used to remove a single stash from the list of stashed entries. This is useful when you know you will not be using those changes at any point in the future and keeps your git repository clean of any unused changes. git stash clear extends this to dropping all stashed changes, but use this sparingly as it can be difficult to recover any stashes after this!
  • Finally git stash branch <branch_name> can be used to create a branch from the latest stash and deletes the latest stash. This can be useful when conflicts emerge after you have applied the stash to the latest version of your branch (this can happen and can be very messy!)

Conclusions

If git is warning you that you can’t do something because you have uncommited changes git stash can come to your rescue! It works by storing changes to files that you aren’t ready to create a commit, ensuring that work is not lost when you change branches, make a pull request or only want to commit part of the work you’ve already done. This can come in useful from time to time, and multiple stashes can be stored, but make sure you don’t overuse! Unlike commits, stashes can be difficult to keep track of and they are document as comprehensively, so when you are ready to publish your change, make a commit!

Sources

[1] https://www.atlassian.com/git/tutorials/saving-changes/git-stash

[2] https://git-scm.com/docs/git-stash


How to use git stash to store changes you aren’t ready to commit

Photo by Praveen Thirumurugan on Unsplash

Git is a tool that all Data Scientists and Software Engineers should know how to use. Whether you work on a project alone, or work as part of a large distributed team, understanding how to use Git can save you a lot of time in the long run. While git is useful for clearly seeing a history of changes that you’ve made to your repository, in some cases you aren’t ready to commit changes you’ve made to the history. This can be a bit of a challenge when git is telling you you need to commit or you will lose your changes. Git often does this when you want to change branches or pull changes from the remote repository. So what can you do? git stash to the rescue! The stash command allows you to “stash” changes that you aren’t ready to commit yet, so that you can save the changes without having to add them to the history. This will allow you to easily change branches or pull from the remote repository without having to worry about losing your changes. So how does it work?

Git Stash

The normal workflow of git would be editing files, staging the changes and then making commits. This workflow would create a nice clean commit history where work clearly builds on each other. It allows you to get into the flow of the feature you are developing, make clear changes and make your usual amazing code contributions in one coding session. Of course, this is the ideal workflow but in the reality of developing in a working environment as part of a team means that you often have to switch tasks or responsibilities throughout the day. This can be frustrating when you are halfway through a code change such that you aren’t yet ready to commit but at the same time you don’t want to lose your work. You may be asked to change what you are working on because a bug has appeared that needs to be resolved as soon as possible, another developer has asked you to take a look at another branch, new commits have been made to the branch you are working on pr you just want a break. At this point, before you can do anything else, git may be telling you to do something about the changes you’ve made.

If you don’t want to create a new commit as of yet (although you can always go back and edit the commit or the message), you can use git stash to save the uncommited changes. This process allows you to work on other tasks in the repository without losing the changes you’ve made so far. Then, when you want to come back to those changes, you can simple reapply those changes without affecting the commit history. Awesome! How does it work?

Working with Git Stash

By default, when you call git stash all the changes that you have made to tracked files will be moved to a stash. At this point, your current working directory will prevent back to the stage it was after the last commit. This means that all changes you have made after then will be stored in a stash that you will be able to access later.

The stash you’ve made will then contain:

  • Changes that have been made to staged files
  • Changes that have been made to tracked files (but that are unstaged)

But you need to be aware that it will not automatically contain:

  • New files in your working directory that have not been staged yet
  • Files that are purposefully ignored

This is something that you need to be aware of, especially if you working on a change that involves a new file. In order to include this, you can use the flags -u , which will include untracked files, and -a that will also include ignored filed.

Once you have created the stash, done the changes that you needed to do elsewhere and then come back to the original branch you were working on, you will be wondering how can you get those stashed changed back? Well, there are two mains this can be done:

  • git stash apply — will take the changes that you have stored in a stash, apply them to the working directory of the currently checked-out branch, and will keep the stash intact. This is useful when you are pulling the changes into a different branch than originally developed or you are editing the changes but you may want to keep the original changes just in case. Importantly, the stash will still be there in the future until it is cleaned up, meaning that if something goes wrong you can convert back to the original stash.
  • git stash pop — Will also apply the changes stored in the stash to the current working directory, but this will delete the stash after the changes have been applied. This can be used when you don’t care about keeping the stash for later or if you are planning on making changes to the stash and won’t want to revert to the original stash at any point.

This is incredibly useful especially for storing changes that you aren’t ready to share with the rest of the team.

It must be noted however that this is not a substitute for commit your changes to the repository. Stashing should only be performed in moderation. This is because it can become hard to keep track of changes, especially where there are multiple stashes, because their history and what they contain is not as clear as commits. This means that they should be used sparingly and only when needed, otherwise the changes should be in a commit!

Additional functionality

Beyond being used relatively simply as above, git stash is a very powerful tool that can be modified in a variety of ways.

  • git stash --patch (or using the -p flag) will open up an editor that will allow you to interactively select which changes you would like to stash and which you want to keep in your working directory. This is useful when you don’t want to stash all your changes because you may want to commit some of your changes.
  • git stash save "message" will allow you to add a message to your stashes. This is especially useful when you have multiple stashes and want to clearly tell what the changes contain or why they have been stashed. This is good practice if you use stash often and it will make your life much easier when you go back to applying the stashed changes.
  • git stash show will show you the contents of your most recent stash (the number of files change, number of additions and the number of removals). This is useful to roughly see what the latest stash contains, but if you want to see all the changes in the files you can pass -p which will essentially show the same output as git diff
  • If you’ve made more than one stash git stash list will show a list of all the stashes that have been made. This will show which branch the stash comes from and their stash index (how you can select between stashes). By default, all stashes are identified as “WIP” (work in progress) on top of the branch and the commit that you created the stash from.
  • git stash show stash@<index> can be used to show the changes from that specific stash, for example git stash show stash@{2} will show the changes in the third stash. This can extend to the previous commands such as git stash pop stash@{2} which will pop the third stash, as opposed to the default which will be the last stashed changed.
  • git stash drop can be used to remove a single stash from the list of stashed entries. This is useful when you know you will not be using those changes at any point in the future and keeps your git repository clean of any unused changes. git stash clear extends this to dropping all stashed changes, but use this sparingly as it can be difficult to recover any stashes after this!
  • Finally git stash branch <branch_name> can be used to create a branch from the latest stash and deletes the latest stash. This can be useful when conflicts emerge after you have applied the stash to the latest version of your branch (this can happen and can be very messy!)

Conclusions

If git is warning you that you can’t do something because you have uncommited changes git stash can come to your rescue! It works by storing changes to files that you aren’t ready to create a commit, ensuring that work is not lost when you change branches, make a pull request or only want to commit part of the work you’ve already done. This can come in useful from time to time, and multiple stashes can be stored, but make sure you don’t overuse! Unlike commits, stashes can be difficult to keep track of and they are document as comprehensively, so when you are ready to publish your change, make a commit!

Sources

[1] https://www.atlassian.com/git/tutorials/saving-changes/git-stash

[2] https://git-scm.com/docs/git-stash

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.

Leave a comment