git Git如何检出分支的提交

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

Git how to checkout a commit of a branch

git

提问by user1615666

How can I check out a commit version of a branch in git?

如何检出 中分支的提交版本git

For example, my branch, Dev, has a commit ad4f43af43e. How can I check out that commit? Not just a single file, but a whole branch.

例如,我的分支Dev,有一个 commit ad4f43af43e。我怎样才能检查那个提交?不仅仅是单个文件,而是整个分支。

I searched online and found: git checkout <commit>, but it didn't specify branch name

我在网上搜索发现:git checkout <commit>,但它没有指定分支名称

回答by smarber

git checkout <hash>   # non named commit
git checkout <branch_name>  # named commit

The two lines above will place the HEAD pointer on the given commit. You should know that a branch name is a commit, except it can evolve if you a new commit is added when you're on that branch.

上面的两行将 HEAD 指针放在给定的提交上。您应该知道分支名称是一个提交,除非您在该分支上添加了新的提交,否则它可以演变。

If you want to place your branch Devon the commit ad4f43af43eyou can do this

如果你想把你的分支Dev放在提交上,ad4f43af43e你可以这样做

git branch -f Dev ad4f43af43e

Be careful! This is dangerous because you may loose commits

当心!这很危险,因为您可能会丢失提交

回答by myselfmiqdad

If you're looking to branch out from a specific commit of a branch, first be sure you are in the branch,

如果您想从分支的特定提交中分支出来,首先要确保您在该分支中,

git checkout dev

Now I want to checkout a specific commit 123654 from the dev branch to a new branch while keeping the head on main branch.

现在我想从 dev 分支检出一个特定的提交 123654 到一个新分支,同时保持主分支的头部。

git checkout -b new-branch 123654

回答by Sajib Khan

You can checkoutto the commit-shathen, create a new branch (say, feature) from that commit.

您可以checkoutcommit-sha那时,创建一个新的分支(说feature)从提交。

$ git checkout <commit>
$ git checkout -b feature    # create a new branch named `feature` from the commit


# if you want to replace the current branch (say 'develop') with new created branch ('feature') 
$ git branch -D develop     # delete the local 'develop' branch
$ git checkout -b develop   # create a new 'develop' branch from 'feature' branch