TheMonkWhoSoldHisCode
TheMonkWhoSoldHisCode

Reputation: 2332

Git push is deleting branches

I am new to git and I fail to understand how can a "git push" command deletes another branch.

There are two branches as you can see below. master and testbranch. These branches were created by two different people. However a "git push" by a user deleted the branch created by another user. Can someone explain how is this possible and what needs to be done to avoid such behavior. For the record, I use GitLab.

Edit: Additional info : The use who is pushing has a mirror configured.

enter image description here

Upvotes: 2

Views: 347

Answers (1)

joanis
joanis

Reputation: 12229

Summarizing the comments into an answer: as you indicated in the chat, the push was done from a mirror, and that is the cause of the problem.

A mirror is meant to be kept in complete sync, so when you push from a mirror, it effectively does --prune by default. A mirror is not meant to be used as a sandbox, it's meant to be used for backups or other situations where you want a complete reproduction of a repo somewhere else.

When pushing from a mirror clone, every push operation will be done with the --mirror switch implied, which force pushes everything and prunes locally deleted branches. (Ref: git push manual, look for --mirror)

Preventing such problems can be done with:

  • branch protection for the important branches (although this won't be practical for all branches);
  • user education, making sure users don't treat mirrors as sandboxes;
  • regular automated backups, to be able to restore the repo if this happens again.

Upvotes: 4

Related Questions