如何将我的 git 存储库切换到特定的提交

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

How can I switch my git repository to a particular commit

git

提问by michael

In my git repository, I made 5 commits, like below in my git log:

在我的 git 存储库中,我进行了 5 次提交,如下所示在我的 git 日志中:

commit 4f8b120cdafecc5144d7cdae472c36ec80315fdc
Author: Michael 
Date:   Fri Feb 4 15:26:38 2011 -0800

commit b688d46f55db1bc304f7f689a065331fc1715079
Author: Michael
Date:   Mon Jan 31 10:37:42 2011 -0800

commit b364f9dcec3b0d52666c4f03eb5f6efb7e1e7bda
Author: Michael
Date:   Wed Jan 26 13:33:17 2011 -0800

commit 4771e26619b9acba3f059b491c6c6d70115e696c
Author: Michael 
Date:   Wed Jan 26 11:16:51 2011 -0800

commit 6e559cb951b9bfa14243b925c1972a1bd2586d59
Author: Michael 
Date:   Fri Jan 21 11:42:27 2011 -0800

How can I roll back my previous 4 commits locally in a branch? In other words, how can I create a branch without my latest 4 commits (assume I have the SHA of that commit from git log)?

如何在分支中本地回滚我之前的 4 次提交?换句话说,如何在没有最近 4 次提交的情况下创建分支(假设我从 git log 获得了该提交的 SHA)?

回答by Artefacto

To create a new branch (locally):

创建一个新分支(本地):

  • With the commit hash (or part of it)

    git checkout -b new_branch 6e559cb
    
  • or to go back 4 commits from HEAD

    git checkout -b new_branch HEAD~4
    
  • 使用提交哈希(或其中的一部分)

    git checkout -b new_branch 6e559cb
    
  • 或者从 HEAD 返回 4 个提交

    git checkout -b new_branch HEAD~4
    

Once your new branch is created (locally), you might want to replicate this change on a remote of the same name: How can I push my changes to a remote branch

创建新分支(本地)后,您可能希望在同名远程复制此更改:How can I push my changes to a remote branch



For discarding the last three commits, see Lunaryorn's answer below.

丢弃最后三个提交,请参阅下面的 Lunaryorn 的回答



For moving your current branch HEAD to the specified commit without creating a new branch, see Arpiagar's answer below.

在不创建新分支的情况下将当前分支 HEAD 移动到指定的提交,请参阅下面的 Arpiagar 答案

回答by arpiagar

All the above commands create a new branch and with the latest commit being the one specified in the command, but just in case you want to your current branch HEAD to move to the specified commit, below is the command

以上所有命令都创建了一个新分支,并且最新的提交是命令中指定的那个,但以防万一您希望当前分支 HEAD 移动到指定的提交,下面是命令

 git checkout <commit_hash>

It detachesand point the HEAD to specified commit and saves from creating a new branch when the user just wants to view the branch state till that particular commit.

当用户只想查看分支状态直到该特定提交时,它会分离并将 HEAD 指向指定的提交并避免创建新分支。



You then might want to go back to the latest commit & fix the detached HEAD:

然后你可能想回到最新的提交并修复分离的 HEAD:

Fix a Git detached head?

修复一个 Git 分离头?

回答by lunaryorn

If you want to throw the latest four commits away, use:

如果您想丢弃最新的四次提交,请使用:

git reset --hard HEAD^^^^

Alternatively, you can specify the hash of a commit you want to reset to:

或者,您可以指定要重置为的提交的哈希值:

git reset --hard 6e559cb

回答by KingCrunch

Just checkout the commit you wants your new branch start from and create a new branch

只需签出您希望新分支开始的提交并创建一个新分支

git checkout -b newbranch 6e559cb95