Letharion
Letharion

Reputation: 4229

When is `git push --force` appropriate?

Something I've seen "hammered in" repeatedly is that one should not ever git push --force, though surely there are times when it is appropriate?

When is --force intended to be used?

My current example:

  1. Create branch and make some changes.
  2. Push branch "live" to show code.
  3. Rebase on origin, make changes/re-write history in the process.
  4. My code has now diverged from my own remote code.

Is push --force the appropriate solution to this?

Upvotes: 1

Views: 327

Answers (4)

thameera
thameera

Reputation: 9473

push -f becomes really necessary if you need to remove some sensitive data from the repo which you have mistakenly pushed. In that case you'll need to do a git filter-branch and git push -f. This article explains the process.

Also, I've come across instances when you need to do push -f when a junior developer has done something stupid with the repo. Anyway, one shouldn't have given them the rights in the first place or given them adequate training.

Upvotes: 2

ellotheth
ellotheth

Reputation: 4503

Rewriting history will be a headache for anyone who pulled your repo and made their own changes based on your branches. When you rewrite your remote history and the downstream people pull it, their changes will end up dangling off in the middle of nowhere. (Check out the Recovering from an upstream rebase section of the git rebase docs.)

That's why if other people are using your repo, it would generally be better to merge changes from origin into live and push the merge. It's not as clean as a rebase, but no one downstream will need to do funky things to keep up with your rewrites:

git checkout live
git merge --no-ff origin/whatever
git push origin live

You can always rebase when you're ready to pull the changes in live back into the main branch.

On the other hand, if you're the only one using your repo, or the people downstream are ok with fixing remote history rewrites, --force away.

Upvotes: 1

Samy Dindane
Samy Dindane

Reputation: 18706

I usually push -f right after the initial push, when I know that no one has fetched my changes yet, or when I'm the only one working on the project.
Otherwise, as long as you rewrite the history, you should avoid forced pushes.

Upvotes: -1

dunni
dunni

Reputation: 44515

In that case yes, assumed that you're the only one who works on that branch, or that others understand the implications, when you do a forced push and rewrite the history of that feature branch.

Upvotes: 2

Related Questions