4

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?

Highway of Life
  • 22,803
  • 16
  • 52
  • 80
Don P
  • 60,113
  • 114
  • 300
  • 432
  • Did you commit your changes? If not then just git branch new_feature and commit to that. If you've commited then do the same and use git log to find the version master was at before you started making changes and git checkout to get it back to that state. – Joe Mills Mar 13 '13 at 05:43

4 Answers4

3

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.

Highway of Life
  • 22,803
  • 16
  • 52
  • 80
2

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

Community
  • 1
  • 1
stdcall
  • 27,613
  • 18
  • 81
  • 125
2

If you haven't committed your changes just switch to a new branch:

git checkout -b new_features

This will take all your changes to the new branch without effecting master branch. Here you can commit all your changes.

Atin Agrawal
  • 178
  • 3
  • 12
1

Try this:

% git commit ...               # save any untracked changes
% git branch <new-branch>      # created a branch, but you're still on master
% git log                      # find the first development commit
% git reset --hard "<commit>^" # set the master branch to before the commit
% git checkout <new-branch>    # recommence your development work
% git checkout master          # return to work prior to development

Be careful with reset --hard, as you can lose untracked changes. Also, do not reset --hard beyond a commit that has already been pushed to the remote.

Eric Walker
  • 7,063
  • 3
  • 35
  • 38