I just wrote changes to the wrong git branch

Problem

Ever realized you're writing changes to the wrong branch? 

Solution

git stash 
git checkout -b <branch>
git stash pop

Explanation

Git stash will shelve your changes. While they're shelved, you can do anything you need to: change branches, create a new branch, get a cup of coffee, whatever. In this example, we're using git checkout -b xxx to check out the branch we should have been working on, then popping our stash on to the new branch. Pop will implement all your stashed changes on the branch you're currently on then discards the stash. I didn't want to use the word "apply" because git apply is a similar command. The difference is that git apply leaves the stash in the list for future use. In this case, we don't need it, so pop suits our use.

Add comment

Loading