Disheart
Disheart

Reputation: 197

Is it safe to force push to a public remote branch?

I made some commits on my local feature branch and decided to push it to the public repository for back-up purposes only. Is it safe to rebase some commits and force (with lease?) push them to a public repository? In this case I am the only person working on this branch but would it still be possible to force push when there were working more people on this branch?

Upvotes: 2

Views: 422

Answers (2)

Philippe
Philippe

Reputation: 31137

Is it safe to rebase some commits and force (with lease?) push them to a public repository?

If you are sure that you only work in this feature branch, indeed, you could what you want and indeed, rebase and force push to sync is perhaps the better solution!

In this case I am the only person working on this branch but would it still be possible to force push when there were working more people on this branch?

You could still do force push in some rare cases where you and your team decide that there is some added value compare to keeping history as it is. But expect these very specific case, everybody agrees that it's better to never rewrite the history of a shared branch pushed. Because it will annoy all the other developers and will often require some more time and more advanced knowledge (git rebase -i) to sync its work with a remote with a history modified (force pushed).

Like a lot of things in git, that's not the tool that will prevent you to do some things but rather a team convention because you find something better...

Ps: and yes, if you what to force push, do it always with '--force-with-lease' to prevent losing someone else history that you didn't see!

Upvotes: 0

Makoto
Makoto

Reputation: 106430

In a nutshell:

Don't do it. Not without talking it over first.

If Git is telling you that it won't accept your push because the refs don't line up, Git is doing its job by attempting to protect the integrity of your repository.

That is, if you've got history on your local branch that Git doesn't agree with, Git won't accept it.

Once you force-push that branch, Git assumes you know what you're doing and will let you push the branch.

This has an adverse effect on anyone else using the branch:

  • History has been rewritten, so when they attempt to get new work or attempt to integrate their work upstream, they have to rewrite their own local history to match upstream.
  • Commits could be lost in this manner. Since you're rewriting history with a rebase, this is a scenario which is very likely to happen.
  • Without any prior communication, you will have caused confusion among the maintainers, and this will lead to slowdowns in development.

If it's literally just you then it's fine, but if there's any chance of anyone else using your code base, you have to take precautions.

Upvotes: 3

Related Questions