Vedant Jain
The Product Guy

The Product Guy

Git - Moving uncommitted code to a different branch

Git - Moving uncommitted code to a different branch

Forgot to switch the branch before writing the code?

Vedant Jain's photo
Vedant Jain

Published on Jul 31, 2021

1 min read

Subscribe to my newsletter and never miss my upcoming articles

Did you start writing your code without realizing that you are on the wrong branch? Well, you are not the only one. This happens to most of us.

Here is a quick step-by-step guide on how to move your code to a separate branch without pushing it -

  1. Commit the changes on your (temporarily)
  2. Checkout to the branch where you want to move these changes.
  3. Merge the branch with changes to this branch.
  4. Checkout to the branch where we did the temporary commit.
  5. Undo the previous commit on the
git add -A        # 0. Stage changes, "-A" will stage all changed files
git commit -m "Temporary commit"        # 1. 
git checkout <correct-branch>        # 2. "correct-branch" is where you want to move these changes
git merge <main-branch>        # 3. "main-branch" is where we did temporary commit in step 1.
git checkout <main-branch>        # 4. back to the branch where it all started.
git reset HEAD~        # 5. Un commit changes from step 1.

Now selectively keep or discard the changes from your main branch.

Cheers! 🍻

 
Share this