-
Notifications
You must be signed in to change notification settings - Fork 147
Copying or Splitting a File in Git
Peter Doak edited this page Jun 5, 2025
·
3 revisions
When a file is copied in the same directory this git mv trick
git mv foo bar
git commit
SAVED=`git rev-parse HEAD`
git reset --hard HEAD^
git mv foo copy
git commit
git merge $SAVED # This will generate conflicts
git commit -a # Trivially resolved like this
git mv copy foo
git commit
keeps git blame simple, i.e. no extra switches are required.
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.
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>
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