Home > Enterprise >  Rewrite a file in an earlier commit then propagate that change to all later commits seamlessly
Rewrite a file in an earlier commit then propagate that change to all later commits seamlessly

Time:10-12

Suppose I have a series of commits like this.

(0) Initial commit
(1) Version 1
(2) Version 2

I already know that I can rewrite history without creating a new commit by using git rebase to edit, say, the README in commit 0. But how do I propagate that change to all newer commits? If I don't do that, it just looks like I had a section in commit 0 that I then later removed in commit 1.

Essentially I want the history to look like the change I made to the README was in fact always there from the beginning, without creating any new "merge" commits.

CodePudding user response:

To accomplish what you want, you can use an interactive rebase where you say that you want to edit the first commit.

Run git rebase -i --root to get a rebase task list right from the beginning of the history (since you say you want to edit the initial commit).

In the commit selection window, set the initial commit to edit and the rest of pick:

edit sha0 (0) Initial commit
pick sha1 (1) Version 1
pick sha2 (2) Version 2

Make the changes to README and commit them with git commit --amend as you should have been prompted by rebase, and when you're done run git rebase --continue and Git will rewrite the rest of the history on top of your modified initial commit.

Warning: if you have any merged in your history, this operation will destroy them. If you do have merges you want to preserve, add the --rebase-merges option to that git rebase command.

CodePudding user response:

If you use git rebase to change the initial commit, then that change is propagated to the later commits automatically. If you use git filter-repo (or the old git filter-branch), then you have to make the change in each commit.

  • Related