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

rusydy

Rusydy

Posted on September 11, 2023

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.

💖 💪 🙅 🚩
rusydy
Rusydy

Posted on September 11, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

DS_Store in git, why ?
github DS_Store in git, why ?

June 15, 2023

Managing Cleaner Git Branch
git Managing Cleaner Git Branch

February 13, 2023

Git Rebase
git Git Rebase

February 4, 2023

How to keep your secrets off GitHub
webdev How to keep your secrets off GitHub

October 17, 2022