Git: Oops! I Changed Those Files In The Wrong Branch!

I do this a lot… I’ll be assigned some work, start into it and get part way down the path, am ready for a commit and I realize that I’ve been changing code in the wrong branch of my local git repository. For example:


… but I didn’t want those changes on the master branch… I wanted them on some other branch, such as “mybranch” or “existingbranch”.

The good news is that git is AWESOME and this really isn’t a big deal. I can easily move these changes to a new branch or to an existing branch by doing one of the following… and there are probably many other ways of accomplishing the goal of moving your changes to a new or existing branch, too. These are the two methods I use most often, though.


Move To A New Branch

To move these changes to a new branch, run this command where “mybranch” is the name of the branch you want to create.

   1: git checkout -b mybranch

</div> </div>

and then run git status again. You’ll see that the changes you made are still in place and you can now commit them to the new branch.



Move To An Existing Branch

To move these changes to an exiting branch, just checkout the other branch with the regular checkout command. For example, run this command where “existingbranch” is the name of an existing branch.

   1: git checkout existingbranch

</div> </div>

Then run git status again, and you’ll see the same results but you’ll be on the existing branch:


Find And Replace With Regular Expressions In Vim / ViEmu