【问题标题】:How to add a local repo and treat it as a remote repo如何添加本地仓库并将其视为远程仓库
【发布时间】:2016-10-09 09:22:16
【问题描述】:

我正在尝试使本地存储库充当名称为 bak 的远程存储库,用于我的 PC 上的另一个本地存储库,使用以下命令:

git remote add /home/sas/dev/apps/smx/repo/bak/ontologybackend/.git bak

这给出了这个错误:

fatal: '/home/sas/dev/apps/smx/repo/bak/ontologybackend/.git' is not a valid remote name

我正在尝试同步两个本地存储库,其中一个配置为名为 bak 的远程存储库,然后发出 git pull bak。

最好的方法是什么?


编辑:

对不起,我傻了,我刚刚意识到远程添加应该是:

git remote add bak /home/sas/dev/apps/smx/repo/bak/ontologybackend/.git

遥控器的名称在地址之前。

【问题讨论】:

    标签: git git-remote


    【解决方案1】:

    我发布此答案是为了提供一个脚本,其中包含创建具有本地远程的本地存储库的三种不同场景的解释。您可以运行整个脚本,它将在您的主文件夹中创建测试存储库(在 windows git bash 上测试)。解释在脚本中,以便更轻松地保存到您的个人笔记中,它非常易读,例如Visual Studio 代码。

    我还要感谢Jack 链接到this answer,其中adelphus 对该主题有很好、详细、实用的解释。

    这是我在这里的第一篇文章,所以请提出应该改进的地方。

    ## SETUP LOCAL GIT REPO WITH A LOCAL REMOTE
    # the main elements:
    # - remote repo must be initialized with --bare parameter
    # - local repo must be initialized
    # - local repo must have at least one commit that properly initializes a branch(root of the commit tree)
    # - local repo needs to have a remote
    # - local repo branch must have an upstream branch on the remote
    
    { # the brackets are optional, they allow to copy paste into terminal and run entire thing without interruptions, run without them to see which cmd outputs what
    
    cd ~
    rm -rf ~/test_git_local_repo/
    
    ## Option A - clean slate - you have nothing yet
    
    mkdir -p ~/test_git_local_repo/option_a ; cd ~/test_git_local_repo/option_a
    git init --bare local_remote.git # first setup the local remote
    git clone local_remote.git local_repo # creates a local repo in dir local_repo
    cd ~/test_git_local_repo/option_a/local_repo
    git remote -v show origin # see that git clone has configured the tracking
    touch README.md ; git add . ; git commit -m "initial commit on master" # properly init master
    git push origin master # now have a fully functional setup, -u not needed, git clone does this for you
    
    # check all is set-up correctly
    git pull # check you can pull
    git branch -avv # see local branches and their respective remote upstream branches with the initial commit
    git remote -v show origin # see all branches are set to pull and push to remote
    git log --oneline --graph --decorate --all # see all commits and branches tips point to the same commits for both local and remote
    
    ## Option B - you already have a local git repo and you want to connect it to a local remote
    
    mkdir -p ~/test_git_local_repo/option_b ; cd ~/test_git_local_repo/option_b
    git init --bare local_remote.git # first setup the local remote
    
    # simulate a pre-existing git local repo you want to connect with the local remote
    mkdir local_repo ; cd local_repo
    git init # if not yet a git repo
    touch README.md ; git add . ; git commit -m "initial commit on master" # properly init master
    git checkout -b develop ; touch fileB ; git add . ; git commit -m "add fileB on develop" # create develop and fake change
    
    # connect with local remote
    cd ~/test_git_local_repo/option_b/local_repo
    git remote add origin ~/test_git_local_repo/option_b/local_remote.git
    git remote -v show origin # at this point you can see that there is no the tracking configured (unlike with git clone), so you need to push with -u
    git push -u origin master # -u to set upstream
    git push -u origin develop # -u to set upstream; need to run this for every other branch you already have in the project
    
    # check all is set-up correctly
    git pull # check you can pull
    git branch -avv # see local branch(es) and its remote upstream with the initial commit
    git remote -v show origin # see all remote branches are set to pull and push to remote
    git log --oneline --graph --decorate --all # see all commits and branches tips point to the same commits for both local and remote
    
    ## Option C - you already have a directory with some files and you want it to be a git repo with a local remote
    
    mkdir -p ~/test_git_local_repo/option_c ; cd ~/test_git_local_repo/option_c
    git init --bare local_remote.git # first setup the local remote
    
    # simulate a pre-existing directory with some files
    mkdir local_repo ; cd local_repo ; touch README.md fileB
    
    # make a pre-existing directory a git repo and connect it with local remote
    cd ~/test_git_local_repo/option_c/local_repo
    git init
    git add . ; git commit -m "inital commit on master" # properly init master
    git remote add origin ~/test_git_local_repo/option_c/local_remote.git
    git remote -v show origin # see there is no the tracking configured (unlike with git clone), so you need to push with -u
    git push -u origin master # -u to set upstream
    
    # check all is set-up correctly
    git pull # check you can pull
    git branch -avv # see local branch and its remote upstream with the initial commit
    git remote -v show origin # see all remote branches are set to pull and push to remote
    git log --oneline --graph --decorate --all # see all commits and branches tips point to the same commits for both local and remote
    }
    
    

    【讨论】:

    • 欢迎您!并感谢您花时间整理这些。您愿意参与并回馈我们的社区是 SO 的基石。
    • 是的,在这种情况下,明确表示非常有帮助。 git clone 命令本身会在幕后执行其中一些中间步骤,看起来,并且逐个查看有助于更好地理解详细的机制。
    【解决方案2】:

    如果您的目标是保留存储库的本地副本以便于备份或粘贴到外部驱动器或通过云存储(Dropbox 等)共享,您可能需要使用bare repository。这允许您在没有工作目录的情况下创建存储库的副本,并针对共享进行了优化。

    例如:

    $ git init --bare ~/repos/myproject.git
    $ cd /path/to/existing/repo
    $ git remote add origin ~/repos/myproject.git
    $ git push origin master
    

    同样,您可以像远程仓库一样进行克隆:

    $ git clone ~/repos/myproject.git
    

    【讨论】:

    • 这应该是公认的答案,因为它完全符合“什么是最好的方法?”的问题。正如@opensas 所说,“本地仓库被视为远程仓库”确实是一个裸目录(就像一个真正的远程仓库一样)
    • 我建议进行编辑:这里指出您应该使用“git remot add..”+“git push”还是只使用“git clone”:stackoverflow.com/a/31590993/5446285(adelphus 的回答)
    • @Jack - 你能详细说明你发现的困惑吗?我很乐意修改,但希望答案相对简洁。
    • 注意:裸存储库会将您的文件隐藏在 git blob 中。要查看它们,请输入:git show $(git rev-parse HEAD):your_filename
    【解决方案3】:

    您对 remote add 命令的论点已反转:

    git remote add <NAME> <PATH>
    

    所以:

    git remote add bak /home/sas/dev/apps/smx/repo/bak/ontologybackend/.git
    

    请参阅git remote --help 了解更多信息。

    【讨论】:

    • 到底是不是特别需要.git?
    • 它只是一个路径...Git 不在乎它的名字。
    • 看来您需要使用绝对路径,这对我来说并不明显。当我尝试使用相对路径时,我得到了fatal: '../dir' does not appear to be a git repository。
    • 将file:// 放在路径的前面并使用本地存储库的完整路径非常重要,这样客户端软件就可以通过预期的协议访问它。在回答 Erik 的上述问题时,显然需要路径末尾的 .git。
    • 至少在 git 2.25+(我已经安装的版本)中,您不需要使用 git 元数据所在的目录(.git)设置完整路径。所以在这个例子中,git remote add bak /home/sas/dev/apps/smx/repo/bak/ontologybackend 就足够了
    【解决方案4】:

    您的格式似乎不正确:

    如果您想共享本地创建的存储库,或者您想从其他人的存储库中获取贡献 - 如果您想 以任何方式与新存储库交互,通常最容易 将其添加为遥控器。你可以通过运行 git remote add [alias] [网址]。这会在名为 [alias] 的本地远程服务器下添加 [url]。

    #example
    $ git remote
    $ git remote add github git@github.com:schacon/hw.git
    $ git remote -v
    

    http://gitref.org/remotes/#remote

    【讨论】:

      猜你喜欢
      • 2021-04-07
      • 1970-01-01
      • 2018-02-06
      • 2018-12-05
      • 2019-06-22
      • 2012-06-26
      • 1970-01-01
      • 2021-05-21
      相关资源
      最近更新 更多