Forgot to branch in git, need to move changes from master

This question already has an answer here:

  • Move existing, uncommitted work to a new branch in Git 4 answers

  • If not yet committed anywhere ( git status shows a bunch of stuff modified, it's OK if it's "git add"-ed too):

    $ git checkout -b newbranch
    

    Despite the name checkout this usage (with -b ) does not check anything out. The -b flag says "create a new branch", so git creates the branch-name and makes it correspond to the current HEAD commit. Then it makes HEAD point to the new branch, and stops there.

    Your next commit is therefore on newbranch , which has as its parent commit, the commit you were on when you started modifying files. So assuming you were on master , and you had these commits:

    A - B - C       <-- HEAD=master
    

    the checkout -b makes this read:

    A - B - C       <-- master, HEAD=newbranch
    

    and a later commit adds a new commit D :

    A - B - C       <-- master
              
                D   <-- newbranch
    

    git stash
    git stash branch <branchname>
    

    git branch -M master my-branch
    

    and then

    git fetch origin refs/heads/master:refs/heads/master
    

    or

    git branch master my-branch  (or another ref)
    
    链接地址: http://www.djcxy.com/p/23582.html

    上一篇: Git承诺新分支

    下一篇: 忘了在git中分支,需要从主服务器移动更改