git move locally committed changes to the new branch and push

22,778

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
Share:
22,778

Related videos on Youtube

Quillion
Author by

Quillion

I love programming. At this point programming for me is essential part of everyday life! It has become essential extension of me and regardless of what I do I can not be thinking about programming. And I love it!!! My kids ask me what programming is, and I say: Programming is like being a magician, you write a spell and it does magic.

Updated on July 09, 2022

Comments

  • Quillion
    Quillion almost 2 years

    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.

    • Balog Pal
      Balog Pal almost 11 years
      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.
    • Quillion
      Quillion almost 11 years
      @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.
    • Balog Pal
      Balog Pal almost 11 years
      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
    • Quillion
      Quillion almost 11 years
      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
    • Balog Pal
      Balog Pal almost 11 years
      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
    • Quillion
      Quillion almost 11 years
      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
    • Balog Pal
      Balog Pal almost 11 years
      you must have pushed the master at some point, to undo reset master as was written and push --force.
    • Quillion
      Quillion almost 11 years
      @BalogPal Thanks it worked :) I will however still keep this just because now this is something different
    • Bryan P
      Bryan P over 7 years
      Seems a combination of stackoverflow.com/q/1628563/1224158 and then pushing the new branch would work.
  • Quillion
    Quillion almost 11 years
    So then: 1) git checkout -b mybranch 2) git push 3) git reset --hard origin/master and that is it?
  • Balog Pal
    Balog Pal almost 11 years
    before 3) git checkout master
  • Bryan P
    Bryan P over 7 years
    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.
  • Matthias
    Matthias about 7 years
    @BryanP I guess it could be that git push not only pushes the checked out branch, but also others ( stackoverflow.com/questions/948354/… )
  • Muhammed Ibrahim
    Muhammed Ibrahim almost 4 years
    step 2) might be needed to be git push origin mybranch instead.