¿Como copiar un único fichero o directorio desde otro repositorio GIT de forma que preserves su historia?
Internet está lleno de formulas mágicas cada cual más compleja.
Aqui os propongo una mucho más simple y rápida que consiste en hacer un git format-patch para toda la historia del fichero o subdirectorio en cuestión y luego importarla en el repositorio de destino.
mkdir /tmp/mergepatchs cd ~/repo/org export reposrc=myfile.c #or mydir git format-patch -o /tmp/mergepatchs $(git log $reposrc|grep ^commit|tail -1|awk '{print $2}')^..HEAD $reposrc cd ~/repo/dest git am /tmp/mergepatchs/*.patch
Simple y rápido
Nice solution! I’m stealing this.
Now that I will be using this for the fourth time I have to say a quick thanks for the brilliant tip.
I’m using it as a little script: https://gist.github.com/voltagex/3888364
Thanks very much for this, it’s pretty useful.
This does not work if the file was made in the initial commit. To check this, run:
INITCOMMIT=$(git rev-list --parents HEAD | egrep "^[a-f0-9]{40}$")
git show $INITCOMMIT
If this is the case, replay the initial commit to get the above code to work like before:
cd ~/repo/org
INITCOMMIT=$(git rev-list --parents HEAD | egrep "^[a-f0-9]{40}$")
git format-patch -1 -o /tmp/mergepatchs ${INITCOMMIT}
cd ~/repo/dest
git am /tmp/mergepatchs/0001*.patch
I’ve been researching a bit, and it’s easier to do it this way:
git format-patch -o /tmp/mergepatchs –root
Hope it helps!
very nice and simple cosultion in comparision to others based on cloning the original repo, and then removing everything except the file in the cloned repo.
But how to do, if the file was renamed ? Can we also recover the older commits of the file when it has another name ?
Great instructions! Worked a treat! Thanks!
I cannot make it work (meaning keep the history) when the file has be moved.
My file hase 5 commits that modified it and the latest moved to to some other folder.
When applying this technic to move the file from one repo to another, the patch is only containing the latest commit.
Any idea on how to solve this issue ?
I finally got around the issue of renamed/moved files.
Basically you to know the path of the file before it changed and add it to the list of files you create the patch on.
something like
git log –pretty=email –patch-with-stat –reverse –full-index –binary — new_file_path old_file_path > /tmp/new_file_path_history.patch
easy, simple & most importantly working solution