重新定义高手头?(Redefine a master head?)

我有一个回购的提交已经从:

... C1 ... C2 ... C3 ... Cn在主分支上的... C1 ... C2 ... C3 ... Cn比方说,我只是在Cn提交点发现代码存在需要时间调试的主要问题,并且我想要一个稳定的/好主人尽快。 假设我知道C3是最后一个已知的好点。 有没有办法让我重新定义主控头为C3 ,并将Cn标记为新分支头,并说“调试”分支? (回购是远程的,但让我们说它不是公开共享的)。

I have a repo where the commits has gone from:

... C1 ... C2 ... C3 ... Cn on master branch, Let's say I found out only at Cn commit point that the code has major issues that will take time to debug, and I want to have a stable/good Master asap. Suppose I know C3 is the last known good point. Is there a way for me to redefine master head as C3, and label Cn as a new branch head, say "debug" branch? (the repo is remote, but let's say it is not publically shared).

最满意答案

概要

在Git中, master是指向给定提交的另一个分支标签。 从技术上讲,您甚至不需要名为master的分支就可以存在于存储库中。

您的两个最佳选择是git-checkout和git-reset ,具体取决于您是要移动还是丢弃历史记录。 前者是你要求的,而后者是完整的。

使用结帐

您可以通过将主分支重置为任意提交来完成您想要的操作。 例如:

# Move current master branch aside. git branch -m master debug # Create a new branch at committish. Use a branch name or a commit ID. git checkout -b master c3

使用重置

或者,如果您不关心您现有的历史记录,最快的解决方案就是将主人的头部重置为交易。 例如,假设您已经在主分支上:

git reset --hard c3

虽然这不会在主分支中保留当前的历史记录,但完整的历史记录仍然可以通过reflog访问,直到删除。

Summary

In Git, master is just another branch label that points to a given commit. Technically, you don't even need a branch named master to exist within the repository at all.

Your two best options are git-checkout and git-reset, depending on whether you want to move or discard history. The former is what you asked for, while the latter is included for completeness.

Using Checkout

You can do what you want by resetting the master branch to an arbitrary commit. For example:

# Move current master branch aside. git branch -m master debug # Create a new branch at committish. Use a branch name or a commit ID. git checkout -b master c3

Using Reset

Alternatively, if you don't care about your existing history, the fastest solution is just to reset the HEAD of master to a committish. For example, assuming you are already on the master branch:

git reset --hard c3

While this won't leave your current history available in the master branch, the full history will still be accessible via the reflog until pruned.

更多推荐