4

Scenario: I have 2 branches: dev and sprint1

  1. I made some changes while checked out to branch sprint1;
  2. I don't want to add/commit these changes in sprint1, I want to add/commit them on dev;
  3. I tried to checkout dev without committing changes in sprint1 first, but the checkout fails;

My question is: How can I bring those changes in sprint1 to dev without committing them to sprint1 ;

Andrew C
  • 13,845
  • 6
  • 50
  • 57
viscroad
  • 203
  • 2
  • 9
  • possible duplicate of [How to merge my local uncommitted changes into another Git branch?](http://stackoverflow.com/questions/556923/how-to-merge-my-local-uncommitted-changes-into-another-git-branch) – skrrgwasme Oct 22 '14 at 16:33

2 Answers2

3

Stashing

git stash
git checkout dev
git stash apply
git commit
Kate Miháliková
  • 2,035
  • 15
  • 21
3

You can merge your local modifications into the branch you want to checkout.

On your sprint1 branch do:

git checkout --merge dev

This will merge the local modifications of the working directory into the dev branch and switch to it. The changes are just merged and not committed. So you can now go on as usual.

git add ....
git commit

From the git checkout documentation

--merge

When switching branches, if you have local modifications to one or more files that are different between the current branch and the branch to which you are switching, the command refuses to switch branches in order to preserve your modifications in context. However, with this option, a three-way merge between the current branch, your working tree contents, and the new branch is done, and you will be on the new branch.

René Link
  • 48,224
  • 13
  • 108
  • 140