Git pull just one commit
Assuming your remote is origin
and your branch is master
,
git fetch && git merge $(git rev-list master..origin/master | tail -n1)
This will:
- Run
git fetch
to updateorigin/master
- Get the list of commit hashes (
git rev-list
) from aftermaster
up toorigin/master
, and get the last line (tail
), i.e., the first new commit - Pass the result of the last step to
git merge
git pull
is essentially a shorthand for git fetch
(download remote commits into remote-tracking branches) and then git merge
(merge your HEAD
, i.e. the current commit, with the ones you just downloaded).
You can get the flexibility you expect by decoupling both steps:
First, run
git fetch
, then inspect the history you just downloaded (if you work on themaster
branch,git fetch
should have downloaded remote commits in branchorigin/master
which you can inspect withgit log origin/master
).Then, merge the commit you want using
git merge <commit>
. Note:git merge
will get all the changes in<commit>
and its ancestry, so this works if<commit>
is the oldest non-merged commit. If you do not have any unpushed commits, this will "fast-forward", i.e. it won't create a new commit but just advanceHEAD
to this commit. If you're not happy withgit merge
, you have other options likegit rebase
,git cherry-pick
, ...