How can I prevent others from forking my repository?

Christopher Mcclure
Christopher Mcclure
Seasoned entrepreneur with 15 years in tech startups.

This is a common question, especially for those new to code hosting platforms. Let me explain it to you in simple terms.

First, the most direct answer is: If your code repository (repo) is public, you technically cannot prevent others from forking it.

This might sound a bit disappointing, but you need to understand the original intention of platforms like GitHub. Their core is "openness" and "collaboration." You can think of the "Fork" feature as "copying a version to my own space," which is the foundation of all collaboration. If your code is public, it's like putting a book in a public library; anyone can borrow and copy it. You can't stop people from photocopying that book.

So, if you really, truly don't want anyone to copy your code, the only way is to:

Set your repository to "Private."

This is the simplest, most direct, and most effective method. Once your repository is private, only you and your explicitly invited collaborators can see it. Others won't even be able to access your repository page, so naturally, they can't fork it.

This typically applies to situations such as:

  • Company's commercial project code.
  • Personal freelance work that hasn't been delivered yet.
  • Sensitive code or personal experiments you don't want to make public.

However, I suspect you might have another scenario in mind: "I want my project to be visible to others, to showcase my skills, or for job applications, but I don't want others to just take it and use it, especially to sell it."

If this is your situation, then what you need to focus on isn't "preventing forks," but rather the "License."

Let me give you an analogy:

  • Setting a private repository is like locking your family heirloom in your personal safe; no one can see it, and it's absolutely secure.
  • Setting a license is like displaying that heirloom publicly in a museum. While everyone can see it and take photos (which is equivalent to forking), there's a sign next to the display case outlining the exhibition rules (this is the license), such as: "Prohibited for commercial use," "Copies must attribute the source," and so on.

This "license" is a legal statement. It doesn't prevent people from clicking that fork button, but it clearly tells everyone what they can and cannot do after forking your code.

  • If you don't add any license, your code is protected by copyright law by default, and theoretically, others cannot do anything with it (cannot use, modify, or distribute). However, this can be ambiguous in the open-source community, and people might disregard it.
  • If you add a clear license, such as MIT (very permissive) or GPL (requires modifications to also be open source), or even a specific "non-commercial" license, you set the rules for everyone who uses your code.

If someone violates the terms of the license—for example, if you explicitly stated "non-commercial use," but they package your code and start selling it—then you can pursue legal action against them.

To summarize, here are two paths for you:

  1. If you want absolute confidentiality: Directly set your repository to Private. This is the only technical means to "prevent forks."
  2. If you want to make it public but retain rights: Keep your repository Public, and then choose an appropriate License for it. This won't disable the fork button, but it will provide you with legal protection.

So, the key isn't how to "prevent" it, but rather to first clarify the purpose of your project: do you want complete confidentiality, or do you want to protect your rights while making it public? Once you're clear on this, you'll know what to do.