Git | Working with Stash - GeeksforGeeks

27 Feb, 2020
Git allows multiple users to work on the same project simultaneously. Suppose if a developer is working on a feature in a branch and he needs to pull changes from some other developer’s branch or if he has to work urgently on some other feature, but the feature he is currently working on is incomplete. In this case, you cannot commit the partial code of the currently working feature. To add this new feature, you have to remove your current changes and store them somewhere else. For this type of situation, Git offers a very useful command known as ‘git stash‘.git stash command saves the previously written code and then goes back to the last commit for a fresh start. Now you can add the new feature without disturbing the old one as it is saved locally. After committing the new feature you can go on with working on the old feature which was incomplete and uncommitted.
To save the uncommitted changes for later usage, you can use the ‘git stash‘ command. This command saves your local modifications away and reverts the working directory to match the HEAD commit, so it will give you clean working directory.
git stash
By default, running git stash will stash the changes that have been added to your index (staged changes) and changes made to files that are currently tracked by Git (unstaged changes). To stash your untracked files, use git stash -u.
You can create multiple stashes and view them using the ‘git stash list‘ command. Each stash entry is listed with its name (e.g. stash@{1}), the name of the branch that was current when the entry was made, and a short description of the commit the entry was based on.
git stash list
To provide more context to the stash we create the stash using the following command:
git stash save "message"
You can reapply the previously stashed changes with the ‘git stash pop‘ or ‘git stash apply‘ command. The only difference between both the commands is that ‘git stash pop‘ removes the changes from the stash and reapplies the changes in the working copy while ‘git stash apply‘ only reapplies the changes in the working copy without removing the changes from the stash. In simple words, “pop” removes the state from the stash list while “apply” does not remove the state from the stash list.
git stash pop
git stash apply
By default ‘git stash pop‘ or ‘git stash apply‘ will reapply the most recently created stash: stash@{0}To choose which stash to apply, you can pass the identifier as the last argument (For eg.:- git stash pop stash@{2}).
git stash show command is used to display the summary of operations performed on the stash.
git stash show
If you want to create and check out a new branch starting from the commit at which the stash was originally created and also apply the changes saved in stash, use ‘git stash branch branch_name stash_name‘. It drops the stash which is given as the argument and if no stash is given, it drops the latest one.
git stash branch newbranch stash@{0}
To delete any particular stash (For ex:– stash@{1}), use ‘git stash drop stash@{1}‘. By default this command will drop stash@{0} if no argument is provided (git stash drop).To delete all stashes at once, use ‘git stash clear‘ command.
Git
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference Between Git Push Origin and Git Push Origin Master
How to Push Git Branch to Remote?
Git - Difference Between Git Fetch and Git Pull
How to Export Eclipse projects to GitHub?
Merge Conflicts and How to handle them
How to Add Git Credentials in Eclipse?
What is README.md File?
Top 10 GitHub Alternatives That You Can Consider
Git - Merge
Using Patches in Git