Reputation: 3016
I've been working on my local repository and pushing to the git server several times while the server I deploy my work on is still behind all those commits.
Shortly, the situation is like this:
local: commit n+5
git server: commit n+5
dev server: commit n
Now I want to pull only the commit number n+5 to the dev server (without including the commits n+1, n+2, n+3 and n+4).
I googled and found some solutions, including this one on Stackoverflow.
I tried git reset --hard <commit>
but it didn't work, I got this error:
git status
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
git reset --hard 77de3a5
fatal: ambiguous argument '77de3a5': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
I wanted to try the other proposed solution (using git fetch
and git merge
) but I got confused when it was said
Note: git merge will get all the changes in and its ancestry, so this works if is the oldest non-merged commit.
I am afraid that this approach would include (pull/merge) all the commits that are before the commit n+5.
Is there any way to pull just one commit (without including the previous commits)?
PS: I know the cleanest way is to use different branches, but what to do when you get a bit messy!
Upvotes: 1
Views: 499
Reputation: 141
Sure, to fetch exactly one commit:
git fetch [remote] [branch/commit] --depth=1
That isolated commit can then be referenced as FETCH_HEAD. I've done this frequently when updating shallow submodules, but in other cases having an isolated history-less commit may not be useful.
As for the reset --hard error, that failed because you had not done a git fetch and the server did not yet know that 77de3a5 existed.
If you are simply trying to keep the dev server history cleaner you might instead want:
git fetch
git merge --squash [commit#]
Alternatively, if you are rather trying to avoid accidentally introducing superfluous merge commits:
git fetch
git merge --ff-only [commit#]
Finally if you only want the changes introduced by that one commit
git fetch
git cherry-pick [commit#]
Upvotes: 1