How to create a remote Git repository from a local one?

前端 未结 8 1744
不知归路
不知归路 2020-12-02 03:29

I have a local Git repository. I would like to make it available on a remote, ssh-enabled, server. How do I do this?

8条回答
  •  执笔经年
    2020-12-02 04:25

    There is an interesting difference between the two popular solutions above:

    1. If you create the bare repository like this:

      cd     /outside_of_any_repo
      mkdir  my_remote.git
      cd     my_remote.git
      git init --bare
      

    and then

    cd  /your_path/original_repo
    git remote add origin /outside_of_any_repo/my_remote.git
    git push --set-upstream origin master
    

    Then git sets up the configuration in 'original_repo' with this relationship:

    original_repo origin --> /outside_of_any_repo/my_remote.git/
    

    with the latter as the upstream remote. And the upstream remote doesn't have any other remotes in its configuration.

    1. However, if you do it the other way around:

      (from in directory original_repo)
      cd ..
      git clone --bare original_repo  /outside_of_any_repo/my_remote.git
      

    then 'my_remote.git' winds up with its configuration having 'origin' pointing back to 'original_repo' as a remote, with a remote.origin.url equating to local directory path, which might not be appropriate if it is going to be moved to a server.

    While that "remote" reference is easy to get rid of later if it isn't appropriate, 'original_repo' still has to be set up to point to 'my_remote.git' as an up-stream remote (or to wherever it is going to be shared from). So technically, you can arrive at the same result with a few more steps with approach #2. But #1 seems a more direct approach to creating a "central bare shared repo" originating from a local one, appropriate for moving to a server, with fewer steps involved. I think it depends on the role you want the remote repo to play. (And yes, this is in conflict with the documentation here.)

    Caveat: I learned the above (at this writing in early August 2019) by doing a test on my local system with a real repo, and then doing a file-by-file comparison between the results. But! I am still learning, so there could be a more correct way. But my tests have helped me conclude that #1 is my currently-preferred method.

提交回复
热议问题