簡體   English   中英

Git 拉直到特定的提交

[英]Git pull till a particular commit

我想做一個git pull但只到一個特定的提交。

 A->B->C->D->E->F (Remote master HEAD)

所以假設我local master HEAD 指向B ,我想拉到E 我該怎么辦 ?

這不是拉特定的提交,而是拉到特定的提交。

git pull只不過是git fetch然后是git merge 所以你能做的是

git fetch remote example_branch

git merge <commit_hash>

首先,從遠程倉庫獲取最新的提交。 這不會影響您當地的分支機構。

git fetch origin

然后簽出遠程跟蹤分支並執行 git log 以查看提交

git checkout origin/master
git log

獲取您要合並的提交的提交哈希(或只是它的前 5 個字符)並將該提交合並到 master

git checkout master
git merge <commit hash>

您還可以拉取最新的提交並撤消直到您想要的提交:

git pull origin master
git reset --hard HEAD~1

用你想要的分支替換master

使用 git log 來查看您要還原到哪個提交:

git log

就個人而言,這對我更有效。

基本上,它的作用是拉取最新的提交,然后您手動一一還原提交。 使用 git log 來查看提交歷史。

優點:如宣傳的那樣工作。 您不必使用提交哈希或拉取不需要的分支。

缺點:您需要一次還原提交。

警告:提交/存儲所有本地更改,因為使用--hard您將丟失它們。 使用風險自負!

這對我有用:

git pull origin <sha>

例如

[dbn src]$ git fetch
[dbn src]$ git status
On branch current_feature
Your branch and 'origin/master' have diverged,
and have 2 and 7 different commits each, respectively.
...
[dbn src]$ git log -3 --pretty=oneline origin/master
f4d10ad2a5eda447bea53fed0b421106dbecea66 CASE-ID1: some descriptive msg
28eb00a42e682e32bdc92e5753a4a9c315f62b42 CASE-ID2: I'm so good at writing commit titles
ff39e46b18a66b21bc1eed81a0974e5c7de6a3e5 CASE-ID2: woooooo
[dbn src]$ git pull origin 28eb00a42e682e32bdc92e5753a4a9c315f62b42
[dbn src]$ git status
On branch current_feature
Your branch and 'origin/master' have diverged,
and have 2 and 1 different commits each, respectively.
...

這會拉取 28eb00、ff39e4 和之前的所有內容,但不會拉取 f4d10ad。 它允許使用 pull --rebase,並尊重 gitconfig 中的 pull 設置。 這是有效的,因為您基本上將 28eb00 視為一個分支。

對於我使用的 git 版本,此方法需要完整的提交哈希 - 不允許使用縮寫或別名。 你可以這樣做:

[dbn src]$ git pull origin `git rev-parse origin/master^`

如果您將提交合並到您的分支中,您應該獲得所有的歷史記錄。

觀察:

$ git init ./
Initialized empty Git repository in /Users/dfarrell/git/demo/.git/
$ echo 'a' > letter
$ git add letter
$ git commit -m 'Initial Letter'
[master (root-commit) 6e59e76] Initial Letter
 1 file changed, 1 insertion(+)
 create mode 100644 letter
$ echo 'b' >> letter
$ git add letter && git commit -m 'Adding letter'
[master 7126e6d] Adding letter
 1 file changed, 1 insertion(+)
$ echo 'c' >> letter; git add letter && git commit -m 'Adding letter'
[master f2458be] Adding letter
 1 file changed, 1 insertion(+)
$ echo 'd' >> letter; git add letter && git commit -m 'Adding letter'
[master 7f77979] Adding letter
 1 file changed, 1 insertion(+)
$ echo 'e' >> letter; git add letter && git commit -m 'Adding letter'
[master 790eade] Adding letter
 1 file changed, 1 insertion(+)
$ git log
commit 790eade367b0d8ab8146596cd717c25fd895302a
Author: Dan Farrell 
Date:   Thu Jul 16 14:21:26 2015 -0500

    Adding letter

commit 7f77979efd17f277b4be695c559c1383d2fc2f27
Author: Dan Farrell 
Date:   Thu Jul 16 14:21:24 2015 -0500

    Adding letter

commit f2458bea7780bf09fe643095dbae95cf97357ccc
Author: Dan Farrell 
Date:   Thu Jul 16 14:21:19 2015 -0500

    Adding letter

commit 7126e6dcb9c28ac60cb86ae40fb358350d0c5fad
Author: Dan Farrell 
Date:   Thu Jul 16 14:20:52 2015 -0500

    Adding letter

commit 6e59e7650314112fb80097d7d3803c964b3656f0
Author: Dan Farrell 
Date:   Thu Jul 16 14:20:33 2015 -0500

    Initial Letter
$ git checkout 6e59e7650314112fb80097d7d3803c964b3656f
$ git checkout 7126e6dcb9c28ac60cb86ae40fb358350d0c5fad
Note: checking out '7126e6dcb9c28ac60cb86ae40fb358350d0c5fad'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at 7126e6d... Adding letter
$ git checkout -b B 7126e6dcb9c28ac60cb86ae40fb358350d0c5fad
Switched to a new branch 'B'
$ git pull 790eade367b0d8ab8146596cd717c25fd895302a
fatal: '790eade367b0d8ab8146596cd717c25fd895302a' does not appear to be a git repository
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
$ git merge 7f77979efd17f277b4be695c559c1383d2fc2f27
Updating 7126e6d..7f77979
Fast-forward
 letter | 2 ++
 1 file changed, 2 insertions(+)
$ cat letter
a
b
c
d

我從這個視頻中找到了更新的答案,接受的答案對我不起作用。

首先使用git clone <HTTPs link of the project> (或使用SSH)從git(如果還沒有)克隆最新的repo,然后使用git checkout <branch name>轉到git checkout <branch name>

使用命令

git log

檢查最新的提交。 復制特定提交的 shal。 然后使用命令

git fetch origin <Copy paste the shal here>

按回車鍵后。 現在使用命令

git checkout FETCH_HEAD

現在特定的提交將可用於您的本地。 更改任何內容並使用git push origin <branch name>推送代碼。 就這樣。 檢查視頻以供參考。

簡單明了

git pull origin <Commit-hash>

git log查看本地分支和其他分支的區別

  git log
  git merge cuY2324X

然后git merge到特定或特定的提交,通過檢出到其他你想要將代碼推送到特定提交的分支並使用至少 6 位提交

如果要將特定提交的代碼拉到新分支,可以使用以下命令:

git checkout -b <new_branch_name> <commit_hash>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM