git 如何在git中向后移动分支?

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

How to move a branch backwards in git?

gitgithub

提问by user89021

The title is not very clear. What I actually need to do often is the following:

标题不是很清楚。我实际上经常需要做的是以下内容:

Let's say I have a development going on with several commits c1,c2,... and 3 branches A,B,C

假设我有几个提交 c1,c2,... 和 3 个分支 A,B,C 的开发正在进行

c1--c2--c3--(B)--c4--(A,C)

Branch A and C are at the same commit.

分支 A 和 C 处于同一提交。

Now I want branch A to go back where B is, so that it looks like this:

现在我希望分支 A 回到 B 所在的位置,使其看起来像这样:

c1--c2--c3--(A,B)--c4--(C)

Important is that this has to happen locally and on GitHub.

重要的是,这必须在本地和 GitHub 上进行。

回答by Bram Schoenmakers

Use the reset subcommand:

使用重置子命令:

git checkout A
git reset --hard B
git push --force github

As a sidenote, you should be careful when using git resetwhile a branch has been pushed elsewhere already. This may cause trouble to those who have already checked out your changes.

作为旁注,git reset在分支已经被推送到其他地方时使用时应该小心。这可能会给已经签出您的更改的人带来麻烦。

回答by Tim Henigan

If there are no commits on branch A, then the git reset --hard Bsolution given by Bram Schoenmakerswill work.

如果在 branch 上没有提交A,那么git reset --hard BBram Schoenmakers 给出解决方案将起作用。

However if there are commits are branch Awhich must be preserved, then the following should do the trick:

但是,如果有A必须保留的提交是分支,那么以下应该可以解决问题:

  1. Make a backup copy of your repo (just in case)
  2. git checkout A
  3. git rebase -i --onto B SHA1-A^
  1. 制作您的回购的备份副本(以防万一)
  2. git checkout A
  3. git rebase -i --onto B SHA1-A^

...where SHA1-A^is the commit id of the parent of your branch A

...SHA1-A^您的分支的父级的提交 ID在哪里A

See the git rebaseman pagefor details.

有关详细信息,请参阅git rebase手册页

NOTE: This will rewrite history (as rebase always does). Special consideration should be made if your Abranch was ever pushed to a public repo.

注意:这将重写历史记录(就像 rebase 一样)。如果您的A分支机构曾经被推送到公共回购,则应特别考虑。

回答by Vladyslav Savchenko

I usually use this sequence and find it the simplest way:

我通常使用这个序列并找到最简单的方法:

git checkout B
git branch -f A B

回答by kubi

Delete the branch both locally and remotely, recreate the branch, push the branch back up to the server.

在本地和远程删除分支,重新创建分支,将分支推送回服务器。

git branch -d A
git push origin :heads/A
git branch B A
git push origin A:A

Alternately you can use the following command to undo that last commit.

或者,您可以使用以下命令撤消最后一次提交。

git revert c4

Which will make your timeline look like:

这将使您的时间线看起来像:

c1--c2--c3--(B)
             \
              c4--(C)
               \
                (^c4)--(A)

where (^c4)is a commit that undoes c4

(^c4)撤消的提交在哪里c4

I don't recommend using rebaseor reverton a branch that has been pushed to a remote repo, they can cause tons of trouble for you or anyone else using that repo.

我不建议在已推送到远程存储库的分支上使用rebaserevert,它们会给您或使用该存储库的任何其他人带来大量麻烦。