How can I stash all the files, even the untracked ones?
git stash -u
How can I avoid conflicts when I pop my stash entry?
# Restore the stash entry on top of the commit
# that was HEAD at the time the git stash command was run
git stash branch <branch_name>
Stash -u option
If you are a Git user, git stash [push]/ git stash pop should be well known from you.
But, do you already experience the “Oh sh**, git stash didn’t stash my untracked files” ?
Next time, just add the -u option.
From a such git status,
> git status
On branch master
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
Untracked files:
(use "git add <file>..." to include in what will be committed)
CHANGELOG.md
no changes added to commit (use "git add" and/or "git commit -a")
if you run git stash
> git stash
Saved working directory and index state WIP on master: 9296abd Add README
> git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
CHANGELOG.md
nothing added to commit but untracked files present (use "git add" to track)
if you run git stash -u
> git stash -u
Saved working directory and index state WIP on master: 9296abd Add README
> git status
On branch master
nothing to commit, working tree clean
Branch command
Another issue Developers are used to experiencing is getting conflicts after git stash pop because the branch on which they ran git stash has changed since the stash was done.
Instead of git stash pop run git stash branch [branch_name]. Git will restore the stash entry on a new branch you named on top of the HEAD commit at the time the git stash command was run.
Suppose you try to pop changes to the README.md file, updated since your stash.
> git stash pop
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
The stash entry is kept in case you need it again.
With the branch command, your changes are applied to a new readme_update branch on top of the latest commit at the time you stashed.
> git stash branch readme_update
Switched to a new branch 'readme_update'
On branch readme_update
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")
Dropped refs/stash@{0} (aaaaa4ef4365f9e7218f21233a9b477100e6d8f68)