12

I have made two commits in my git repository and push them to my git server

the two commits are

  • In first commit file A is committed
  • In second commit file B is committed

now on the other development server I want to pull only the first commit or FILE A from git server. How to do this ?

Dau
  • 8,578
  • 4
  • 23
  • 48

2 Answers2

21

First, on your development server, you'll need to fetch the list of commits from the git server like this:

git fetch origin master (or whatever branch you need)

Then there are a few options to achieve what you want:

Cherry pick the first commit - this simply 'plucks' the chosen commit from another branch/repo and applies it to your current local branch. It can be very useful, but should be used with caution (see below).

git cherry-pick  <hash-of-commit-you-want>

In this particular case, you could do

git cherry-pick FETCH_HEAD^ (gets commit before the HEAD of what's been fetched)

Or, pull everything and then do a hard reset to the commit you want (in this case the one just before HEAD). A hard reset effectively rewinds your local branch back in time to the chosen commit, changing the state of all files to how they were at that time (so in this case File B would either be deleted, or go back to how it was before either commit, depending on whether or not it previously existed).

git pull
git reset --hard HEAD^ (or git reset --hard <hash-of-commit-you-want>)

I would prefer the second option as cherry-picking can have some knock on effects if you're not careful with it. I believe it creates a new hash for the commit, so the cherry-picked commit and the original commit aren't identical. I'm afraid I don't have time right now to read up on this and confirm exactly what the pitfalls are, but I would strongly recommend that you investigate it for yourself if you decide to use it.

EDIT - Another solution (given that this is a live server and that it's not acceptable for File B to appear on the server at any point) is to do the following:

git fetch origin master
git checkout FETCH_HEAD^

This fetches all commits from the repo, and then checkes out your local repo to the commit before the HEAD of what was fetched. The only downside here is that you will then be in 'detached head' state and will have to create a new branch locally, but that's not a big problem.

Will Pragnell
  • 3,073
  • 1
  • 20
  • 21
  • thanx for the answer, I think your first option is correct to me, I am also thinking about choosing second option when I ask the question but didn't choose because I am thinking it as a wrong way. btw let me check – Dau Jan 19 '12 at 11:36
  • a doubt in my mind is if we 'fetch origin master then it will immediately make affect on the code' and this is the live server on which I am going to take the pull. so the file B will also appear on server for some time? – Dau Jan 20 '12 at 05:46
  • Ah, I see! I didn't realise that this was a live server. You are correct. In that case you should cherry-pick the commit you want. Or you can probably just checkout that branch up to the specific commit that you want. – Will Pragnell Jan 20 '12 at 10:06
  • but how can I `cherry-pick` the commit is not on the server I have to pull it on server; so I make the title of question pull upto a specific commit – Dau Jan 20 '12 at 10:10
  • this is why you do the fetch - this loads the commits onto the server without applying them to your working tree. – Will Pragnell Jan 20 '12 at 10:34
  • git fetch origin master && git cherry-pick FETCH_HEAD^ will do it - though the warnings about cherry-picking creating a new commit still apply – Will Pragnell Jan 20 '12 at 10:36
  • its not working for me; but for your valuable time I voted it – Dau Jan 20 '12 at 11:28
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/6888/discussion-between-will-pragnell-and-dau) – Will Pragnell Jan 20 '12 at 11:37
1

This doesnt make to much sense - if you commited the files you have them in your repo anyway.

Maybe this is what you want

git checkout -- FileAOnly

Or

git checkout origin/master -- FileAonly
Adrian Cornish
  • 23,227
  • 13
  • 61
  • 77
  • 1
    checkout used to retrieve files from the old commits in your local system. but my question is how to pull specific commit/file from git server – Dau Jan 19 '12 at 06:54
  • Edited answer (+2 more chars) – Adrian Cornish Jan 19 '12 at 06:57
  • can origin/master use with checkout? – Dau Jan 19 '12 at 07:03
  • 1
    @Dau: Git doesn't have a concept of "old commits". It just has commits which represent all the files at that point. Humans frequently call the tip of the branch as 'new', or 'current', and all other commits as "old", but this is a human meaning. – Arafangion Jan 19 '12 at 07:31
  • @Arafangion I know about it, but what the solution of my problem to pull specific commit? – Dau Jan 19 '12 at 08:43