21

I am on master. When I do git status I am told

$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 13 commits.
#   (use "git push" to publish your local commits)
#
nothing to commit, working directory clean

So all 13 only exist on my local machine. The problem is that these 13 commits are now supposed to go on a new branch that I should create and push onto the server. I have tried looking at rebase but I am told

$ git rebase origina/master
fatal: Needed a single revision
invalid upstream origina/master

How would I go about pushing these changes into a new branch without messing up the master?

Just to clarify. This is not a duplicate of
moving committed (but not pushed) changes to a new branch this one simply does not work for me no matter what I do.
or
Git: Howto move changes since last commit to a new branch again is of no help.

Community
  • 1
  • 1
Quillion
  • 6,346
  • 11
  • 60
  • 97
  • please rephrase the question to explain why the last link is not the solution of your problem, as it definitely is for the one you stated. – Balog Pal Jul 02 '13 at 15:10
  • @BalogPal Because it has to do with only one change instead of 13 changes, therefore I assumed that it would be different. If it is not, then I will gladly do it, apologize and close/delete this question. Should I try it? The changes are 2 month's worth of work and I can't afford to lose it. – Quillion Jul 02 '13 at 15:16
  • In the question you say "these 13 commits are now supposed to go". If you actually want only one or a couple you should ask for that; if you have your commits it's really hard to lose anything in git. but as it allows many ways and many approaches you shall be precise in asking. probably if you described the resulting state we'd be ahead – Balog Pal Jul 02 '13 at 15:18
  • Yes I want to commit all 13 changes, but the link provides the solution for only one latest change. I do not know what is the difference – Quillion Jul 02 '13 at 15:19
  • the result of what the last link and answer below that you'll see on the origin, master branch unchanged, and the 13 commits appear on a new branch starting at commit where your origin/master now points – Balog Pal Jul 02 '13 at 15:23
  • Well I have tried that, and unfortunately I have ended up having those changes committed to both branches -_-ll well will just have to now remove them from master somehow – Quillion Jul 02 '13 at 15:30
  • you must have pushed the master at some point, to undo reset master as was written and push --force. – Balog Pal Jul 02 '13 at 15:35
  • @BalogPal Thanks it worked :) I will however still keep this just because now this is something different – Quillion Jul 02 '13 at 15:39
  • Seems a combination of http://stackoverflow.com/q/1628563/1224158 and then pushing the new branch would work. – Bryan P Nov 02 '16 at 06:51

1 Answers1

59

Just do git checkout -b yourbranch and push that.

Then reset master to origin/master.

Order:

git checkout -b mybranch
git push
git checkout master
git reset --hard origin/master
Community
  • 1
  • 1
Balog Pal
  • 16,195
  • 2
  • 23
  • 37
  • So then: 1) `git checkout -b mybranch` 2) `git push` 3) `git reset --hard origin/master` and that is it? – Quillion Jul 02 '13 at 15:11
  • 4
    before 3) git checkout master – Balog Pal Jul 02 '13 at 15:17
  • WARNING: this sequence didn't work for me. Rather than moving the "13" new local commits to a new branch keeping the remote intact, at least some of the recent changes that I wanted only on the new branch appear in master (or develop in my case). I don't think this was what OP intended. – Bryan P Nov 02 '16 at 06:50
  • 1
    @BryanP I guess it could be that git push not only pushes the checked out branch, but also others ( http://stackoverflow.com/questions/948354/default-behavior-of-git-push-without-a-branch-specified ) – Matthias Feb 09 '17 at 14:39
  • step 2) might be needed to be `git push origin mybranch` instead. – Muhammed Ibrahim Jul 18 '20 at 12:17