How do I change the content of a file within an old commit in Git?

Asked

Viewed 635 times

2

I have a file that has undergone three changes and commits have been made for each change, I want to change the contents manually of the first commit to correct an unauthorized mention.

Is it possible to do something like this, or do I have to redo all the commits? Remembering that so far only one person has access to the project.

Thank you.

2 answers

3

You can make the change with the git rebase -i HEAD~3

This command will open an editor with the last 3 commits and the instructions you should follow.

What you want is in the 3rd commit to change the pick for reword.

After you save this file it will open a new editor with the commit message you want to change. You just have to change the message and save.

Note that if you’ve already pushed the commit you’re going to change you may have to do git push --force

Note: the git push --force It should never be used, unless you really know what you’re doing. Since in this case you said only one person is working on the project, there should be no problem.

  • That’s exactly what I found in the international stackoverflow and posted below.

  • Yeah, it works too ;)

3


I found the answer in Stackoverflow international, it’s quite simple the process:

First go to the desired commit:

git rebase --interactive b6f925df^

Then change pick for edit in the commits you want to modify and then run:

git commit --all --amend --no-edit

To finish run:

git rebase --continue

Take a look at this approach because all SHA-1 files will be modified and this can cause serious problems so think very carefully before doing so.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.