Git squash commit renamed files (save history)

Background

Hi, I am working on a local function branch. This local branch has messed up with many small commits. Before I push the branch on the remote, I would like to put in order.

For this, I would do an interactive rebase:

git rebase -i 

There are no problems so far.

Problem

Now the tricky part here: During the development of the function, I did several refactoring, including renaming and moving files. History of renamed files is available due to their renaming:

 git -mv 

But when I crush the commits before and after renaming, the story goes away, and git notifies about the changes like deleting and adding a file.

What is the problem?

How can I perform squash, including renaming without losing file history?

+6
source share
1 answer

Git does not track renames directly, compares the contents of a file and detects renames in a similar way.

When you dig a story, you make all the changes to the file in one commit. Then the file can change dramatically compared to the previous commit. Thus, it is not very similar to the previous commit, and git considers this to be delete / add.

If you want to view the history of such a file, you must configure the find-renames . For instance. to use for 50%

 git log --follow --find-rename=50 -- someFile 

Similar options are also available for diff , merge and rebase . Take a look at the docs:

Git rebase

rename threshold = n Controls the similarity threshold used to detect rename. See also git -diff 1 -M.

Git diff

- find-renames [= n]

Rename Detection. If n is specified, this is the threshold value for the similarity index (i.e., the number of additions / exceptions compared to file size). For example, -M90% means that git should treat the delete / add pair as a rename if more than 90% of the file has not changed. An unsigned% number should be read as a fraction with a decimal point in front of it. Ie, -M5 becomes 0.5 and thus coincides with -M50%. Similarly, -M05 is the same as -M5%. To limit detection to an exact rename, use -M100%. The default affinity score is 50% .

+1
source

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


All Articles