Forgot to branch in git, need to move changes from master
git branch -M master my-branch
With a
-m
or-M
option,<oldbranch>
will be renamed to<newbranch>
. If<oldbranch>
had a corresponding reflog, it is renamed to match<newbranch>
, and a reflog entry is created to remember the branch renaming. If<newbranch>
exists,-M
must be used to force the rename to happen.
Source
and then
git fetch origin refs/heads/master:refs/heads/master
or
git branch master my-branch (or another ref)
git stash
git stash branch <branchname>
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