Jacob Roberts
Jacob Roberts

Reputation: 1815

Need To: Change Git Branch After Changes

Sometimes I forget to change my branch before I make code changes using Visual Studio Code. I've tried to change my branch after changes are made but I get a response like, 'Git: Your local changes to the following files would be overwritten by checkout'. How can I commit the changes to the branch that I needed to be working in?

Upvotes: 3

Views: 9426

Answers (2)

McGlothlin
McGlothlin

Reputation: 2099

git stash
git checkout <existing branch>
git stash apply

This will pick up the changes you made on your original branch and put them in the new branch you've created. The -b flag will switch you to the new branch immediately.

Edit: As noted below, git stash/apply is not necessary when creating a new branch (using git checkout -b <new branch>). However if you try to check out an existing branch without stashing, you will get the following message:

error: Your local changes to the following files would be overwritten by checkout:
    README.md
Please, commit your changes or stash them before you can switch branches.
Aborting

Be careful when you stash and apply because you could end up with a merge conflict if there are similar edits in your target branch.

Upvotes: 11

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391396

There's a couple of mechanisms at play here.

Let me go off on a tangent here. The relevance will become apparent further down.

If you do a cherry-pick, git will analyze the changes of the commit you're cherry-picking and apply those changes on top of your current commit.

As an example, if the commit you're cherry-picking is adding a few more lines at the bottom or in the middle of a file, git will try to do the exact same thing on top of the commit you're at, even if the files before the change are different.

Now, let's get back to what you're trying to do. You've made some changes to some files, but not yet committed, and now you try to change which branch you're at before you commit.

In isolation, this might seem like git could do the same thing as above. git could identify the changes you've done, and try to bring them onto the branch you want to be at.

But there's a difference. If the cherry-pick cannot cleanly apply your changes, you will end up with a merge conflict. If you mess up trying to fix this merge conflict you can abort. And restart, and try again.

However, since you have not yet committed your changes, any such merge conflict could seriously impact the changes you've done, up to a point where you're in a jam. You cannot simply abandon what you've done, since you would lose your changes, and git has seriously compromised your files by trying to fix the merge conflict up to a point.

So git doesn't do that.

If you have uncommitted changes, and try to switch branch, you can only do so if the files you have changed are identical between the two branches. If they're not, git will simply refuse to switch to the new branch, because any merge conflict that would result could seriously damage your files.

The solution is simple, you have a couple of choices:

  1. You can do a git stash, which will actually make a commit of your changes, then switch branch, and then do a git stash pop. This will do the exact same thing as a git cherry-pick, you can restart the merge, you can retry it, you can do so with the knowledge that any problems will keep your stash so that you don't lose your changes.
  2. You can do an actual commit, then switch branches, then do a git cherry-pick to bring over the commit. This will do the same as above but you have to make sure you clean up the other branch as well. The end result can be the same, but on the way the onus is on you to do the right thing.

Upvotes: 2

Related Questions