7

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?

Roman
  • 124,451
  • 167
  • 349
  • 456
  • 1
    `stash` didn't work because you used it wrong. See `git help stash`. The difference you ask for is that if you had stashed the changes they would then be stashed, but now you lost those changes instead. – Biffen Sep 23 '14 at 11:29
  • What does it mean to "stash" changes? If changes are "stashed" then they a kind of memorized? Then what is the difference between "stash" and "add". I do memorize changes (in the branch) if I "add" these changes to the staging area. – Roman Sep 23 '14 at 11:33
  • `git help stash` will explain this in detail. – Wooble Sep 23 '14 at 11:35
  • 1
    @Roman Stashing means putting changes away ‘outside of’ Git's history. Again see `git help stash` as well as [this](http://git-scm.com/book/en/Git-Tools-Stashing). – Biffen Sep 23 '14 at 11:35

3 Answers3

10

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).

Mudassir Razvi
  • 1,783
  • 12
  • 33
  • Why git provides a possibility to stash everything but not a single file? Doesn't stash "discards my changes to the files in working dir and checks out the latest committed version of that file"? – Roman Sep 23 '14 at 11:31
  • 1
    @roman, you can stash any part you want. Similar to `git add -p` which lets you add or reject each chunk, you can do `git stash -p`. See [here](http://stackoverflow.com/a/5506483/912144) – Shahbaz Sep 23 '14 at 11:39
  • 1
    *latest* isn't quite the right word, here; it leaves room for misinterpretation. Instead, you should right: *the version recorded by the current commit* (i.e. the one that `HEAD` points to). – jub0bs Oct 11 '14 at 10:47
  • You can now stash single files, as of Git 2.13: https://stackoverflow.com/a/5506483/1743874 – Piccolo Jul 16 '19 at 16:53
2

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).

LVitya
  • 528
  • 4
  • 11
0

With out commit changes when need to change the branch you can use git stash command to save changes to a stack of unfinished changes. If you need to stash only a selected part of the works done you can use git stash -p. To reuse the saved changes use git stash pop command.

In git checkout command if you are giving the command as git checkout your working tree will direct to current HEAD. git checkout branchname When you checkout from a branch and if there are uncommited changes those changes are added to the newly checkout branch as modified changes. git checkout filepath when you are using checkout with a file path the uncommited changes of the file will get lost.

Reference - https://git-scm.com/docs/git-checkout

https://git-scm.com/book/en/v1/Git-Tools-Stashing

  • `git checkout filepath` or `git checkout -- filepath` will take effect on changes that are unstaged. If the changes are already staged, checkout should no longer be effective (we should not lose the staged, but not yet committed changes in this case). We'd need to reach for `git reset HEAD filepath` if we wish to unstage. – Shan Dou Sep 09 '19 at 23:56