Search code examples
gitgithubgit-commitgit-pushgit-reset

Commit my newly modified code so that it will be patched on the original codebase, and not on top of my last commit?


I did a git commit followed by git push. After that, I made some code changes and want to commit this code instead. But now if I do a git push, the changes will be shown on top of my changes (showing deleted lines from my first commit which never existed in the codebase).

How can I commit my newly modified code x such that it will be patched on the original codebase, and not on top of my last commit (as if it were my first commit)?


Solution

  • Provided this is a feature branch on which you're working alone

    # make your last changes, then
    git add .
    git commit --amend
    git push --force origin HEAD
    

    Here, --amend is the flag asking git to replace last commit with this one in the branch history. That's the reason you need --force in the subsequent push.