Stash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| git stash [push]
# --keep-index tells Git to not only include all staged content in the stash being created, but simultaneously leave it in the index.
git stash --keep-index
# list stashes
git stash list
# apply stash
# The changes to your files were reapplied, but the file you staged before wasn’t restaged.
git stash apply
# --index option to tell the command to try to reapply the staged changes.
git stash apply --index
# interactively stash
git stash --patch
# By default, git stash will stash only modified and staged tracked files. If you specify --include-untracked or -u, Git will include untracked files in the stash being created. However, including untracked files in the stash will still not include explicitly ignored files;
git stash -u
# to additionally include ignored files, use --all (or just -a).
git stash -a
|
Creating a Branch from a Stash
If you stash some work, leave it there for a while, and continue on the branch from which you stashed the work, you may have a problem reapplying the work. If the apply tries to modify a file that you’ve since modified, you’ll get a merge conflict and will have to try to resolve it. If you want an easier way to test the stashed changes again, you can run git stash branch <new branchname>
, which creates a new branch for you with your selected branch name, checks out the commit you were on when you stashed your work, reapplies your work there, and then drops the stash if it applies successfully
1
| git stash branch testchanges
|
Cleaning your Working Directory
1
2
3
4
5
6
7
8
9
10
11
12
| # remove all the untracked files in your working directory
# The -f means 'force' or “really do this,” and is required if the Git configuration variable clean.requireForce is not explicitly set to false.
git clean -f -d
# If you ever want to see what it would do, you can run the command with the --dry-run (or -n) option, which means “do a dry run and tell me what you would have removed”.
git clean -d -n
# By default, the git clean command will only remove untracked files that are not ignored. Any file that matches a pattern in your .gitignore or other ignore files will not be removed. If you want to remove those files too, such as to remove all .o files generated from a build so you can do a fully clean build, you can add a -x to the clean command.
git clean -n -d -x
# interactively clean
git clean -x -i
|
Reference
https://git-scm.com/book/en/v2/Git-Tools-Stashing-and-Cleaning