Git - Moving uncommitted code to a different branch
Forgot to switch the branch before writing the code?
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 -
- Commit the changes on your (temporarily)
- Checkout to the branch where you want to move these changes.
- Merge the branch with changes to this branch.
- Checkout to the branch where we did the temporary commit.
- 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.