gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 00:58:21 25 4
gpt4 key购买 nike

我想从一个文件中取出一个函数并将其放入另一个文件中,但保留 blame 历史记录。

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 我只会看到它归咎于这个新的提交。

最佳答案

维护 blame 历史的一般规则是在进行任何编辑之前先进行单独的移动提交。根据我的经验,这允许 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

关于git - 拆分文件时保留 git 历史记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3887736/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com