如何使用 git 将一个分支重置为另一个分支?

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

How to reset a branch to another branch with git?

git

提问by danza

let's say that we have an hotfixesbranch which was created from master. we added commits to hotfixes, but those commits were not useful, so now we want to start from a fresh copy of masteragain.

假设我们有一个hotfixesmaster. 我们添加了对 的提交hotfixes,但这些提交没有用,所以现在我们想从一个新的副本开始master

to clarify better, this is the reference workflow: http://nvie.com/posts/a-successful-git-branching-model/

为了更好地澄清,这是参考工作流程:http: //nvie.com/posts/a-successful-git-branching-model/

let's also say that we pushed hotfixesto the originremote because we have an awful set up and that's the only way to test something, so we need to reset the branch also on the remote server.

假设我们推hotfixes送到origin远程是因为我们有一个糟糕的设置,这是测试某些东西的唯一方法,所以我们也需要在远程服务器上重置分支。

how to reset hotfixesto a copy of master?

如何重置hotfixes为副本master

采纳答案by Michael Wild

You mean you want to push your local masterto the remote hotfixesbranch? Like this:

您的意思是要将本地推master送到远程hotfixes分支?像这样:

git push origin +master:hotfixes

However, this requires that you are allowed to re-write the history on the remote side.

但是,这需要允许您在远程端重写历史记录。

回答by danza

this is how i did it with basic Git commands:

这就是我用基本的 Git 命令做到的:

git checkout hotfixes
git reset --hard master
git push --force origin hotfixes

of course it's important to notify everyone working on hotfixes. most likely they will have to delete their local copy and start from a fresh one. an alternative, less invasive idea is to create a new branch:

当然,重要的是要通知每个工作的人hotfixes。他们很可能不得不删除他们的本地副本并从一个新副本开始。另一种侵入性较小的想法是创建一个新分支:

git checkout master
git branch -tb hotfixes-2 # this creates branch `hotfixes-2` from a copy of `master`
git push origin HEAD # this creates `hotfixes-2` on the remote server

回答by Tuxdude

If I understood your question correctly, what you're looking for is a way to move the branch pointer of origin/hotfixesto point to the current revision of origin/master.

如果我正确理解了您的问题,那么您正在寻找的是一种将 的分支指针移动origin/hotfixes到指向origin/master.

If that be the case, these set of command should work (assuming you already have checked out hotfixesin your local git repo any time in the past):

如果是这种情况,这些命令集应该可以工作(假设您hotfixes过去任何时候都已经在本地 git 存储库中签出过):

# git branch -f does not allow modifying the currently checked out
# branch, so checkout any other branch than hotfixes
git checkout <SOME_OTHER_BRANCH_THAN_HOTFIXES>

# Move the branch pointer of hotfixes to the commit currently
# pointed by origin/master
git branch -f hotfixes origin/master

# Force push the history rewrite in the hotfixes branch
# into origin
git push -f origin hotfixes

回答by Aaron Wortham

The answers here are solid. I have needed this exact change when resetting my staging branch to master. In that case I want to both reset the origin to match master and also reset my local to match that. So here is a git alias that allows you to pass in the branch name and do both commands in one move. (It's a little dangerous)

这里的答案是可靠的。将我的暂存分支重置为 master 时,我需要这个确切的更改。在这种情况下,我既想重置原点以匹配主人,也想重置我的本地以匹配它。所以这里有一个 git 别名,它允许您传入分支名称并一次性执行两个命令。(有点危险)

reorient = "!f() { git push origin +master: && git reset --hard origin/ ; }; f"

Then use it like:

然后像这样使用它:

git reorient hotfixes

The answers above were totally correct. But this will simply allow for fewer keystrokes and a quicker turnaround! Hope it helps.

以上答案完全正确。但这只会允许更少的击键和更快的周转!希望能帮助到你。

回答by Robert Molina

Based on a few of the answers in this thread, I did the following script with a few prompts to reduce risk of messing stuff up:

基于此线程中的一些答案,我执行了以下脚本并提供了一些提示,以降低搞砸的风险:

#!/bin/bash
# Questions for loop:
for value in {1..3}
do
  # Asking if user wants to reset hotfix:
  if [ "$value" == "1" ] ;then
    echo -n "Are you sure you want to hard reset the hotfix branch (y/n)? "
    read answer
    if [ "$answer" == "${answer#[Yy]}" ] ;then
        echo 'Okay, maybe next time.'
        exit
    fi
  fi
  # Asking if user is in void:
  if [ "$value" == "2" ] ;then
    echo -n "Are you in the void branch (y/n)? "
    read answer
    if [ "$answer" == "${answer#[Yy]}" ] ;then
        echo 'You should checkout to the void branch.'
        exit
    fi
  fi
  # Asking if user has any uncommited changes:
  if [ "$value" == "3" ] ;then
    echo -n "Do you have any uncommited changes (y/n)? "
    read answer
    if [ "$answer" == "${answer#[Nn]}" ] ;then
        echo 'You should commit your changes to avoid losing them.'
        exit
    fi
  fi
done

echo 'Resetting...'
git checkout void
git branch -f hotfix origin/master
git push -f origin hotfix

100% open to any feedback to improve this script.

100% 接受任何反馈以改进此脚本。