Search code examples
gitcommitgit-commitgit-extensionsgit-amend

Understanding git commit with amend option


let's say i have a file 'A' i have modified the file and pushed it to the repository by the following commands

git add A
git commit -m "changed A"
git push 

later after i have realised that i need changes in file 'B' so i have modified it and ran following commands

    git add B
    git commit --amend -m "changed AandB"
    git push

but i get error..

error: failed to push some refs to 'https://xxx@github.com/xx/xx'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.

what should i do to overcome that error


Solution

  • Amend actually changes the git history. You need to do force (-f) push to update the remote.

    N.B: force push will replace the remote's history with the local's history so, it's dangerous.

    $ git push -f