sovon
sovon

Reputation: 907

git stash does not belongs to any branch?

I am learning and practicing git. what i have learned about stashing is, "I am working on a branch and if i have to checkout to another branch but do not want to commit the incomplete work which i was working on, then I should stash that and checkout to other branch. After coming back to stashed branch i have to 'stash apply' and then i can work on my previous incomplete work again". Is that completely right? Now when I was testing this, I did something little strange. I created a file and write something and add this to lets say branch 'A', then i stashed and checkout to master branch and command 'git stash apply' and then the file which i added to my branch 'A' and stashed, i now visible in branch.

Now My question is,

Sorry for writing something which might be silly. But I really wanna understand. Thanks.

Upvotes: 1

Views: 440

Answers (2)

cafebabe1991
cafebabe1991

Reputation: 5176

Why new file is visible at master branch?, (as i added and stashed it to branch 'A')

When you do a git stash you actually write the current state of your repository into the stash.So that file which was not yet committed got saved in the stash. Now you did git stash apply applying that state to your current branch which was branch master. Hence that file shows up in the master branch.

Isn't git add(for and unmarked file) means started to mark a new file for a particular branch?

Yes it is.

FYI

When you do a stash even after an add operation you still save that file in the stash area irrespective of the branch.

Isn't stashing means saving 'incomplete marked work' for a particular branch?

No, doing a git stash does not mark it to save for a particular branch, it just dumps the current state of your repository, irrespective of the branch.

Read here for more information

Upvotes: 2

poke
poke

Reputation: 387557

Stashes do persist information about their parent (in order to show what they change), but they don’t belong to particular branches. That’s why it’s possible to stash something on branch A, and apply that stash on branch B.

You can see the stashes as a single stack of patches that you can apply. Whenever you stash something, the stash is added on top of that stack, and whenever you apply a stash, then the top stash is applied to the current branch. Since there is only a single stack of stashes, you can apply them to any branch.

Upvotes: 2

Related Questions