复制文件及其整个历史记录 [英] Copy file and its entire history

查看:171
本文介绍了复制文件及其整个历史记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我和另一位开发人员正在开发一个由其他代码访问的API。当我们更改API的行为以更好地满足我们的需求时,我们发布了API的其他版本,而不用贬低旧版本,以便使用API​​的现有应用程序不必立即更新。例如:

  $ cat 0.5.php 
<?php
$ username = $ _POST [ '名称'];
?>

$ cat 0.6.php
<?php
$ username = $ _POST ['username'];
?>

当我们开始一个新版本时,通常我们会 cp 版本N-1.php到N.php和代码。但是,如果我们使用Git来做到这一点,那么我们会从文件中丢失整个 blame diff 和其他历史记录用于比较和恢复。 如何将旧文件的历史记录伪装成新文件,使得 blame log diff ,并且这样的命令只是工作而不向他们展示额外的标志或参数,如 - 遵循 这是一个愚蠢的黑客攻击,但它似乎接近你想要的行为。请注意,它假设您已经为您关注的0.5.php的最早提交标记为 first


  1. branch



    %git checkout -b tmp

    li>
  2. 创建0.5.php文件的提交历史记录的补丁文件夹和补丁版本

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


  3. 删除文件并签出第一个副本



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


  4. 重命名文件

    %mv 0.5.php 0.6.php


  5. 调整您的补丁文件以使用新名称



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


  6. 提交(如果您还没有几次)

    %git add -A&& git commit -m'已准备好转换历史记录'


  7. 应用补丁程序

    %git am -s patches / 0 *


  8. 新文件并删除 tmp 分支



    %git co master&& git co tmp - 0.6.php&& git分支-D tmp


瞧!你是0.6.php文件现在有一个复制0.5.php文件的历史记录,除了0.6.php历史记录中的每个提交都会有0.5.php历史记录中的唯一编号。时代和责备应该是正确的。稍加额外的努力,你可能会把所有这些都放到脚本中,然后将脚本别名为 git cp


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 the API without deprecating the old versions so that extant applications using the API will not have to update immediately. For example:

$ cat 0.5.php
<?php
$username = $_POST['name'];
?>

$ cat 0.6.php
<?php
$username = $_POST['username'];
?>

When we start a new version, typically we will cp version N-1.php to N.php and code from there. However, if we do this with Git, then we lose the entire blame, diff, and other histories from the file for comparison and reverting. How can I 'forge' this history of the old file into the new file such that blame, log, diff, and such commands "just work" without presenting to them additional flags or arguments such as --follow?

解决方案

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.

这篇关于复制文件及其整个历史记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆