Skip to content

Copying or Splitting a File in Git

Peter Doak edited this page Jul 16, 2018 · 2 revisions

Maintain the history to best effort

When a file is copied in the same directory this git mv trick keeps git blame simple, i.e. no extra switches are required.

When using this trick only the "original" file gets the full log without a switch.

However if a file is moved to a different directory generally you will need to use some extra git magic to regain your log and file history.

History appears lost? What to do

Git is better than this and in general the above trick is to placate those of us that grew up with "file" based history like in SVN and CVS.

git log --follow <file>
git blame -C -M <file>

Rebasing after using the trivial merge trick

The normal interactive or automatic rebase will try to collapse the split that maintains the "copied" file history. To prevent this one must rebase preserving merges '-p'.

git rebase -p develop
git commit -a -m "something to effect of keep both sides of merge for file history"
git rebase --continue