代码之家  ›  专栏  ›  技术社区  ›  Paul Tarjan

拆分文件时保留git历史记录

  •  30
  • Paul Tarjan  · 技术社区  · 15 年前

    我想把一个函数从一个文件中取出,放到另一个文件中,但要保留责备的历史记录。

    cp a.php b.php
    
    vim b.php
    # delete everything but 1 function
    
    vim a.php
    # delete the 1 function
    
    git add a.php b.php
    git commit
    

    git blame b.php 我只觉得这是把责任归咎于这个新的承诺。

    4 回复  |  直到 15 年前
        1
  •  6
  •   Community Mohan Dere    9 年前

    也许前面的这个问题可以提供信息:

    How does git track source code moved between files?

    事后 通过检查从提交到提交的整个存储库的状态。

        2
  •  28
  •   vine77    10 年前

    移动 在进行任何编辑之前先提交。根据我的经验 git blame -C 选择。因此,在将文件拆分为新文件的情况下,可以通过两次提交来完成:

    1. 将原件复制到新目的地,确保删除原件
    2. 从复制的文件中删除多余的部分

    cp a.php b.php
    mv a.php c.php
    git add a.php b.php c.php
    git commit
    vim b.php  # delete everything but 1 function
    vim c.php  # delete the 1 function
    git add b.php c.php
    git commit
    
        3
  •  5
  •   max    15 年前

    尝试 git blame -C -C b.php

        4
  •  4
  •   Lukas Eder    7 年前

    Peter's answer to another question here git-split.sh :

    #!/bin/sh
    
    if [[ $# -ne 2 ]] ; then
      echo "Usage: git-split.sh original copy"
      exit 0
    fi
    
    git mv $1 $2
    git commit -n -m "Split history $1 to $2"
    REV=`git rev-parse HEAD`
    git reset --hard HEAD^
    git mv $1 temp
    git commit -n -m "Split history $1 to $2"
    git merge $REV
    git commit -a -n -m "Split history $1 to $2"
    git mv temp $1
    git commit -n -m "Split history $1 to $2"
    

    它只是将源文件复制到一个新文件中,并且两个文件具有相同的历史记录。解释为什么这项工作可以在 that other answer