Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep git history when splitting a file

I want to take a function out of one file and put it into another, but keep the blame history.

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 

But if I run git blame b.php I only see it blaming to this new commit.

like image 868
Paul Tarjan Avatar asked Oct 08 '10 04:10

Paul Tarjan


People also ask

Does git store history of all commits?

The blob, tree, and commits are how Git stores the complete history of your repository. It does all the references by the object hash: there is no way of manipulating the history or files tracked in the repository without breaking the relations.

How is git history stored?

Centralized systems store a separate history for each file in a repository. Git stores history as a graph of snapshots of the entire repository. These snapshots, called commits in Git, can have multiple parents, creating a history that looks like a graph instead of a straight line.

Where is git history saved?

Git stores the complete history of your files for a project in a special directory (a.k.a. a folder) called a repository, or repo. This repo is usually in a hidden folder called . git sitting next to your files.


1 Answers

The general rule to maintaining blame history is to make a separate move commit first before any edits. It has been my experience that this allows git blame to work without the need for the -C option. So in the case of splitting the file up into new files, this can be done in two commits:

  1. Duplicate the original to the new destinations, making sure to delete the original
  2. Remove the extra sections from the duplicated files

In the example provided, this would be:

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 
like image 149
vine77 Avatar answered Sep 22 '22 15:09

vine77