简体   繁体   English

Git 拉直到特定的提交

[英]Git pull till a particular commit

I want to do a git pull but only till a specific commit.我想做一个git pull但只到一个特定的提交。

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

so suppose my local master HEAD points to B , and I want to pull till E .所以假设我local master HEAD 指向B ,我想拉到E What should I do ?我该怎么办 ?

This is not pulling a specific commit, this is pulling upto a specific commit.这不是拉特定的提交,而是拉到特定的提交。

git pull is nothing but git fetch followed by git merge . git pull只不过是git fetch然后是git merge So what you can do is所以你能做的是

git fetch remote example_branch

git merge <commit_hash>

First, fetch the latest commits from the remote repo.首先,从远程仓库获取最新的提交。 This will not affect your local branch.这不会影响您当地的分支机构。

git fetch origin

Then checkout the remote tracking branch and do a git log to see the commits然后签出远程跟踪分支并执行 git log 以查看提交

git checkout origin/master
git log

Grab the commit hash of the commit you want to merge up to (or just the first ~5 chars of it) and merge that commit into master获取您要合并的提交的提交哈希(或只是它的前 5 个字符)并将该提交合并到 master

git checkout master
git merge <commit hash>

You can also pull the latest commit and just undo until the commit you desire:您还可以拉取最新的提交并撤消直到您想要的提交:

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

Replace master with your desired branch.用你想要的分支替换master

Use git log to see to which commit you would like to revert:使用 git log 来查看您要还原到哪个提交:

git log

Personally, this has worked for me better.就个人而言,这对我更有效。

Basically, what this does is pulls the latest commit, and you manually revert commits one by one.基本上,它的作用是拉取最新的提交,然后您手动一一还原提交。 Use git log in order to see commit history.使用 git log 来查看提交历史。

Good points: Works as advertised.优点:如宣传的那样工作。 You don't have to use commit hash or pull unneeded branches.您不必使用提交哈希或拉取不需要的分支。

Bad points: You need to revert commits on by one.缺点:您需要一次还原提交。

WARNING: Commit/stash all your local changes, because with --hard you are going to lose them.警告:提交/存储所有本地更改,因为使用--hard您将丢失它们。 Use at your own risk!使用风险自负!

This works for me:这对我有用:

git pull origin <sha>

eg例如

[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.
...

This pulls 28eb00, ff39e4, and everything before, but doesn't pull f4d10ad.这会拉取 28eb00、ff39e4 和之前的所有内容,但不会拉取 f4d10ad。 It allows the use of pull --rebase, and honors pull settings in your gitconfig.它允许使用 pull --rebase,并尊重 gitconfig 中的 pull 设置。 This works because you're basically treating 28eb00 as a branch.这是有效的,因为您基本上将 28eb00 视为一个分支。

For the version of git that I'm using, this method requires a full commit hash - no abbreviations or aliases are allowed.对于我使用的 git 版本,此方法需要完整的提交哈希 - 不允许使用缩写或别名。 You could do something like:你可以这样做:

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

If you merge a commit into your branch, you should get all the history between.如果您将提交合并到您的分支中,您应该获得所有的历史记录。

Observe:观察:

$ 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

I've found the updated answer from this video , the accepted answer didn't work for me.我从这个视频中找到了更新的答案,接受的答案对我不起作用。

First clone the latest repo from git (if haven't) using git clone <HTTPs link of the project> (or using SSH) then go to the desire branch using git checkout <branch name> .首先使用git clone <HTTPs link of the project> (或使用SSH)从git(如果还没有)克隆最新的repo,然后使用git checkout <branch name>转到git checkout <branch name>

Use the command使用命令

git log

to check the latest commits.检查最新的提交。 Copy the shal of the particular commit.复制特定提交的 shal。 Then use the command然后使用命令

git fetch origin <Copy paste the shal here>

After pressing enter key.按回车键后。 Now use the command现在使用命令

git checkout FETCH_HEAD

Now the particular commit will be available to your local.现在特定的提交将可用于您的本地。 Change anything and push the code using git push origin <branch name> .更改任何内容并使用git push origin <branch name>推送代码。 That's all.就这样。 Check the video for reference.检查视频以供参考。

简单明了

git pull origin <Commit-hash>

git log to check the difference between local and other branch git log查看本地分支和其他分支的区别

  git log
  git merge cuY2324X

then git merge to specific or particular commit by checking out to other branch in which you want to push code to specific commit and use at least 6 digits of commit然后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