忘记在 git 中分支,需要从 master 移动更改
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19612439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Forgot to branch in git, need to move changes from master
提问by larryq
I eagerly ducked into code mode and modified some files, but neglected to branch from master first. The mods aren't so extensive that I can't redo them, but what's a good way of taking my (so far, uncommitted) changes in master and migrating them to a new branch, leaving master untouched in the end?
我急切地进入代码模式并修改了一些文件,但忽略了首先从 master 分支。mod 并没有那么广泛,以至于我无法重做它们,但是有什么好方法可以将我的(到目前为止,未提交的)更改在 master 中并将它们迁移到一个新分支,最终让 master 保持不变?
回答by torek
If not yet committed anywhere (git status
shows a bunch of stuff modified, it's OK if it's "git add"-ed too):
如果还没有在任何地方提交(git status
显示一堆修改过的东西,如果它也是“git add”-ed也可以):
$ 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.
尽管有这个名字,checkout
这个用法 (with -b
) 并没有检查出任何东西。该-b
标志表示“创建一个新分支”,因此 git 创建分支名称并使其对应于当前HEAD
提交。然后它HEAD
指向新的分支,并停在那里。
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:
因此newbranch
,您的下一次提交是 on ,它的父提交是您开始修改文件时所在的提交。所以假设你在master
,并且你有这些提交:
A - B - C <-- HEAD=master
the checkout -b
makes this read:
这checkout -b
使得这个读:
A - B - C <-- master, HEAD=newbranch
and a later commit adds a new commit D
:
和以后的提交添加一个新的提交D
:
A - B - C <-- master
\
D <-- newbranch
回答by ensc
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.
使用
-m
或-M
选项,<oldbranch>
将重命名为<newbranch>
. 如果<oldbranch>
有相应的 reflog,则将其重命名为 match<newbranch>
,并创建一个 reflog 条目以记住分支重命名。如果<newbranch>
存在,-M
必须用于强制重命名发生。
and then
进而
git fetch origin refs/heads/master:refs/heads/master
or
或者
git branch master my-branch (or another ref)
回答by alko
git stash
git stash branch <branchname>