1

So I'm on my local machine and I have url to remote git repo. What I need is to fetch from there to my local machine only files which were committed in the last commit. I.e. if only a.txt and b.txt were committed last time, I want to fetch only those two files and nothing else.

I know it sounds like a bit weird use case. Any help would be appreciated.

monitor
  • 251
  • 2
  • 9
  • I don't think you can do that. The last commit is just a delta from the one before that. You need the entire history to get the actual file itself. – Mad Physicist Sep 09 '16 at 18:31
  • Once you have the history, you can look through it with the tools git provides and figure out which files were changed. Then you can delete everything else from the working dir or do whatever else you want with that information. – Mad Physicist Sep 09 '16 at 18:33
  • That's understandable. But how to limit all that repo history just to those files changed in the last commit. Kinda pretend that repo contains only a.txt and b.txt, and simply clone that repo than? – monitor Sep 09 '16 at 18:34
  • You can't. You'd have to clone the entire thing and then do something like `git filter-branch` to rewrite the history to get rid of everything but those two files, or just delete everything from the working directory that isn't them. – Mad Physicist Sep 09 '16 at 18:38
  • Alternatively, you can just use `scp` or whatever remote copy tool you prefer to fetch the files from the server's working directory, if there is one. – Mad Physicist Sep 09 '16 at 18:41
  • Ok, thanks. Looks like grabbing everything and doing local filtering is the only way to go for me. – monitor Sep 09 '16 at 18:44
  • For now that seems like the best way. The machinery of git does not prevent something like this, there is just no script to do it that I am aware of for doing this in one step. – Mad Physicist Sep 09 '16 at 19:02
  • That being said, here's an idea: create a branch on the first commit (with no files in it), then merge a.txt and b.txt from the master branch. You could find out what a.txt and b.txt actually are from something like `git diff --name-only HEAD~ HEAD` (ripped from [here](http://stackoverflow.com/q/5096268/2988730)). – Mad Physicist Sep 09 '16 at 19:04

1 Answers1

0

first pull the branch

git pull --rebase <repo> <branch>

then reset specific commits you don't want, and remaining you would get there.

git reset --hard <commit-hash>

also if you want to get hash use for particular commit

git log

edit :

You have to get the whole repo, but when you merge it locally, it only brings in the changed files.

The important line is this: (this sets your upper level branch, so it fetches from that only if base is updated )

git remote add upstream git://github.com/original/repo.git

And then the actual fetching/merging can be done like this (two steps):

git fetch upstream master
git merge upstream/master

or in one step:

git pull upstream master

Another way is to use SourceTree : when you fetch the code, it will show you all uncommitted files, you can keep whatever you want. But, basically it will only show changed files which you want, and it does that.

Ajay P. Prajapati
  • 1,973
  • 1
  • 17
  • 34