I have a file with a lot of lines and want to refactor it, by splitting it across multiple files. By simply copy/pasting I will loose history of committed lines and their authors when git blame
ing. Is there any way to copy/paste those lines and preserve their authors?

- 47,849
- 12
- 88
- 91
-
Not a perfect dupe, but should answer your question – Lasse V. Karlsen Aug 22 '17 at 06:57
-
1Possible duplicate of [Difference between author and committer in Git?](https://stackoverflow.com/questions/18750808/difference-between-author-and-committer-in-git) – Lasse V. Karlsen Aug 22 '17 at 06:57
-
@LasseV.Karlsen and how it will help me to preserve the authors? – Engineer Aug 22 '17 at 07:06
-
You will have to commit with their name as the author – Lasse V. Karlsen Aug 22 '17 at 07:27
-
Git will attribute the pasted lines to the author of the commit that added them, you can't persuade git to go hunt for the original author, so you have to be explicit when committing. – Lasse V. Karlsen Aug 22 '17 at 07:27
-
However, git tooling is good at identifying moved pieces of text, have you checked that you actually have a problem that needs to be solved? – Lasse V. Karlsen Aug 22 '17 at 07:28
-
@LasseV.Karlsen There are almost 4 authors and lot of lines. This is so much routine work. Any easy way and what do you mean by git tooling? Which git commands to use? Simple copy/paste could not work. Once again I am copy/pasting lines not whole files. – Engineer Aug 22 '17 at 08:21
-
What I mean is do a test, copy and paste some lines, make a commit (with and without that author parameter), do a git blame and see what happens. – Lasse V. Karlsen Aug 22 '17 at 08:22
-
If you don't like the results, then no, there is no easy way to do this because git is focused on changes, not code ownership. – Lasse V. Karlsen Aug 22 '17 at 08:25
-
Does this answer your question? [How to preserve git history when refactoring into multiple files](https://stackoverflow.com/questions/38729453/how-to-preserve-git-history-when-refactoring-into-multiple-files) – andruso Nov 13 '19 at 13:52
1 Answers
Git does not record the author (or committer) of a specific line of code.
What Git does—all that it does—is record a complete snapshot, paired with a set of metadata: author name/email/date, committer name/email/date, parent commit.
Various tools, such as git log
, git show
, git diff
, and git annotate
(or git blame
), use the recorded data in various ways. The git blame
code in particular will compare each commit against its parent. If some line(s) were added or changed in this particular commit, it will assign the commit's author to the entire line(s) and report the result.
This means it's easy to get the tool to assign some particular set of changed line(s) to some particular author: make a commit with that name set as the author.
This also means it's easy to accidentally change every line of a file (e.g., by changing line-terminator characters from LF-only to CRLF or vice versa) and "acquire authorship" of every line of that file. But all this means is that when you are using git blame
, you need to know how to work the tool: you need to be aware of its limitations.
Some—I include myself in this group—would argue that if you move code around for refactoring, you should "take ownership" of the moved lines. The original author may have written the original code, but not in that form. The line for i in expr
, for instance, means something very different in two different functions and/or files. If you move a big enough chunk, maybe the code retains its "real meaning", and ... maybe not. Git will keep the original author of the original lines in their original place, and anyone who understands git blame
will not just look at the new refactored code, but continue to follow the code back through history to find its original author, pre-refactor.
Still, if you want to split "author" and "committer", Git gives you a (per-commit) way to do that.

- 448,244
- 59
- 642
- 775