When I move a file in git using git-mv the status shows that the file has been renamed and even if I alter some portions it still considers to be almost the same thing (whic
2020-05-19: The following solution has the advantages of not changing the log of the original file, not creating a merge conflict, and being shorter.
You can force Git to detect the history of the copied file in three commits:
--no-ff
.Credits to Raymond Chen. What follows is his procedure. Say the file is named orig
, and you want the duplicate to be named apple
:
git checkout -b dup # create and switch to branch
git mv orig apple # make the duplicate
git commit -m "duplicate orig to apple"
git checkout HEAD~ orig # bring back the original
git commit -m "restore orig"
git checkout - # switch back to source branch
git merge --no-ff dup # merge dup into source branch
The former solution had four commits:
(Solution taken from https://stackoverflow.com/a/44036771/1389680.)
Git does not do rename tracking nor copy tracking, which means it doesn't record renames or copies. What it does instead is rename and copy detection. You can request rename detection in git diff
(and git show
) by using the -M
option, you can request additional copy detection in changed files by using the -C
option (-C
implies -M
), and you can request more expensive copy detection among all files with --find-copies-harder
or -C -C
(which implies -C
, which implies -M
). See the git-diff manpage.
You can also configure git to always do rename detection by setting diff.renames
to a boolean true value (e.g. true
or 1
), and you can request git to do copy detection too by setting it to copy
or copies
. See the git-config manpage.
Check also the -l
option to git diff
and the related config variable diff.renameLimit
.
Note that git log <pathspec>
works differently in Git: here <pathspec>
is set of path delimiters, where path can be a (sub)directory name. It filters and simplifies history before rename and copy detection comes into play. If you want to follow renames and copies, use git log --follow <filename>
(which currently is a bit limited, and works only for single file).