I got quite confused with all merging and rebasing commands and got couple of errors. Given project is just for myself. My problem is following:
My commits in master, origin/master branch (from latest, all publicly visible):
Commit 4 - test_data.txt commited, same file as in commit 2
Commit 3 - revert of commit 2, code.txt bug found, state of commit 1
Commit 2 - code.txt test_data.txt changed
Commit 1 - Everything fine
git checkout commit 2
Because you've pushed commits to the master branch of a publicly available repository, then it's ill advised to rewrite history. You should make commit 5 fix on top of 4 and let 2-4 continue to have the bug in history.
However, to answer your question:
Checkout the branch tip and do an interactive rebase.
git rebase -i <commit 1>
edit on commit 2. Make your changes, then continue.
git rebase --continue
Now history has been rewritten and you will need to force push the branch.
git push origin master --force
And because of these shenanigans, others will have to force update.
git fetch origin git reset --hard origin/master