Copy file and its entire history

后端 未结 6 1489
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-03 06:40

Myself and another developer are developing an API accessed by other code. As we change the behaviours of the API to better suit our needs, we release additional versions of

6条回答
  •  误落风尘
    2021-01-03 07:22

    This is a goofy hack but it seems to come close to the behavior you want. Note, it assumes that you have tagged the earliest commit of 0.5.php you care about as first:

    1. branch

      % git checkout -b tmp

    2. make a patch folder and patch versions of your 0.5.php file's commit history

      % mkdir patches && git format-patch first 0.5.php -o patches

    3. delete your file and checkout the first copy of it

      % rm 0.5.php && git checkout first -- 0.5.php

    4. rename your file

      % mv 0.5.php 0.6.php

    5. tweak your patch files to use the new name

      % sed 's/0\.5\.php/0\.6\.php/g' -i patches/0*

    6. commit (if you haven't already a couple of times)

      % git add -A && git commit -m'ready for history transfer'

    7. apply the patches

      % git am -s patches/0*

    8. go back to master, pull the new file over and delete the tmp branch

      % git co master && git co tmp -- 0.6.php && git branch -D tmp

    Voila! You're 0.6.php file now has a history that replicates your 0.5.php file's except each commit in 0.6.php's history will have a unique id from those in 0.5.php's history. Times and blame should still be correct. With a little extra effort you could probably put all of that in a script and then alias the script to git cp.

提交回复
热议问题