Search code examples
gitgithubgitlabcommitgit-commit

Can I change the commit history in two ways using Git?


I'm asking this question because I'm studying the Pro Git book and there is a chapter, Rewriting History, that explains how to change the last commit $ git commit --amend, change multiple commit messages $ git rebase -i HEAD~3, delete a particular file or folder in all the commits with $ git filter-branch --tree-filter.

Here my question: the book doesn't mention the possibility of doing this work by multiple checkouts along ALL the commit's history. So I move my HEAD, commit by commit, and I change this particular commit with $ git commit --amend one by one.

I know it's really (REALLY!) expensive in terms of time spent, but is it really possibile (i.e. it works)?
And if yes, in which cases this could be useful?


Solution

  • For all commits, you can use newren/git-filter-repo, which can rewrite anything you need (author, commit message, files or file contents, ...)

    For example, regarding commit message, using message-callback:

    The message callback is quite similar to the previous three callbacks, though it operates on a bytestring that is likely more than one line:

    git-filter-repo --message-callback '
     if b"Signed-off-by:" not in message:
       message += b"\nSigned-off-by: Me My <self@and.eye>"
     return re.sub(b"[Ee]-?[Mm][Aa][Ii][Ll]", b"email", message)'