2017-02-10 157 views
0

如何檢出分支的提交版本git。例如,我的分支Dev有一個提交ad4f43af43e。我怎樣才能檢查出提交?不只是一個文件,而是整個分支。我在網上搜索,發現:git checkout <commit>,但它沒有指定分支名稱Git如何結帳分支提交

謝謝。

+2

分支只是一個指向提交的指針。通過SHA1檢出提交,然後直接將HEAD指向該提交。我建議你閱讀https://git-scm.com/book/en/v2/Git-Branching-Branches-in-a-Nutshell – rom1v

回答

0

您可以將checkout改爲commit-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 
+0

,當你說創建分支'功能',分支是在遠程或本地? – user1615666

+0

@ user1615666:* all * Git分支是本地的。 (即使你的「遠程追蹤分支」,比如'origin/master',實際上也是本地的!當你運行'git fetch'時,它們會自動*更新*以匹配'origin'。把'fetch'想象爲「Tell我的Git通過互聯網電話調用另一個Git,並獲得其他Git爲我的Git所做的任何更新。「同時'git push'是」讓我的Git調用其他Git,而不是*獲取*東西,*給他們的東西,並要求他們更新他們的分支。「) – torek

0

如果你正在尋找從一個特定的分支出來犯的一個分支,首先要確定你是在分支,

git checkout dev 

現在我想籤一個特定的從呈交123654 dev分支到一個新的分支,同時保持頭部在主分支上。

git checkout -b new-branch 123654 
0
git checkout <hash> # non named commit 
git checkout <branch_name> # named commit 

兩行以上將放置在給定的提交頭指針。您應該知道分支名稱是一個提交,除非您在該分支上添加新的提交時它可能會發展。

如果要放置您分支Dev在提交ad4f43af43e你可以做到這一點

git branch -f Dev ad4f43af43e 

Becareful!這很危險,因爲你可能會丟失提交