Search code examples
gitgit-commitgit-pull

How to pull my second last or n'th last push


When I run git log, I see:

commit d0fa4410393w92ce6ede2ea26fa2fb74z2e5f1bz (HEAD -> notificationControls, origin/notificationControls)
Author: stufezic-dev <ninjahero@gmail.com>
Date:   Sat Sep 19 12:32:45 2020 +0545

    lockscreen controls working, notification updates yet to fix

commit 2e11d85350a741d6992875e949a0a77abbeb8fde (mainadjust, commitBranch)
Author: stufezic-dev <ninjahero@gmail.com>
Date:   Tue Sep 15 23:30:27 2020 +0545

    notification and lockscreen controls using mediaSession: initiated  
  
commit 2e11d85350a741d6992875e949a0a77abbeb8fde (mainadjust, commitBranch)
Author: stufezic-dev <ninjahero@gmail.com>
Date:   Tue Sep 14 23:30:27 2020 +0545

    notification and lockscreen controls using mediaSession: initiated  
  
...

How do I pull the second last or the third last commit, instead of the latest commit? I had only known of 1 command: git pull master origin, and I'm thinking of how to I pull any commit instead of the latest commit.


Solution

  • git pull origin master is equivalent to git fetch origin, followed by git merge origin/master.

    If you want to merge something else than origin/master in your branch (say : the third commit before origin/master), use git fetch instead of git pull :

    git fetch
    
    # inspect the history
    git log --graph origin/master HEAD
    
    # merge whatever commit you want into your branch
    git merge eacf32