Patricio Treviño

Patricio Treviño

Husband . Father . Developer

Stash all changes in a git repository

Syntax

# stash everything (tracked, untracked, ignored)
git stash --all
# stash everything, (tracked and untracked, but not ignored)
git stash [save -u | --include-untracked]
view raw syntax.text hosted with ❤ by GitHub

Example

With git stash --all

# adds a new file
$ touch file.txt
# update .gitignore to include file.txt
$ "file.txt" > .gitignore
# get the status
$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: .gitignore
no changes added to commit (use "git add" and/or "git commit -a")
# stash changes (stashes .gitignore and file.txt)
$ git stash --all
# get the status
$ git status
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
view raw example1.sh hosted with ❤ by GitHub

With git stash --include-untracked

# adds a new file
$ touch file.txt
# update .gitignore to include file.txt
$ "file.txt" > .gitignore
# get the status
$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: .gitignore
no changes added to commit (use "git add" and/or "git commit -a")
# stash changes (stashes .gitignore, file.txt becomes available as it's no longer ignored)
$ git stash --include-untracked
# get the status
$ git status
On branch master
Your branch is up to date with 'origin/master'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
file.txt
nothing added to commit but untracked files present (use "git add" to track)
view raw example2.sh hosted with ❤ by GitHub

References

Stackoverflow: How do you stash an untracked file?