1

How to merge old commit code into HEAD? If I want to merge the complete file of test.js at commit hash e123ee12 in to HEAD test.js.

I try to using cherry-pick but if there are many commit before I need to cherry-pick one by one.

I also try to checkout to the e123ee12 and add a empty line then commit and checkout back to master and merge the commit.But it will only merge the line that I add,will not merge all line to the master.

How to merge all file at old commit into master?

Chien Lee
  • 57
  • 7
  • 2
    Possible duplicate of [Reset or revert a specific file to a specific revision using Git?](https://stackoverflow.com/questions/215718/reset-or-revert-a-specific-file-to-a-specific-revision-using-git) – Andrew C Oct 09 '17 at 04:32
  • It's different. – Chien Lee Oct 09 '17 at 06:12
  • @ChienLee: How is that duplicate different? I see exactly the same question... – rodrigo Oct 09 '17 at 06:14
  • The possible duplicate question is about revert and reset,but this is about pick old commit and merge into current commit. – Chien Lee Oct 09 '17 at 07:04

1 Answers1

1

You could try and generate a patch from that old commit (including for a single file)

 git format-patch -1 <sha> -- aFile

Then apply it to your current HEAD

  git am -3 < file.patch

Note: for multiple commits, you can cherry-pick a range of commits.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • why it `format-patch -1` then `git am -3` not `git am -1` – Chien Lee Oct 09 '17 at 06:29
  • @ChienLee the -3 if for doing a merge three-way and has nothing to do with the -1 (patch on one commit only): `-`: Prepare patches from the topmost `` commits. – VonC Oct 09 '17 at 06:30
  • I was doing the following `git format-patch -1 e123ee12` then it generate `0001-.patch` then `git checkout master` then `git am -3 0001-.patch` – Chien Lee Oct 09 '17 at 06:38
  • I create another test git folder and it works but still not work for the original project(no line change after apply),still try to figure out. – Chien Lee Oct 09 '17 at 07:06
  • @ChienLee OK. Maybe it considers the history of that file in the original project and thinks that patch was *already* applied... – VonC Oct 09 '17 at 07:09
  • Do you know how to avoid it to consider the history. – Chien Lee Oct 09 '17 at 07:19
  • @ChienLee not sure. May be with a shallow clone? – VonC Oct 09 '17 at 07:27
  • try to `git clone --depth `and do `am` again but still get `Falling back to patching base and 3-way merge... No changes -- Patch already applied.` – Chien Lee Oct 09 '17 at 07:43