aliteralmind
aliteralmind

Reputation: 20163

How to edit (correct) a file from an old commit, and make that change in all commits after it?

I have this linear series of commits:

A, B, C, D, E, F, G
                  ^
                 HEAD

A couple of files in commit D have errors, which need to be fixed in commit D and all following commits (E, F, and G). This is a private repository, so I can just overwrite everything.

Almost all of my Git experience is

git add --all :/
git commit -m 'Commit msg'
git push -u origin branchname

This question

discusses rebasing, but that seems to be related to merging two branches. I'm hesitant to experiment, for fear of permanently damaging the repository. Perhaps that implies I should be branching and then merging...

What is the proper way of going about this?

Upvotes: 0

Views: 100

Answers (1)

jthill
jthill

Reputation: 60235

Assuming your current branch is master,

git checkout $D
# fixup file here
git commit -a --amend  # fixup in added commit (w/o --amend) is also a good option
git rebase @ master  # <-- or git rebase HEAD master, same thing

Be aware that all the rebased or amended commits in $D^..master will be rewritten, but no other children of $D^ -- if you have branch points downstream of $D you'll have to rebase those onto the new history separately.

This is pretty much identical to the rebase --interactive suggestions in linked answers/questions, but when only one thing needs fixing up I find it more pleasingly direct to just do it myself.

Upvotes: 1

Related Questions