1

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!

Community
  • 1
  • 1
singrium
  • 2,746
  • 5
  • 32
  • 45
  • 2
    I think you should read what `git cherry-pick` is all about. `git help cherry-pick` – eftshift0 Nov 15 '19 at 17:38
  • @eftshift0 I tried `git cherry-pick -n 77de3a5`, it didn't work too, I got: `fatal: bad revision '77de3a5'` – singrium Nov 15 '19 at 17:40
  • pulling last commit will automatically align your files with all the changes done so far – Raphael Nov 15 '19 at 17:41
  • I would guess you are providing the wrong revision id to cherry-pick. Check the id you are using. What does `git show --summary 77de3a5` show? – eftshift0 Nov 15 '19 at 17:42
  • 1
    You probably have to do a `git fetch --all` on the deploy server to pull in the new information from the remote. Then you can retry the cherry-pick. – erik258 Nov 15 '19 at 17:46
  • @eftshift0, on my local machine (where it is up to date with the git repository) it shows the author of the commit, the commit message ... But on the dev server (which is not up to date with the git server), it shows `fatal: ambiguous argument '77de3a5': unknown revision or path not in the working tree.` – singrium Nov 15 '19 at 17:47
  • @DanielFarrell, I'll try that. Thanks – singrium Nov 15 '19 at 17:47

1 Answers1

1

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#]
Mr Blue
  • 131
  • 5