Skip to content

Instantly share code, notes, and snippets.

@devinschumacher
Last active October 27, 2025 17:07
Show Gist options
  • Save devinschumacher/c0d45bc626999f19df5429c5cd549a8c to your computer and use it in GitHub Desktop.
Save devinschumacher/c0d45bc626999f19df5429c5cd549a8c to your computer and use it in GitHub Desktop.
Github Private Forks: How to privately fork a repository
title description tags
How to Create a Private “Fork” of a Public GitHub Repo
Step-by-step guide to clone a public repo into a private repository in your organization while still pulling in upstream changes.
git
github
private-fork
cli

How to Create a Private “Fork” of a Public GitHub Repo | Step-by-step Guide

  1. Clone the original repository locally
  2. Create an empty private repo in your org
  3. Create a local “fork” branch
  4. Re-assign remotes
  5. Push branches to your private repo
  6. (optional) check the it worked
  7. Get to work

Click here to watch the video 👇


1. Clone the original repository locally

git clone https://github.com/<ORIGINAL_OWNER>/<REPO_NAME>.git
# Rename repo to whatever you want:
mv <REPO_NAME> <NEW_NAME>

# go into folder
cd <REPO_NAME> <NEW_NAME>

2. Create an empty private repo in your org

gh api \
  --method POST \
  /orgs/<YOUR_ORG>/repos \
  -f name='<NEW_REPO_NAME>' \
  -f private=true \
  -f auto_init=false

3. Create a local “fork” branch

git checkout -b fork

4. Re-assign remotes

Rename the existing origin (public repo) to upstream and add your private repo as origin:

git remote rename origin upstream
git remote add origin [email protected]:<YOUR_ORG>/<NEW_REPO_NAME>.git
git branch --set-upstream-to=upstream/main fork

Verify:

git remote -v
# origin    [email protected]:YOUR_ORG/NEW_REPO_NAME.git (fetch)
# origin    [email protected]:YOUR_ORG/NEW_REPO_NAME.git (push)
# upstream  https://github.com/<OWNER>/<REPO>.git (fetch)
# upstream  https://github.com/<OWNER>/<REPO>.git (push)

5. Push branches to your private repo

git push -u origin main
git push -u origin fork

6. (optional) check the it worked:

git remote show origin
git remote show upstream

7. Get to work

git switch main

(optional) BUT VERY SAFE

1. Remove push ability to the upstream

You can REMOVE the ability to push your now private (and probably NSWF filled) repo by running

git remote set-url --push upstream no_push

Then run git remove -v again, and you should the push remove

⚠️ However: GitHub pull requests are not tied to git push remotes.

When you push to your origin (your fork), GitHub sees:

  • Your fork has new commits
  • Your fork was originally based on the place you forked from
  • GitHub suggests a PR back to upstream (since it knows the relationship)

✅ To be even safer, remove Github from offering PRs back to upstream:

👉 Detach the fork on GitHub:

  1. Go to your fork: https://github.com/your-org/your-repo
  2. Click Settings
  3. Scroll to “Danger Zone”
  4. Click “Detach fork” → Confirm

This will:

  • Break GitHub’s fork link
  • Make GitHub stop suggesting PRs back to the original place you forked from
  • Keep your repo and remotes intact — you can still git fetch upstream from CLI

Example

How to work privately and use the fork

Confirmed. You can treat fork purely as your “upstream sync” branch and do all your actual work on main. Here’s the streamlined flow:

  1. Clone your private fork

    git clone [email protected]:YOUR_ORG/NEW_REPO_NAME.git
    cd NEW_REPO_NAME
  2. Ensure your remotes are set

    git remote add upstream https://github.com/ORIGINAL_OWNER/REPO.git
    # (only if you haven’t already)
  3. Update the fork branch from upstream

    git checkout fork
    # make sure fork tracks upstream/main:
    git branch --set-upstream-to=upstream/main fork
    
    git pull              # fast-forwards fork ← upstream/main
  4. Merge upstream changes into your main

    git checkout main
    git merge fork        # brings in all upstream updates
    git push origin main  # pushes your updated main to your org
  • You never commit on fork—it’s exclusively for pulling in the third-party repo’s updates.
  • All your feature work, bug-fixes, PRs, etc. happen on main (or feature branches off of main).

Shortcut: Merging upstream directly into main

If you’d rather skip the extra branch, just do:

git fetch upstream
git checkout main
git merge upstream/main
git push origin main

Either pattern keeps your main clean and your “sync” branch dedicated solely to upstream updates.

@TopherTimeMachine
Copy link

TopherTimeMachine commented Apr 25, 2025

did a git remove -v and it looked correct.

then did push -u origin master and got the following error.

[email protected]: Permission denied (publickey).
fatal: Could not read from remote repository.

@devinschumacher
Copy link
Author

devinschumacher commented Apr 25, 2025

did a git remove -v and it looked correct.

then did push -u origin master and got the following error.

[email protected]: Permission denied (publickey). fatal: Could not read from remote repository.

i can help you but i need more info that that. can you just give me all the terminal commands/output you went through?

I've never seen that error before, but at first glance it looks like a permissions thing.

EDIT: i just ran the whole flow fresh and I got through everything okay - see here

@devinschumacher
Copy link
Author

devinschumacher commented Apr 29, 2025

@TopherTimeMachine git remove -v is not a command. it's git remote -v

@TopherTimeMachine
Copy link

Sorry. Mistyped. Yes it was git remote -v

@devinschumacher
Copy link
Author

i re ran this a few times. and updated some commands if you wanna give it a try again

@erlonbie
Copy link

Hey, thanks for the gist! I followed and everything worked as expected. I just couldn't find the detach part on the danger zone

@devinschumacher
Copy link
Author

Hey, thanks for the gist! I followed and everything worked as expected. I just couldn't find the detach part on the danger zone

right on!!

@rotimi-best
Copy link

Thanks a lot for this.

If you are getting this error when you run git branch --set-upstream-to=upstream/main fork

error: the requested upstream branch 'upstream/main' does not exist

Before running git branch --set-upstream-to=upstream/main fork

Make sure you run git fetch upstream main

@yantakus
Copy link

yantakus commented Jul 3, 2025

I followed the manual. Trying to detach the form, but there's no "Detach fork" in my project settings danger zone. Have I done something wrong or is it just not there anymore?

@devinschumacher
Copy link
Author

@yantakus screenshot please

@gcleaves
Copy link

gcleaves commented Sep 23, 2025

I'm getting the same error as @TopherTimeMachine . Did you ever find a fix?

Ah, the problem maybe was that I created the repo on github.com. After doing that, I needed to edit .git/config to change [email protected]:YOUR_ORG/NEW_REPO_NAME.git to https://github.com/YOUR_ORG/NEW_REPO_NAME.git

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment