git 如何在特定提交处创建分支点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7310177/
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
How do I make a branch point at a specific commit?
提问by Ram Rachum
In Git, I understand that a branch is a pointer to a commit.
在 Git 中,我知道分支是指向提交的指针。
How do I make a specific branch point to a specific commit? Say I want to make master
point at 1258f0d0aae...
, how do I do that?
如何使特定分支指向特定提交?说我想master
指出1258f0d0aae...
,我该怎么做?
回答by Mark Longair
You can make master
point at 1258f0d0aae
this way:
您可以master
在点1258f0d0aae
是这样的:
git checkout master
git reset --hard 1258f0d0aae
But you have to be careful about doing this. It may well rewrite the history of that branch. That would create problems if you have published it and other people are working on the branch.
但是你必须小心这样做。它很可能会改写该分支的历史。如果您发布了它并且其他人正在该分支上工作,那将会产生问题。
Also, the git reset --hard
command will throw away any uncommitted changes (i.e. those just in your working tree or the index).
此外,该git reset --hard
命令将丢弃任何未提交的更改(即仅在您的工作树或索引中的更改)。
You can also force an update to a branch with:
您还可以使用以下命令强制更新分支:
git branch -f master 1258f0d0aae
... but git won't let you do that if you're on master
at the time.
...但如果你master
当时在,git 不会让你这样做。
回答by cmaster - reinstate monica
If you are currently noton branch master, that's super easy:
如果您目前不在分支 master 上,那非常简单:
git branch -f master 1258f0d0aae
This does exactly what you want: It points master
at the given commit, and does nothing else.
这正是您想要的:它指向master
给定的提交,并且不执行任何其他操作。
If you are currently on master, you need to get into detached head state first. I'd recommend the following two command sequence:
如果您目前在 master 上,则需要先进入 detached head 状态。我推荐以下两个命令序列:
git checkout 1258f0d0aae #detach from master
git branch -f master HEAD #exactly as above
#optionally reattach to master
git checkout master
Be aware, though, that any explicit manipulation of where a branch points has the potential to leave behind commits that are no longer reachable by any branches, and thus become object to garbage collection. So, think before you type git branch -f
!
但是请注意,对分支点的任何显式操作都有可能留下任何分支不再可访问的提交,从而成为垃圾收集的对象。所以,打字前请三思git branch -f
!
This method is better than the git reset --hard
approach, as it does not destroy anything in the index or working directory.
这种方法比这种方法更好git reset --hard
,因为它不会破坏索引或工作目录中的任何内容。
回答by CharlesB
git reset --hard 1258f0d0aae
But be careful, if the descendant commits between 1258f0d0aae
and HEAD
are not referenced in other branches it'll be tedious (but not impossible) to recover them, so you'd better to create a "backup" branch at current HEAD
, checkout master
, and reset to the commit you want.
但是要小心,如果后代在其他分支之间提交1258f0d0aae
并且HEAD
没有被其他分支引用,那么恢复它们会很乏味(但并非不可能),所以你最好在 current HEAD
、 checkoutmaster
和 reset 上创建一个“备份”分支你想要的提交。
Also, be sure that you don't have uncommitted changes before a reset --hard
, they will be truly lost (no way to recover).
另外,请确保在 a 之前没有未提交的更改reset --hard
,它们将真正丢失(无法恢复)。
回答by Ivan
git branch -f <branchname> <commit>
git branch -f <分支名称> <提交>
I go with Mark Longair's solution and comments and recommend anyone reads those before acting, but I'd suggest the emphasis should be on
我采用 Mark Longair 的解决方案和评论,并建议任何人在行动前阅读这些内容,但我建议重点应该放在
git branch -f <branchname> <commit>
Here is a scenario where I have needed to do this.
这是我需要执行此操作的场景。
Scenario
设想
Develop on the wrong branch and hence need to reset it.
在错误的分支上开发,因此需要重置它。
Start Okay
开始好了
Cleanly develop and release some software.
干净地开发和发布一些软件。
Develop on wrong branch
在错误的分支上开发
Mistake: Accidentally stay on the release branch while developing further.
错误:在进一步开发时不小心停留在发布分支上。
Realize the mistake
意识到错误
"OH NO! I accidentally developed on the release branch." The workspace is maybe cluttered with half changed files that represent work-in-progress and we really don't want to touch and mess with. We'd just like git to flip a few pointers to keep track of the current state and put that release branch back how it should be.
“哦不!我不小心在发布分支上开发的。” 工作区可能堆满了代表正在进行的工作的一半更改的文件,我们真的不想接触和弄乱。我们只是想让 git 翻转一些指针来跟踪当前状态并将该发布分支放回它应该的状态。
Create a branch for the development that is up to date holding the work committed so far and switch to it.
为最新的开发创建一个分支,保存迄今为止提交的工作并切换到它。
git branch development
git checkout development
Correct the branch
纠正分支
Now we are in the problem situation and need its solution! Rectify the mistake (of taking the release branch forward with the development) and put the release branch back how it should be.
现在我们处于问题状态,需要它的解决方案!纠正错误(将发布分支与开发一起推进)并将发布分支放回应有的状态。
Correct the release branch to point back to the last real release.
更正发布分支以指向最后一个真正的发布。
git branch -f release release2
The release branch is now correct again, like this ...
发布分支现在再次正确,就像这样......
What if I pushed the mistake to a remote?
如果我将错误推送到遥控器怎么办?
git push -f <remote> <branch>
is well described in another thread, though the word "overwrite" in the title is misleading.
Force "git push" to overwrite remote files
git push -f <remote> <branch>
在另一个线程中得到了很好的描述,尽管标题中的“覆盖”一词具有误导性。
强制“git push”覆盖远程文件