3

Is there a way to prevent anybody except me from force-pushing to one of my Git repositories (in Github, specifically)?

It's my understanding that protected branches protect against force-pushing entirely, but I'm fine as long as I'm the one doing it.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
jay
  • 1,524
  • 4
  • 25
  • 45
  • This could be done if you could get collaborators to install a pre-push hook. –  Jan 03 '16 at 20:30
  • As soon as anyone else but you uses your branch (read or write) your force-pushing causes that they cannot pull anymore but need to clone every time something changes. So I don't see the usecase here. Maybe it would help if you add a bit more context. – Simon Warta Jan 03 '16 at 20:34
  • A co-worker fetched a branch X in my repo, fixed a bug, then interactive rebased the branch for some reason and force-pushed it back to my repo. Meanwhile, I had some other branches off of X before it was rebased so now those branches have merge conflicts. I'm fine with being able to force push to my own repo because I know what's connected to what (and I use `git commit --amend` a lot) but I'd like other people to ask me first before mucking with it. – jay Jan 03 '16 at 22:34
  • Related: [Elegant solution to prevent force push on master only](https://stackoverflow.com/q/7419244/4561887) – Gabriel Staples Oct 18 '22 at 05:46

1 Answers1

4

Whenever there is a protection issue on a GitHub repo, the surest way to enforce it is to have... two repos.

  • One where you are the only collaborator (and can do force pushes)
  • one fork, where collaborators can still force pushes, but will have to rebase their PR (pull requests) branch on top of your branches, in order to submit their pull requests.
    They would use a triangular workflow.

https://cloud.githubusercontent.com/assets/1319791/8943755/5dcdcae4-354a-11e5-9f82-915914fad4f7.png

That way, you can control what gets into your repo.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250