Adam S
Adam S

Reputation: 9255

Is it possible to 'git pull' only the next commit?

I understand git pull will update the working branch to the tip of a remote branch that it tracks. So let's say if I do git pull, it will pull in all the latest changes. In my case, it will pull in 5 changes on top of my tree.

Is there a way to git pull only the next change? i.e., if git pull would put change 03, 04, 05, 06 and 07 on top of my tree, how do I pull only change 03? At the time of this command I do not know the commit ID of the next change it would pull in.

The tip of my tree is completely unaltered and will not have any merge conflicts and such.

Upvotes: 13

Views: 4862

Answers (3)

You can. Quite simple:

git fetch
git log --oneline --decorate --graph --all
git merge <some-commit-id>
#Repeat last line as often as you wish
#to incorporate upstream changes piecemeal.

The combination of flags to git log is really useful to get a quick overview of what has happened, so much so that I always have a shell alias defined to use this precise combination:

alias gitlog='git log --oneline --decorate --graph'

With that alias, the above sequence becomes

git fetch
gitlog --all
git merge ...
...

Upvotes: 1

Devin Rhode
Devin Rhode

Reputation: 25377

This should be possible in theory. git status shows there are 8 commits to pull, if a script could parse out the "8" and take an input (defaulting to 1) for the number of next commits you want. You could run git-pull-next 2 and it could do this:

  1. In some way, check how many new commits there are (git status works)
  2. Set to variable newCommits
  3. Also set variable, pullNextNCommits, equal to cli input, defaults to 1
  4. Run git pull
  5. Run git log
  6. Grab the commit hash that is newCommits ago, minus pullNextNCommits
  7. Store variable current_git_branch as result of git rev-parse --abbrev-ref HEAD
  8. Run git checkout <hash>
  9. delete current_git_branch locally
  10. git checkout -b current_git_branch

TADA! Not impossible! Just have to write this script...

This answer is a community wiki, feel free to flesh out the psuedocode more

There's probably a better way to do this - since a git pull merge could have issues, it'd be better to take inspiration from the first answer here. But somehow, programmatically find the next commit hash.

Upvotes: 0

Stuart Golodetz
Stuart Golodetz

Reputation: 20656

Perhaps something like this?

git fetch
git merge <commit to merge>

To find the ID of the commit you want to merge, look it up in gitk after running the fetch.

Upvotes: 16

Related Questions