Git stash
Stashing Changes: git stash
Application Scenarios
When you are developing a project on the
devbranch and a bug arises that requires immediate fixing, but your current work is only half done and you don’t want to commit it yet. You can use thegit stashcommand to save your changes to the stash, then switch to thehotfixbranch to fix the bug. After completing the fix, you can switch back to thedevbranch and restore your stashed changes.Due to an oversight, you developed content on the
masterbranch instead of thedevbranch. You can stash your changes, switch back to thedevbranch, and then restore your content.
In summary, the git stash command allows you to save changes that you don’t want to commit yet to a stash. You can later restore these stashed changes to any branch, not just the one you were originally working on. The stash can include changes in both the working directory and the staging area, meaning any uncommitted changes will be saved.
Command Details
1. git stash
This command saves all uncommitted changes (both in the working directory and staging area) to the stash for later restoration.
1 | $ git status |
2. git stash save
This command functions the same as git stash, but it allows you to add a message for reference. For example, git stash results in:
1 | stash@{0}: WIP on master: b2f489c second |
While git stash save "test1" results in:
1 | stash@{0}: On master: test1 |
3. git stash list
This command displays the contents currently stored in the stash.
4. git stash pop
This command pops the most recent stash entry and applies it to the working directory of the current branch. Note that this command deletes the popped stash entry (it follows a last-in, first-out order). For instance, after executing git stash save "test1" and git stash save "test2":
1 | $ git stash list |
This shows that test2 was popped first. If there are conflicts between the restored changes and the current working directory (e.g., both modified the same lines), an error will occur, and you need to resolve the conflict, potentially by creating a new branch.
5. git stash apply
This command applies the changes from the stash to the working directory without deleting the stash entry. This means you can apply the same stash entry multiple times across different branches.
1 | $ git stash apply |
The stash contents are still available. You can specify which stash to apply using git stash apply <stash-name> (e.g., stash@{1}).
6. git stash drop <name>
This command removes a specific stash entry from the stash.
7. git stash clear
This command clears all entries in the stash.
8. git stash show
This command shows the differences between the latest stash and the current directory.
1 | $ git stash show |
To view the differences for a specific stash, use git stash show stash@{1}. You can also use git stash show -p for a more detailed view:
1 | $ git stash show -p |
Similarly, you can view the differences for a specific stash with git stash show stash@{1} -p.
9. git stash branch
This command creates a new branch from the latest stash. This is useful when you have stashed changes but want to continue developing on the current branch and later restore the stash content to the working directory. If modifications conflict (even if not on the same line), creating a new branch can help resolve these conflicts. You will need to resolve any conflicts manually.
- Title: Git stash
- Author: LHT
- Created at : 2022-06-03 06:10:00
- Link: https://blog.327774.xyz/2022/06/02/git/Git stash/
- License: This work is licensed under CC BY-NC-SA 4.0.