Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move all new code on Master to a new branch in Git

Tags:

git

branch

I'm using Git. I accidentally started doing all of my code changes on Master. How can I create a branch "new_feature" and have all of my changes moved to the "new_feature" branch and off of master?

like image 318
Don P Avatar asked Feb 18 '23 04:02

Don P


2 Answers

If you've already committed your code, you need to only run git checkout -b new_feature. This will both create your new branch and switch you to that new branch.

If you have not yet committed your changes, run git checkout -b new_feature then commit your changes.

If after creating your new branch and committing your code, you need to revert your master branch:

git checkout master              # switch to the master branch
git reset --hard origin/master   # revert master to the current origin/master

Be aware that git reset --hard will cause you to lose any uncommitted changes.

like image 89
Highway of Life Avatar answered Feb 20 '23 08:02

Highway of Life


If you haven't committed yet to the master branch. do a git diff to create a patch. create new branch

git checkout -b new_branch

and apply the patches using git apply

You can follow this question in order to understand better how to create patches from uncommited / unstaged changes

like image 40
stdcall Avatar answered Feb 20 '23 06:02

stdcall