Search code examples
gitgithubgit-remote

How to remove remote url from Github repository


I'm sure this is a super simple question but I wasn't able to find an answer to it, so I'm extremely grateful for your answers!

I have created a boilerplate repository that I (and anybody else) can copy as a starting point for a new project. Here's the problem: when I upload that repository to Github, I obviously need to set the remote URL. However, when someone (me, for example) clones the repository and makes changes, it's easy to push the changes without setting up your own URL, and therefore push changes to the original boilerplate.

How can I remove the remote URL from the repository and still host it on Github?


Solution

  • The remote isn't set on GitHub; it's set when you clone. Cloning sets up a remote, called origin by default, that points to the URL you used to clone.

    A few options:

    1. Don't clone in the first place.

      You can create an empty repository and then pull from the source repository without setting a remote:

      git init foo
      cd foo
      git pull https://some.tld/foo/bar
      

      To make this easier, you could create an alias that does it all in one go, e.g. by adding something like this to your ~/.gitconfig:

      [alias]
          clone-template = "!f() { git init \"$2\" && git -C \"$2\" pull \"$1\"; }; f"
      

      Then you'd use it like this:

      git clone-template https://some.tld/foo/bar foo
      

      where foo is the local directory name you'd like to use.

    2. Clone and then remove the origin remote:

      git clone https://some.tld/foo/bar
      cd bar
      git remote remove origin
      

      To make this easier, you could create an alias that does both operations, as above.

      The tricky part there is knowing the name of the directory that was created by the clone so you can cd into it. The alias above requires the local directory as an argument to get around this.

    3. Use a third-party tool like degit that clones without history.

      The main disadvantages here are (a) having a third-party dependency, possibly using a dev stack you don't want to set up, and (b) losing all commit history as well as the remote.

      You can mitigate against (a) by doing something like git clone --depth=1 followed by removing the .git/ directory, but this will still nuke all your history. If you're going to do this, I suggest you just unset the origin as in the second option.