Last night, I made an intriguing discovery about the intricacies of
git commit amending that, I believe, is worth sharing.
It turns out that once you've amended a
git commit, the conventional
git pull and
git push commands won't work as expected. This quirk arises due to the inherent discrepancy between the local and the remote repositories once a commit has been amended. In most cases, the local repository is regarded as the source of truth, requiring the remote repository to align with it.
To illustrate this concept, consider a recent real-world scenario: I decided to remove three commits from my pull request. As anticipated, a divergence emerged between my local and remote repositories. Typically, I would have instinctively used
git push -f to forcefully push these changes to the remote. However, I received advice against following my muscle memory and instead opted for the traditional approach of executing
git pull, followed by
git push. To my astonishment, the remote repository still retained the three commits I had removed.
Now, I couldn't help but reflect on why my previous teams resorted to using force-pushes so frequently. However, it's important to note that there's no need for concern regarding the potential loss of tracking before and after a force-push. GitHub has an effective record-keeping system that ensures your repository's history remains intact.