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

2

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

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

Thank you.

    
asked by anonymous 14.10.2015 / 01:26

2 answers

2

I found the answer in the International StackOverflow , the process is very simple:

First you should go to the desired commit:

git rebase --interactive b6f925df^

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

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

To end run:

git rebase --continue

It has a focus on this approach because all SHA-1 files will be modified and this can cause serious problems so think hard before doing so.

    
14.10.2015 / 01:39
3

You can make the change with 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 pick to reword .

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

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

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

    
14.10.2015 / 01:39