git 如何将分支的内容复制到新的本地分支?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14998923/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 15:34:02  来源:igfitidea点击:

How can I copy the content of a branch to a new local branch?

gitbranch

提问by serengeti12

I have worked on a local branch and also pushed the changes to remote. I want to revert the changes on that branch and do something else on it, but I don't want to lose the work completely. I was thinking of something like create a new branch locally and copy the old branch there, then I can revert the changes and continue working on the old branch. Is there a better way maybe? Or how do I do this?

我曾在本地分支工作,并将更改推送到远程。我想恢复该分支上的更改并对其进行其他操作,但我不想完全失去工作。我正在考虑在本地创建一个新分支并在那里复制旧分支,然后我可以恢复更改并继续在旧分支上工作。也许有更好的方法?或者我该怎么做?

回答by Daniel Hilgarth

git checkout old_branch
git branch new_branch

This will give you a new branch "new_branch" with the same state as "old_branch".

这将为您提供一个与“old_branch”具有相同状态的新分支“new_branch”。

This command can be combined to the following:

此命令可以组合为以下内容:

git checkout -b new_branch old_branch

回答by Lyle Z

git branch copyOfMyBranch MyBranch

This avoids the potentially time-consuming and unnecessary act of checking out a branch. Recall that a checkout modifies the "working tree", which could take a long time if it is large or contains large files (images or videos, for example).

这避免了检查分支的潜在耗时和不必要的操作。回想一下,结帐会修改“工作树”,如果它很大或包含大文件(例如图像或视频),这可能需要很长时间。

回答by VonC

With Git 2.15 (Q4 2017), "git branch" learned "-c/-C" to create a new branch by copying an existing one.

在 Git 2.15(2017 年第 4 季度)中,“ git branch”学会了“ -c/-C”通过复制现有分支来创建新分支。

See commit c8b2cec(18 Jun 2017) by ?var Arnfj?re Bjarmason (avar).
See commit 52d59cc, commit 5463caa(18 Jun 2017) by Sahil Dua (sahildua2305).
(Merged by Junio C Hamano -- gitster--in commit 3b48045, 03 Oct 2017)

请参阅?var Arnfj?re Bjarmason ( )提交的 c8b2cec(2017 年 6 月 18 日。 请参阅Sahil Dua ( ) 的commit 52d59cccommit 5463caa(2017 年 6 月 18 日(由Junio C Hamano合并-- --commit 3b48045,2017 年 10 月 3 日)avar
sahildua2305
gitster

branch: add a --copy(-c) option to go with --move(-m)

Add the ability to --copya branch and its reflog and configuration, this uses the same underlying machinery as the --move(-m) option except the reflog and configuration is copied instead of being moved.

This is useful for e.g. copying a topic branch to a new version, e.g. workto work-2after submitting the worktopic to the list, while preserving all the tracking info and other configuration that goes with the branch, and unlike --movekeeping the other already-submitted branch around for reference.

branch: 添加一个--copy( -c) 选项来搭配--move( -m)

--copy分支及其引用日志和配置添加功能,这使用与--move( -m) 选项相同的底层机制,除了引用日志和配置被复制而不是被移动。

这是例如复制一个话题分支到一个新的版本,例如,有用workwork-2提交后work的话题列表,同时保留所有与分支而来的跟踪信息和其他配置,而不像--move保持其他已提交的分支周围参考。

Note: when copying a branch, you remain on your current branch.
As Junio C Hamano explains:

注意:复制分支时,您将保留在当前分支上。
正如 Junio C Hamano 解释的那样:

When creating a new branch Bby copying the branch Athat happens to be the current branch, it also updates HEADto point at the new branch.
It probably was made this way because "git branch -c A B" piggybacked its implementation on "git branch -m A B",

This does not match the usual expectation.
If I were sitting on a blue chair, and somebody comes and repaints it to red, I would accept ending up sitting on a chair that is now red (I am also OK to stand, instead, as there no longer is my favourite blue chair).

But if somebody creates a new red chair, modelling it after the blue chair I am sitting on, I do not expect to be booted off of the blue chair and ending up on sitting on the new red one.

B通过复制A恰好是当前分支的分支来创建新分支时,它也会更新HEAD以指向新分支。
它可能是这样制作的,因为“ git branch -c A B”在“ git branch -m A B”上搭载了它的实现,

这与通常的期望不符。
如果我坐在一张蓝色椅子上,有人来把它重新漆成红色,我会接受最终坐在一张现在是红色的椅子上(我也可以站着,因为不再有我最喜欢的蓝色椅子了)。

但是,如果有人创造了一把新的红色椅子,以我所坐的蓝色椅子为模型进行建模,我不希望被从蓝色椅子上踢下来并最终坐在新的红色椅子上。