2

I've got a code work flow dependent on pulling changes from a repo clones on dev machines into a repo master, then updating copies of that repo master on prod machines using something like a "git fetch" followed by a "git merge."

I've run into an issue where I've committed things to master, but due to a code freeze those changes were never pulled into the prod machines. To get out of the code freeze, I need to update a single file that's also been commited to master.

At this point, the prod machines are all behind the master repo by something like 10 commits. It is only the most recent commit that I want to pull into the prod machines. A "git fetch && git merge" is going to fast-forward my whole repo when all I need is a single file fast-forwarded.

What's the best way to cherry-pick that single file? If I "git checkout file.ext" then it updates from the local repo copy and not the updated master. I'd like to get my local copy to "know" about the most recent version of the file without updating everything else.

My idea was to do a "git fetch" to update the repo and then a "git checkout file.ext" to update just one file. Is that the best way of doing what I want to do? If not, can someone please advise me on a way to checkout a single updated file from a master that's behind on many Git commits?

buley
  • 28,032
  • 17
  • 85
  • 106

3 Answers3

3

Try something like

git fetch
git checkout master -- src/your/file.txt
madlep
  • 47,370
  • 7
  • 42
  • 53
1

you just need to reference the file after the commit hash. To get the desired hash you can run

git log remoteRepo/remotebranch

and not the hash of the commit which contains the version of the single file you want to merge into your local repository. And after you have this hash instead of running git fetch && git merge you should run:

git fetch && git checkout <HASH> <PATH_TO_SINGLE_FILE>

now you should have the updated file in your working branch.

regards, René

Rene Groeschke
  • 27,999
  • 10
  • 69
  • 78
0

Note : this solution does not use any of git specific commands. There may be a single git command to do all this work.

Make a copy of the updated single file somewhere outside revision control.
Go to old revision (the one that production machines are on).
Copy the the updated single file back to revision control.
Checking in will create a new branch.
Checkout new branch to the production machines.

bbaja42
  • 2,099
  • 18
  • 34