When starting git it’s really helpful to know there are a bunch of locations your source can exist. These are the:
- stash
- working directory
- index
- local repo
- remote repo
I don’t know if this cheat sheet helps or just overwhelms but some may find it useful: http://www.ndpsoftware.com/git-cheatsheet.html#loc=workspace
git stash
The docs say:
but there are plenty of ambiguities here.
stash
What exactly does stash
mean?
And what’s a dirty working directory
?
Say you’ve got this:
|
On branch master Changes not staged for commit: (use "git add ..." to update what will be committed) (use "git checkout -- ..." to discard changes in working directory) modified: A Untracked files: (use "git add ..." to include in what will be committed) B |
in your current directory your gut reaction might be that it will store your changes to both A and B. After all you’ve changed both and they’re both in your working directory.
That’s not the case:
|
git stash Saved working directory and index state WIP on master: 2236ca9 with A On branch master Untracked files: (use "git add ..." to include in what will be committed) B nothing added to commit but untracked files present (use "git add" to track) |
B
is ignored. You need to add it to the repo to stash it.
To stash B
, use git stash -u
Note: a dirty working directory
would seem to indicate a changed file in the current working directory. It isn’t.
dirty
state means modified tracked files and staged changes
https://git-scm.com/book/en/v1/Git-Tools-Stashing
working directory
is a repository created with the git init command.
https://stackoverflow.com/questions/36201342/git-where-exactly-is-the-working-directory
https://stackoverflow.com/questions/53744931/clarifying-the-working-directory-vs-current-directory-in-git
Useful discussions:
https://stackoverflow.com/questions/20642980/does-git-dirty-mean-files-not-staged-or-not-committed-glossary-conflict
View stash
List stashes with:
git stash list
View contents of stash with:
git stash show -p stash@{0}
Apply stash
Apply stash with:
git stash apply stash@{0}
Note that this won’t remove the stash from the list.
If you attempt to stash it again, you’ll just add another identical entry into your stash list.
To remove it from the list use:
git stash drop stash@{0}
https://git-scm.com/book/en/v1/Git-Tools-Stashing