忘了在git中分支,需要从master中移动更改[duplicate]

时间:2022-03-07 23:45:45

This question already has an answer here:

这个问题在这里已有答案:

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保持不变?

3 个解决方案

#1


23  

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.

尽管名称结帐,但这种用法(带-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上,它具有作为其父提交的开始修改文件时所提交的提交。所以假设你是主人,你有这些提交:

A - B - C       <-- HEAD=master

the checkout -b makes this read:

结帐-b使这个读取:

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

and a later commit adds a new commit D:

并且稍后的提交添加新的提交D:

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

#2


2  

git stash
git stash branch <branchname>

#3


2  

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)

#1


23  

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.

尽管名称结帐,但这种用法(带-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上,它具有作为其父提交的开始修改文件时所提交的提交。所以假设你是主人,你有这些提交:

A - B - C       <-- HEAD=master

the checkout -b makes this read:

结帐-b使这个读取:

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

and a later commit adds a new commit D:

并且稍后的提交添加新的提交D:

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

#2


2  

git stash
git stash branch <branchname>

#3


2  

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)