82

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

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

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

如何重置hotfixes为副本master

4

5 回答 5

135

这就是我使用基本 Git 命令的方式:

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

当然,重要的是通知每个从事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
于 2013-04-11T08:03:38.997 回答
46

你的意思是你想把你的本地推master送到远程hotfixes分支?像这样:

git push origin +master:hotfixes

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

于 2013-04-11T07:56:16.247 回答
11

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

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

# 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
于 2013-04-11T07:57:18.887 回答
4

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

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

然后像这样使用它:

git reorient hotfixes

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

于 2018-01-23T21:55:55.587 回答
1

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

#!/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% 接受任何反馈以改进此脚本。

于 2019-11-15T17:16:22.863 回答