1

I need to do a simple thing but I just can't find the right way...

Me and other developer are working with the same GIT branch on a cloud repository. He added some file with code that is not well formatted and committed locally but didn't push to the cloud.

I want to get those files by connecting directly to his .git folder by using git pull. I know that in this stage GIT will create a remote branch for me and a local branch to hold this files.

What I can't understand is:

  1. Can I pull only specific files?

  2. How can I add this files to the main branch I'm working on' edit them and then push them to the server?

BTW: I guess that the right way will be to create a temporary branch, push from my friend machine to it, pull on my machine and then push it to the cloud but in my workplace committing not well formatted code is problematic...

Stefan
  • 109,145
  • 14
  • 143
  • 218
Roy Tsabari
  • 2,000
  • 6
  • 26
  • 41

1 Answers1

1

Assuming you can connect to his machine, and git clone his local repository, you'll want to fetch his branches and create a local tracking branch. It sounds like you've got this bit covered, but for posterity:

git remote add <repo> <his_repo_path>

You can't pull just specific files. What you can do is call your version of his branch something unique, and just cherry-pick commits you want to end up on your server:

git fetch <repo> <his_branch>:throwaway ;# this would call your branch 'throwaway'

Suppose his history (i.e. the history of throwaway) looked like A-B-C-D-E-F. If you just wanted commits B and D, you can grab them into the main branch with cherry-pick:

git checkout <main_branch> ;# checkout your main branch
git cherry-pick `B`
git cherry-pick `D`

And you're done.

There's also a "quick and dirty" solution I don't recommend because it's bad practice, but it gets the job done if you really just need one or two files, and you're pulling from someone who doesn't make intelligently-sized commits:

  1. git checkout -B trashme master ;# or whatever your main branch is
  2. Copy the files over. Literally make copies or have them sent to you via email.
  3. Move them into the repo while the trashme branch is checked out.
  4. Edit away before staging and committing them.
  5. Either cherry-pick those commits or git merge trashme.
Christopher
  • 42,720
  • 11
  • 81
  • 99