DEV Community

Cover image for The Ease of Force-Pushing and GitHub's Safety Net
Rusydy
Rusydy

Posted on

The Ease of Force-Pushing and GitHub's Safety Net

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.

The example of force records

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.

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

đź‘‹ Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay