5.3. Stashes.md - shinokada/gitnotes GitHub Wiki
Add some change to README.md and save it but not add or commit. Then run git stash
:
❯ git stash
❯ cd .git/refs/
❯ cat stash
221478b31a9c6b2983780585b3500ba23260daa6
❯ git cat-file -p 221478b31
tree 9657e3fecbb01137da1935d4c210533856d82585
parent f368651693e57ad2df3615bb47a5fb1701e3b0e7
parent d1a8a9be47a2b866e9a99a8b65171128956a97ae
...
❯ git cat-file -p 9657e3
...
100644 blob 9d47666971a2b201db4d89f0536d5766af389c7c LICENSE
100644 blob f178fb060b79f0ad921e29e4fd0c5ff3d2ffd2c2 README.md
❯ git cat-file -p f178fb
...
## Contributing
To contribute to this project, simply
The f178fb file has the recent change. Just as if I’d committed this file to the repo, I have a snapshot of my working tree at a particular point in time, but held as a stash, instead of as a commit.
❯ cd ../../
Now you can run git checkout xUtils
without Git complains.
Going back to the previous branch, use -
:
❯ git checkout -
Now check the README.md. Your changes aren't there.
❯ cat README.md
Let's create another stash:
❯ rm SECRET
❯ git stash
To see stack of stashes:
❯ git stash list
stash@{0}: WIP on main: f368651 Merge branch 'xReadmeUpdates'
stash@{1}: WIP on main: f368651 Merge branch 'xReadmeUpdates'
stash@{0}
label, is the most recent and stash at index 1 is older.
Make another change:
❯ mkdir temp && touch temp/.keep && git add .
Create a stash with an appropriate message:
❯ git stash push -m "Created temp directory"
Use the push operator, since git stash alone doesn’t let you supply any arguments.
❯ git stash list
stash@{0}: On main: Created temp directory
stash@{1}: WIP on main: f368651 Merge branch 'xReadmeUpdates'
stash@{2}: WIP on main: f368651 Merge branch 'xReadmeUpdates'
To peek at the contents of that stack entry with the following command:
❯ git stash show stash@{0}
...
temp/.keep | 0
3 files changed, 0 insertions(+), 0 deletions(-)
To show the entire patch of your stash (or to check out the diff):
❯ git stash show -p stash@{2}
diff --git a/README.md b/README.md
index 6c6eee6..f178fb0 100644
--- a/README.md
+++ b/README.md
@@ -19,3 +19,7 @@ This project is maintained by teamWYXZ:
...
+
+## Contributing
+
+To contribute to this project, simply
\ No newline at end of file
Use git stash pop
to remove the top stash from the stack and apply that patch to your working environment.
To cherry-pick a particular stash out of the stack and apply it to your working tree with the git stash apply stash@{number}
command.
To apply the stash at the bottom of the stack which is the third item you’d use stash@{2}:
❯ git stash apply stash@{2}
On branch main
Your branch is ahead of 'origin/main' by 17 commits.
(use "git push" to publish your local commits)
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: README.md
no changes added to commit (use "git add" and/or "git commit -a")
Your stash doesn’t have the temp directory, and it still has the SECRETS file.
❯ ls -al
total 64
...
-rw-r--r-- 1 shinichiokada staff 483 Jul 24 16:21 README.md
-rw-r--r-- 1 shinichiokada staff 65 Jul 24 16:23 SECRETS
When you apply a stash, you overwrite not just the working tree, but also the staging area of your repository. You staged your addition of the temp/.keep file after you created the stash you just applied.
Applying a stash doesn’t remove it from the stack; you can see this if you execute git stash list
:
❯ git stash list
stash@{0}: On main: Created temp directory
stash@{1}: WIP on main: f368651 Merge branch 'xReadmeUpdates'
stash@{2}: WIP on main: f368651 Merge branch 'xReadmeUpdates'
Compete the README.md and stage and commit:
❯ git add . && git commit -m "Updates readme"
or
❯ git commit -am "Updates readme"
You want to add that temp/.keep. That change was stashed at the top of the stash stack. so you can use the pop operator to simultaneously apply the patch of that stash and remove it from the top of the stack:
❯ git stash pop
On branch main
Your branch is ahead of 'origin/main' by 18 commits.
(use "git push" to publish your local commits)
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: .DS_Store
new file: js/.DS_Store
new file: temp/.keep
Dropped refs/stash@{0} (b5a2240eadb5038ad14d40fd76ca5ccc69f951fd)
❯ git stash list
stash@{0}: WIP on main: f368651 Merge branch 'xReadmeUpdates'
stash@{1}: WIP on main: f368651 Merge branch 'xReadmeUpdates'
You don’t need to commit that change just yet so you’ll remove it for now. Run the following commands to unstage and remove the file:
❯ git reset temp/.keep
❯ rm temp/.keep
❯ git stash clear
Remove all the stash entries.
In the main branch update README.md:
For information on this project, please contact [Xanthe]
(mailto:[email protected]).
Save your work and run git stash
❯ git stash
Checkout to xUtils:
❯ git checkout xUtils
Update README.md. After #Maintainers
## Contact Info
For info on this project, please contact [Xanthe]
(mailto:[email protected]) or [Will](mailto:[email protected]).
Save it and run git add/commit:
❯ git commit -am "Added Will as a contributor"
Now attempt to apply the stash with the following command:
❯ git stash pop
You get the confilicts:
## Contact Info
<<<<<<< Updated upstream
For info on this project, please contact [Xanthe]
(mailto:[email protected]) or [Will](mailto:[email protected]).
||||||| Stash base
For info on this project, please contact [Xanthe](mailto:[email protected]).
## Contributing
To contribute to this project, simply create a pull request on
this repository and we’ll review it when we can.
=======
For information on this project, please contact [Xanthe](mailto:[email protected]).
## Contributing
To contribute to this project, simply create a pull request on
this repository and we’ll review it when we can.
>>>>>>> Stashed changes
Update the conflict and run git add/commit:
❯ git commit -am "Corrected language usage"
When Git encountered a conflict when trying to merge your stash, Git immediately bailed on the stash removal option, and left the merge conflicts for you to clean up. So Git leaves the stash on the stack.
To get help run:
❯ man git stash
or
❯ git stash --help
To remove stashes from the stack:
git stash drop stashname
Popping a stash is just a combination of apply
and then drop
.
❯ git stash drop stash@{0}
Dropped stash@{0} (bca4bbd32e9935496feccc0428031bd870d677d2)
"git stash drop" is useful for cleaning up your stash stack, should you have a lot on there. Keep the stash stacks brief and as short-lived as possible.