I try to go from one local branch to another one. Git tells me that I cannot do it because my local changes to the following files would be overwritten by checkout
.
Then I get a "recommendation" Please, commit your changes or stash them before you can switch branches
.
I know that I do not need the changes to the mentioned file. It is OK to overwrite them. So, I try to stash
. I execute git stash file_name
. As a result I get:
Usage: git stash list [<options>]
or: git stash show [<stash>]
or: git stash drop [-q|--quiet] [<stash>]
or: git stash ( pop | apply ) [--index] [-q|--quiet] [<stash>]
or: git stash branch <branchname> [<stash>]
or: git stash [save [--patch] [-k|--[no-]keep-index] [-q|--quiet]
[-u|--include-untracked] [-a|--all] [<message>]]
or: git stash clear
OK. It does not work. Then I try git checkout file_name
. No complains from git. Then I can switch from one branch to another one. So, it seems that I got what I needed (go to the second branch without saving changes to the first branch).
However, I would like to ask why stash
did not work, and how the final result would be different in case it had worked?
The git stash command takes your uncommitted changes (both staged and unstaged), saves them away for later use, and then reverts them from your working copy. For example: $ git status On branch main Changes to be committed: new file: style.
The git commit and git stash commands are similar in that both take a snapshot of modified files in the git working tree and store that snapshot for future reference. The key differences between the two are as follows: A commit is part of the public git history; a stash is stored locally.
Git stash saves the uncommitted changes locally, allowing you to make changes, switch branches, and perform other Git operations. You can then reapply the stashed changes when you need them. A stash is locally scoped and is not pushed to the remote by git push .
Use git stash when you want to record the current state of the working directory and the index, but want to go back to a clean working directory. The command saves your local modifications away and reverts the working directory to match the HEAD commit.
You cannot stash a single file explicitly. Had you run git-stash
it would have stashed all your modifications.
git checkout -- file
discards your changes to that file in working dir and checkout the version of that file recorded by the current commit (i.e. the one that HEAD points to).
git stash
saves your changes into "stash" - which looks like stack of temporary commits. Can be seen with git stash list
.
With git stash
it is possible to stash certain file and even chunk of code. Try git stash save -p
. It will interactively ask you what you want to save.
Another way is to add all, but one files to index with git add
. Then run git stash save -k
. It stashes changed file (red in git status
) without files prepared to commit (green in git status
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With