50

When I need to save my changes from one branch before checking out to another branch, git sometimes says: stage or commit the files before you can checkout to another branch. But I have been recommended to use stash option so:

  1. Stage the files is not enough to save my files before checking out to another branch?

  2. What are the differences between stage and stash files?

Thanks

mayo
  • 3,845
  • 1
  • 32
  • 42
fernando1979
  • 1,727
  • 2
  • 20
  • 26
  • possible duplicate of [What's the difference between the index, cached, and staged in git?](http://stackoverflow.com/questions/3516823/whats-the-difference-between-the-index-cached-and-staged-in-git) – Cayce K Jul 23 '15 at 20:26

2 Answers2

79

1.- More than "save" your files, is act as Git expect to according their flow. (Advice, Git knows :) )

2.- Stash will move your modified files into a stack. So, later in the same or in another branch, you will be able to bring them back and see those modifications in your project.

Stage is the step before to make a commit, you add modified files to "Staged files" to create your next commit.


Now, you stash your files with
$git stash

and you add files (stage) with

$git add


Now, why is better stash your changes than staging them? Maybe this part of the documentation can solve your doubts: From documentation:

Stashing:

Often, when you’ve been working on part of your project, things are in a messy state and you want to switch branches for a bit to work on something else. The problem is, you don’t want to do a commit of half-done work just so you can get back to this point later. The answer to this issue is the git stash command.

See the links below :

Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
mayo
  • 3,845
  • 1
  • 32
  • 42
  • 1
    I read that I need to stage the files first because otherway theses files will be ignored to stash them – fernando1979 Jul 24 '15 at 16:59
  • If you stash your files you should "unstash" to recover them. Now, if those are new files, yes, you should stage them first. – mayo Jul 24 '15 at 17:35
  • I dont understand why I need first to stage new files to stash them later. is not the purpose of stash files to "save" changes that are not finished, tested.. Why then the need to stage them like if I were to commit them later? – fernando1979 Jul 24 '15 at 18:08
  • 3
    If your files are "new" they are not being tracked by git until you **add** them. After that, git knows those files and can stash them. – mayo Jul 24 '15 at 18:14
  • 1
    If your files are already tracked and your changes are "new", git already 'knows' those files, so you don't have to add them again. – mayo Jul 24 '15 at 18:15
  • *stash* reverts your code to the latest committed state (assuming all files are staged) and saves your changes (differences) somewhere. If you'd like to unstash you use *git stash pop*. – محمد جعفر نعمة Jun 17 '22 at 17:38
3

It's better to ask difference between stash vs commit and not stash vs stage.

You can not checkout to another branch before commit or stash current changes.

Therefore, if you want to not commit your changes, and also want to checkout to another branch, solution is to stash current changes, checkout to another branch. And after returning to first branch, you can apply stashed changes.

AhmadYo
  • 334
  • 3
  • 6