简体   繁体   English

如何从 git 签出特定的提交

[英]how to checkout a specific commit from git

My code isn't working and it seems I have merged a wrong piece of code in my master branch.我的代码不起作用,似乎我在我的master分支中合并了一段错误的代码。 I am unable to pin point the issue and thus I want to pull specific commits one by one to find out which version was working just before the wrong commit.我无法确定问题所在,因此我想一一提取特定的提交,以找出在错误提交之前哪个版本正在工作。

How could I pull a specific commit?我怎么能拉一个特定的提交? Eg, On Bitbucket, I see a merge with id 9ce920d on branch test but I don't know how to pull this merge.例如,在 Bitbucket 上,我在分支test中看到一个 id 9ce920d的合并,但我不知道如何拉这个合并。

The question has wrong terminology.这个问题有错误的术语。 You cannot pull a commit, you can only checkout a commit.你不能拉一个提交,你只能签出一个提交。 Pulling a commit would defy the whole branch/commit structure saving memory.拉取提交将无视整个分支/提交结构以节省内存。 You pull all commits in a branch or repository, and if you want to check out a specific one, then well, check it out:您拉取分支或存储库中的所有提交,如果您想签出特定的提交,那么,请检查一下:

git checkout 9ce920d

You will be in headless mode (no pointer to the commit, even if you have branches pointing to them - you have to check them out explicitly!), so you may want to create a branch if you want to mess around:您将处于无头模式(没有指向提交的指针,即使您有指向它们的分支 - 您必须明确检查它们!),因此如果您想搞砸,您可能需要创建一个分支:

git checkout -B mess_around_branch

For posterity, OP actually wanted to discard all commits from the latest commit on their branch to a specific commit before.对于后代,OP 实际上想要丢弃从其分支上的最新提交到之前的特定提交的所有提交。 To do this from your branch hit:要从您的分支中执行此操作:

git reset 9ce920d

which will discard all commits, but leave the files as they were, allowing you to decide if you wish to retain some of the changes.这将丢弃所有提交,但保留文件原样,让您决定是否希望保留一些更改。 If you really want to lose everything:如果你真的想失去一切:

git reset --hard 9ce920d
git checkout <id>

should do the trick.应该做的伎俩。 Where 'id' is the specific commit you want to get.其中 'id' 是您想要获得的特定提交。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM