0

I'm in the

Your branch is ahead of 'origin/master' by 2 commits.
(use "git push" to publish your local commits)

state after making and committing some changes in my local master branch.

How do I pick off the local changes into a new branch?

My desired outcome is: local/master is synchronized with origin/master, and I have a local branch with the two commits in it.

Dave
  • 7,555
  • 8
  • 46
  • 88
  • 1
    Possible duplicate of [Create Git branch with current changes](http://stackoverflow.com/questions/3899627/create-git-branch-with-current-changes) – Greg Burghardt Sep 19 '16 at 17:35

2 Answers2

8

First

git checkout -b newBranchName

this will create a new branch, the same as the current master you are working on.

After that,

git checkout master

this will select master as your current branch.

Finally

git reset --hard origin/master

this will remove those 2 commits from master, but they will be still available in newBranchName

EDIT: as @ShmulikKlein Mention, be careful with --hard.. you can always use --soft and then discard the changes

Matias Elorriaga
  • 8,880
  • 5
  • 40
  • 58
1

@Matias is right, but first, you should be very careful when using git reset --hard as along with revretting the last 2 commits, it will also wipe out your entirely working directory and staging area (aka, unstaged and uncommited changes).

Second, I think that the last step should be git reset --hard HEAD~2 when you are on the master branch, since you are only want to revert the last 2 commits, since you don't know how origin/master was changed since you post the question... (unless you check using git status...)

Shmulik Klein
  • 3,754
  • 19
  • 34