Search code examples
gitgit-stashgit-checkout

Files lost after checkouting out to the other branch


I want to apply the same changeset onto two different branches.

Here's what I do:

git init
touch README
git add README
git commit -m "initial"

git branch other

touch file2
git add file2
git stash

git checkout other
git stash apply

git checkout master
git stash apply
git stash drop

And that works. file2 exists on both branches. But if I change to the other branch and then back to master, file2 does no longer exist on master anymore! Why is it happening and what can I do about it? I also have one limitation: I cannot call git commit on master branch after applying stashed changes.

git version 1.7.11.msysgit.0


Solution

  • git stash apply only applies changes to the working tree and index, not to the repository. So when you git checkout a different branch before committing, you lose those local modifications. If you want them to persist on that branch, you have to commit to the branch.