1

I have a git repository, currently on master branch. The last commit (f3d8237 has about a dozen files (all modified). Most of these files are different from their fellows in the working directory.

What I want to do is this: for each file in f3d8237, replace the corresponding file in the working tree with this file.

I have been swimming through SO for this specific scenario. This answer: https://stackoverflow.com/a/12049323/2554788 seems to capture my scenario most closely, but based on the myriad of other answers and comments on this thread, I'm intimidated to take a plunge. Don't want to make things worse!

Of course, it's probably clear I haven't even cut my git milk teeth yet.

Community
  • 1
  • 1
Ifedi Okonkwo
  • 3,406
  • 4
  • 33
  • 45
  • `git checkout -- filename` for each of those files will work, but you're looking for a one liner I guess? – Tim Jul 09 '15 at 08:08

2 Answers2

2

This should do the job:

git show --pretty="format:" --name-only f3d8237 | while read line
do 
   git checkout f3d8237 -- $line
done

As for you being worried about the changes, if not satisfied you can always revert the changes by,

git reset --hard
pratZ
  • 3,078
  • 2
  • 20
  • 29
  • Thanks for the sleek "pretty format" thing. I work on Windows PowerShell, but I'm sure I could also pipe into `foreach` and go on from there. However, I just went on to export those files using TortoiseGit, then merge them over the working directory. – Ifedi Okonkwo Jul 09 '15 at 16:26
1

Inspired by @pratZ's approach, I ended up recreating, then solving the problem using Windows PowerShell. I thought I should go ahead and post my actual solution here (since it might be helpful to others) while accepting his answer.

git show --pretty="format:" --name-only f3d8237 | foreach{ git checkout f3d8237 -- $_ }

This retains the repository HEAD at f3d8237 which was the last commit, but replaces all working copies with the corresponding file.

Ifedi Okonkwo
  • 3,406
  • 4
  • 33
  • 45