190

I want to do a git pull but only till a specific commit.

 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. What should I do ?

This is not pulling a specific commit, this is pulling upto a specific commit.

Ninja420
  • 3,542
  • 3
  • 22
  • 34
  • 1
    possible duplicate of [Pull a specific commit from a remote git repository](http://stackoverflow.com/questions/14872486/pull-a-specific-commit-from-a-remote-git-repository) – Dan Getz Jul 16 '15 at 19:42
  • 5
    `git fetch` and `git merge E`. – poke Jul 16 '15 at 21:49
  • I'm currently in B for example but I only want to pull C, E and F. I want to remove D. Thanks! – Frenkey Sep 06 '19 at 17:17
  • @Frenkey you can do merge the master then remove the last three commits and then cherry pick the last two. Also you can try out rebase -i for more controlled git merging – Ninja420 Sep 17 '19 at 11:11
  • what if I only want to fetch changes from C, do not include changes of A and B just C? How can I do that? – Vikas Chauhan Jan 08 '20 at 07:39
  • @VikasChauhan `git fetch` && `git merge A-1` && `git cherry-pick C` – Ninja420 Jan 08 '20 at 09:15
  • How to download the commit as zip/archive file: https://stackoverflow.com/questions/13636559/how-to-download-zip-from-github-for-a-particular-commit-sha – Monero Jeanniton Nov 16 '21 at 18:42

9 Answers9

215

git pull is nothing but git fetch followed by git merge. So what you can do is

git fetch remote example_branch

git merge <commit_hash>

unrealsoul007
  • 3,809
  • 1
  • 17
  • 33
  • What is the "Refer to this" supposed to be? – brettwhiteman Jul 16 '15 at 21:43
  • @Brett Forgot to post the link.. removed it entirely – unrealsoul007 Jul 17 '15 at 08:23
  • @unrealsoul007 this will merge just that one specific commit right? So to pull from B up to E, the command would be git merge right? – Wim Feijen Jun 01 '17 at 09:16
  • 11
    @WimFeijen `git merge E` will merge `E` along with all its ancestors `C` and `D` (git knows that `A` and `B` don't need to be merged because they were already in your branch). If you ONLY want the changes of `E` then you should `git cherry-pick E` instead. – neXus Jan 24 '19 at 09:57
  • Thanks guys for the answer, I was looking for the same issue. – Akash Bisariya Jun 03 '20 at 15:00
  • When you also like to update your lokal ref for the remote branch you can do: `git pull [remote] [remote-branch-or-commit]:refs/remotes/[remote]/[branch]` e.g. `git pull origin b78741a5:refs/remotes/origin/develop` would update your local ref of remote-branch named `develop` to remote commit-hash `b78741a5` – Radon8472 Feb 25 '22 at 10:13
34

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

git checkout master
git merge <commit hash>
yegor256
  • 102,010
  • 123
  • 446
  • 597
brettwhiteman
  • 4,210
  • 2
  • 29
  • 38
  • 1
    I down-voted you because this does not work as advertised. First of all, with your method "git fetch origin" it fetches all remote branches (this should be pointed out explicitly, especially for people working in projects with many and large branches). Second, with this method "git merge " does not merge your local history up to the wanted commit, if you are back in history. Example: You are locally at commit 100, you want to get up to 150, by fetching everything (with remote HEAD being at 100). – Placeholder Feb 11 '18 at 06:10
  • 1
    In case it's not obvious for anyone else, `git fetch origin` explicitly fetches _origin_. – erik258 May 01 '19 at 19:05
  • 2
    This works perfectly and is better explained than the accepted answer. – walen Jun 20 '19 at 13:03
  • 1
    Best answer, Worked Perfectly – Asfandyar Khan Nov 12 '20 at 07:16
29

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.

Use git log to see to which commit you would like to revert:

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.

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. Use at your own risk!

Placeholder
  • 4,651
  • 6
  • 33
  • 35
  • 3
    I don't know why the downvotes, this seems the cleanest way, and as soon as you want to get up to date a simple pull will do, unlike some of the other answers – Mauricio Pasquier Juan Feb 28 '19 at 14:12
  • Don't either why not more upvotes, the only downside is that you need to go to the `HEAD` before going back - and I don't what would happen if you had to resolve conflicts. But this solution allows for `git pull --rebase`, and contrary to what's said you can directly reset back to `N` commits ago with `git reset --hard HEAD~N` (i.e. for 3 commits, `git reset --hard HEAD~3`). – Stock Overflaw Jun 15 '20 at 16:29
  • 4
    You can do `git reset --hard SHA` and it will reset it to the commit you want, no need to count backwards. – Visya Dec 09 '20 at 10:51
14

This works for me:

git pull origin <sha>

e.g.

[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. It allows the use of pull --rebase, and honors pull settings in your gitconfig. This works because you're basically treating 28eb00 as a branch.

For the version of git that I'm using, this method requires a full commit hash - no abbreviations or aliases are allowed. You could do something like:

[dbn src]$ git pull origin `git rev-parse origin/master^`
dbn
  • 13,144
  • 3
  • 60
  • 86
  • In terms of the OP's question: `git pull origin E`. This has the (potentially huge) advantage that you do not pull (nor store) unwanted history. I haven't tried, but you should be able to discard that "future history" by doing `git reset --hard E` after doing the fetch and merge steps. – RJVB Oct 13 '21 at 14:02
  • This approach worked well for me. – Kirk Liemohn Dec 31 '22 at 18:59
8

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

Use the command

git log

to check the latest commits. Copy the shal of the particular commit. 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> . That's all. Check the video for reference.

TripleM
  • 1,096
  • 7
  • 20
6

If you want to pull the code of a specific commit to a new branch you can use this command:

git checkout -b <new_branch_name> <commit_hash>
Afnan Bashir
  • 79
  • 1
  • 2
1

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
erik258
  • 14,701
  • 2
  • 25
  • 31
0

git log to check the difference between local and other branch

  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

Pierre.Vriens
  • 2,117
  • 75
  • 29
  • 42
-1

Simple and straightforward

git pull origin <Commit-hash>

  • This answer is useful when you are trying to fetch a single commit and replace your local files with the changes in that commit. Any other commits or hierarchy is ignored. – Drk_alien Jun 01 '22 at 08:56