I have an interesting problem at hand. In a shared team repo, a team member added a remote to a completely unrelated repository (this was an accident).
e.g.
git remote add foreign-remote https://github.com/some-open-source-project
He checkout out the master branch of that repo and pushed it to our repo.
e.g.
git checkout foreign-remote/master
git checkout -b experiment
git push origin experiment
This created effectively two unrelated sub-graphs within the team repo:
The left part is our proper code, the right part is that foreign repository.
How can we completely remove that right part? It is not causing any harm but it is very annoying and I would like to get rid of this sub-graph, preferably without leaving any traces behind.
Also, is there a way of preventing this from happening?
Remove the branches that points to the wrong graph from the remote. In your case it is experiment
.
git push origin :experiment
If other developers already fetched that branch they must do a
git fetch --prune
In more detail
A F
/|\ |
B C D G
\|/ |
E <-- origin/master H <--- origin/experiment
Now if you remove the experiment
branch from origin
then commit H
will not be referenced anymore. Thus the commits F-G-H
will be deleted on a git gc
.
After you removed it from the remote. The developers who already fetched it must do a git fetch --prune
to remove the remotes that no longer exists.
Make a backup clone if you want to feel safer
Also, is there a way of preventing this from happening?
Use gerrit code review. It let you define fine-grained access rights and you can reject a changeset on review. Thus the wrong commits would be discarded and not get integrated in your repository.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With