Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between stash vs stage files in GIT

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

like image 366
user3254515 Avatar asked Jul 23 '15 19:07

user3254515


People also ask

Does git stash include staged files?

The answer to the question as asked ("does stash convert staged files to unstaged") is both yes and no. If you've applied with git stash apply (vs git stash pop ), you're in great shape because the stash is still present.

What is stage file in git?

Staged files are files that are ready to be committed to the repository you are working on. You will learn more about commit shortly.

What is stash file in git?

git stash temporarily shelves (or stashes) changes you've made to your working copy so you can work on something else, and then come back and re-apply them later on.

What is stash all and keep staged?

So, a stash is like a local copy of your uncommitted changes. Clicking on “Stash All” will also undo all those uncommitted changes in your branch. If you want to keep the changes in the branch that you have already staged for a commit, you select “Stash All and Keep Staged”.


1 Answers

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 :

  • Git Stashing Doc
  • Git Add Doc
  • Staging example
  • Git Basics
like image 142
mayo Avatar answered Sep 23 '22 10:09

mayo