Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git squash commits of renamed files (an keep history)

Backround

Hi, I'm working on a local feature branch. This local branch is messed up with lots of small commits. Before pushing the branch to the remote I would like to tidy things up.

For this I'd do a interactively rebase:

git rebase -i

No problem so far.

Problem

Now here's the difficult part: During the development of the feature I've done several refactorings including renames and moves of files. The history of the renamed files are available, due to renaming them with:

git -mv

But when I squash the commits before and after the rename-commits, the history is gone and git notifies the changes as deletion and addition of a file.

What is the Problem?

How can I squash commits including rename without loosing file history?

like image 597
RamNow Avatar asked Feb 19 '15 12:02

RamNow


1 Answers

Git does not track renames directly, it compares file contents and detects renames by similarity.

When you squash a history you put all changes of a file in one commit. The file might then change a lot compared to the previous commit. Thus it is not very similar to the previous commit and git thinks it is a delete/add.

if you want to see the history of such a file you must adjust the find-renames threshold. E.g. for 50% similarity use

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 for rename detection. See also git-diff1 -M.

git diff

--find-renames[=n]

Detect renames. If n is specified, it is a threshold on the similarity index (i.e. amount of addition/deletions compared to the file’s size). For example, -M90% means Git should consider a delete/add pair to be a rename if more than 90% of the file hasn’t changed. Without a % sign, the number is to be read as a fraction, with a decimal point before it. I.e., -M5 becomes 0.5, and is thus the same as -M50%. Similarly, -M05 is the same as -M5%. To limit detection to exact renames, use -M100%. The default similarity index is 50%.

like image 154
René Link Avatar answered Oct 06 '22 19:10

René Link