0

There are lots of questions on this site regarding the behavior of git mv and how to retrieve the history of a file across name boundaries. I understand that Git doesn't actually record the moving/renaming of a file, but I'm not entirely understanding where the problem is with doing so manually.

Suppose I want to rename a file, and I care about keeping its history together. If my workflow is something like

git mv file.txt new_file.txt
<make substantial changes to new_file.txt>
git add new_file.txt
git commit -m "rename and change"

then the history will be broken. The output of git log --follow -- new_file.txt will just be this most recent "rename and change" commit.

However, if I instead use a workflow like

git mv file.txt new_file.txt
git commit -m "rename"
<make substantial changes to new_file.txt>
git add new_file.txt
git commit -m "change"

then it seems like I've averted the break. The output of the same log command now gives me "change", "rename", and any commits that file.txt was involved in before that.

I realize that it's redundant if the file isn't being substantially edited, since Git will deduce the renaming on its own in that case. But in terms of actual nasty problems, the most likely one I could think of is some kind of merge conflict, and I haven't been able to find a situation that goes worse than doing things the regular way.

My question is: Am I missing something? Is there danger lurking somewhere in making an explicit "rename" commit like this?

meltyhobo
  • 56
  • 3
  • Why would there be a danger? You're keeping the history obvious, and doing so at the cost of a single commit (which is minimal in most cases). – jhpratt Feb 19 '18 at 21:27
  • @jhpratt I guess I was just thrown off by how, in all of the many questions I read about how to keep track of file renames, I didn't see anyone bring this up. Most people just unilaterally state that it's impossible beyond what Git automatically does. – meltyhobo Feb 19 '18 at 23:13

1 Answers1

0

Nothing is made any worse by doing this. Currently, nothing is made any better either, but if I ever get around to it, and if my changes to Git get accepted, this might actually make git merge work better in some cases (perhaps with a flag argument to git merge) in the future. Someday, maybe.

(Specifically, git merge currently just does git diff <base> <tip> for each of the two tips, without looking at any of the commits between them. It seems to me that it might be good if it ran a quick ancestry-path scan for commits that rename files, and assemble all these renamings for the final base-to-tip diffs. To make this scan acceptably fast it would need to check only for exact-renamings, not approximate ones; so adding a commit that does the rename-but-nothing-else would enable this extra merge pass, if it were ever added, to find the rename across bigger code changes.)

(It's not really clear that this adds much, if any, value, since a rename across a large gap where Git can't detect the rename, might not be of any value to accomplishing the merge.)

torek
  • 448,244
  • 59
  • 642
  • 775