简体   繁体   English

Git如何检出分支的提交

[英]Git how to checkout a commit of a branch

How can I check out a commit version of a branch in git ?如何在git查看分支的提交版本?

For example, my branch, Dev , has a commit ad4f43af43e .例如,我的分支Dev有一个提交ad4f43af43e How can I check out that commit?我怎样才能检查那个提交? Not just a single file, but a whole branch.不仅仅是单个文件,而是整个分支。

I searched online and found: git checkout <commit> , but it didn't specify branch name我在网上搜索发现: git checkout <commit> ,但它没有指定分支名称

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.上面的两行将HEAD指针放在给定的提交上。 You should know that a branch name is a commit, except it can evolve if a new commit is added when you're on that branch.您应该知道分支名称是一次提交,除非在该分支上添加新的提交时它可以演变。

If you want to place your branch Dev on the commit ad4f43af43e you can do this如果你想把你的分支Dev放在提交ad4f43af43e你可以这样做

git branch -f Dev ad4f43af43e

Be careful!当心! This is dangerous because you may lose commits.这很危险,因为您可能会丢失提交。

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

You can checkout to the commit-sha then, 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 

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

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