Why is it dangerous to do a force-push against a remote repository?
Asked Answered
U

2

6

Let's say I've made some changes, committed them, and then pushed my commit to the remote repository. Then I decide that I forgot to comment it adequately, or misspelled something, and I want to do a soft reset to edit and re-commit my changes.

What makes it "dangerous" to force-push this to the remote repository?

Uniocular answered 20/10, 2015 at 21:42 Comment(0)
B
7

Git tries really hard to not overwrite any of your changes, so in your day-to-day routine, if you attempt to push to a remote repository whose HEAD reference differs from your local HEAD reference, Git will pitch a fit about it.

When you use --force, Git takes the training wheels off and assumes that you know what you're doing, even if you don't fully understand or appreciate the ramifications of your actions.

To illustrate your example, this is what your remote repository has on it:

<-- [] <-- [] <-- [HEAD]

...and this is your local after you've pushed, but before you discovered your mistake:

<-- [] <-- [] <-- [HEAD]

When you went to correct the mistake by using --amend, Git actually has two commits that refer to the previous one, but only one of them refers to the canonical HEAD reference.

<-- [] <-- [] <-- [HEAD]
            \
             \<-- [(old HEAD)] 

The SHA of your new HEAD reference doesn't match what's on your remote repository, so Git will refuse to push since it thinks you're going to lose data.

When you force it, though, Git trusts you and takes the new HEAD as the canonical reference. Through this, the reference to the old HEAD commit is lost.

<-- [] <-- [] <-- [HEAD]

This is dangerous since:

  • You could have lost data; the likelihood is that you didn't if it's a simple --amend, but it's still possible

  • Anyone else that's based their work off of the old HEAD reference now has to rebase their work, which can make for a lot of undue heartburn.

Communication can mitigate the second piece, but the first piece requires a bit more caution when force-pushing. Only force-push if you're absolutely certain that what you're pushing won't cause a loss in data or information.

Breedlove answered 20/10, 2015 at 21:49 Comment(1)
Is there an issue, if other people just have pulled the repository and not have done any changes to it?Neuralgia
T
3

One of the main risk of using --force push is to not see a commit pushed by someone else and so, that will be lost!

To prevent such a case, the good practice is to never use --force but to prefer --force-with-lease which guarantee that you have fetched the all the commits of the branch in your repository before consciously ignore them...

Tarkington answered 20/10, 2015 at 22:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.