Recover migrated file history in Git

If the name and contents were indicated in the file, is it possible to mark the movement after the commit?

eg.

  • file1.txt changed to file2.txt , and the content is changed.
  • git commit
  • Git believes that file1.txt was deleted and file2.txt is an unrelated new file.

Is there a way to mark this as a rename after a transaction?

+4
source share
3 answers

No, it is not, because Git does not rename the tracking ... but it renames the detection
(based on how similar the contents and file name are).

It means that

  • git log --follow file2.txt will follow the history of files through rename (note: this, unfortunately, does not always work correctly, since --follow is a bit of a hack),
  • git blame -C -C file2.txt will follow the linear history of files when renaming files, copying contents, and moving files inside the file.
  • git show -C will show that a rename file exists (you can configure Git to always rename and possibly even copy when displaying the differences).

NTN

+3
source

git will not detect a combination of truly significant changes and simultaneous movement as a rename, as you have discovered.

It’s clear that git detects commit changes every time you look at the history by comparing the contents of commit with its parent commit. It may seem too simple, but it turns out that it is a good view.

So you need to insert a new commit, rename which is git log, etc. can detect as such, followed by the rest of the story that you have accumulated since then, and this means a new story for your branch, i.e. relocation or cherry.

So:

 # make a new history with a rename that can be detected by comparing commits # check out the glitched commit ancestor by its sha, # this work won't take enough time to bother inventing a transient name for it git checkout commit-before-rename+bigchange git mv file1.txt file2.txt git commit git checkout commit-with-rename+bigchange -- file2.txt git commit # Now add the rest of the changes, also detectable by comparing commits, # by re-applying them on top of this new history: git cherry-pick commit-before-rename+bigchange..branch-it\'s-on # and force the old branch name to refer to this new history you just made # the old history will be garbage-collected after 90 days or so if # it not part of the history of any other references git checkout -B branch-it\'s-on 

And you are finished, unless some other repositories have gotten a corrupted story by extracting or clicking or cloning, then you have to force push or force these repo owners to force redial (and any subsequent history that they built on top of that, so you really don't want to publish too soon).

+1
source

No no. Git has no idea about file identity. It stores the contents of directory trees. Only the tree as a whole has a concept of history.

There is only one reason to keep track of the fact that a file has been renamed instead of the old one and a new one has been added, and this will allow merging the changes into this file. Since you changed the file (in addition to considering the rename detection to be similar), there is no way to merge the changes without conflict in any case, so there should be no practical difference.

Note that Git handles the reason for the merge, looking for a similar file. It does this in git status and in all merges automatically and in git log , git diff and other commands showing changes if you ask it. If he does not find the renaming, it is hardly possible to merge.

0
source

Source: https://habr.com/ru/post/1500511/


All Articles