-1

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?

Antti
  • 13
  • 3
  • Make this a read-only repository so people can clone it but cannot push to it? – matt Apr 21 '22 at 12:41
  • 2
    The `git clone` operation will add a remote named origin with whatever URI was used in the clone operation. This is not stored in the repo on the server, only on the client. – fredrik Apr 21 '22 at 13:10

2 Answers2

1

check this command

 git remote remove origin
Ahm Sf
  • 33
  • 9
  • Ahm Sf I i remove remote, how can I then update that change to the remote repository in Github? – Antti Apr 21 '22 at 11:58
  • did get your point, why do you need to remove remote url and still you need to send update to github? – Ahm Sf Apr 21 '22 at 12:53
  • I'd like to have my Github repository to be available to clone, but I do not want to have cloned versions to get pushed into it, if that makes sense? – Antti Apr 21 '22 at 12:56
  • Chris I salute you! Thanks a lot, that was really useful. To be honest, I wasn't aware of pull method, will use that from now on! – Antti Apr 21 '22 at 17:03
1

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.

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257