【问题标题】:How to push a local Git repository to another computer?如何将本地 Git 存储库推送到另一台计算机?
【发布时间】:2011-02-22 16:49:16
【问题描述】:

我的笔记本电脑上有一个本地 Git 存储库设置。我想把它推到我的桌面上。

我该怎么做?

【问题讨论】:

  • git 应该在 superusers.com 中
  • 您阅读手册页了吗?输入git help push?
  • @vodkhang:不,它不应该在超级用户上。它是开发人员工具箱和他/她的编程环境的重要组成部分。
  • 嗯,没错。我只是想,因为它涉及到一些管理的东西,所以它应该在那个页面上:)
  • @vodkhang 我也在 serverfault.com 上问过一些关于 git setup 的问题,但我认为真正掌握 git 知识的真正 git 用户都在这里。

标签: git git-push


【解决方案1】:

这是我写的一个脚本来做这件事。该脚本处理我所有通常的新 git repos 初始化

  1. 创建 .gitignore 文件
  2. 初始化.git
  3. 在服务器上创建裸 git repo
  4. 设置本地 git 存储库以推送到该远程存储库

http://gist.github.com/410050

您肯定需要对其进行修改以适应您所拥有的任何设置,尤其是在您使用 Windows 笔记本电脑/台式机时。

这是完整的脚本:

#!/bin/bash
# Create Git Repository
# created by Jim Kubicek, 2009
# jimkubicek@gmail.com
# http://jimkubicek.com

# DESCRIPTION
# Create remote git repository from existing project
# this script needs to be run from within the project directory

# This script has been created on OS X, so YMMV

#######
# Parameters
REPLOGIN=#Login name
REPADDRESS=#Repo address
REPLOCATION=/Users/Shared/Development #Repo location

# The repo name defaults to the name of the current directory.
# This regex will accept foldernames with letters and a period.
# You'll have to edit it if you've got anything else in your folder names.
REPNAME=`pwd | egrep -o "/[a-zA-Z]+$" | egrep -o "[a-zA-Z\.]+"`


# If you have standard files/directories to be ignored
# add them here
echo "Creating .gitignore"
echo 'build/' >> .gitignore # The build directory should be ignored for Xcode projs
echo '.DS_Store' >> .gitignore # A good idea on OS X

# Create the git repo
echo "Initializing the repo"
git init
git add .
git commit -m "Initial commit"

# Copy the repo to the server
echo "Copying the git repo to the server $REPADDRESS"
TEMPREP="$REPNAME.git"
git clone --bare .git $TEMPREP
scp -r $TEMPREP $REPLOGIN@$REPADDRESS:$REPLOCATION/
rm -rf $TEMPREP

# Set up the origin for the project
echo "Linking current repository to remote repository"
git remote add origin $REPLOGIN@$REPADDRESS:$REPLOCATION/$REPNAME.git/

【讨论】:

  • 我建议在您的答案中完整复制您的脚本(因为它不是太大)。一旦 Stack Overflow 发布下一个 cc-wiki 转储 (blog.stackoverflow.com/2009/06/…),您就可以确定此脚本将始终可用。否则,+1
【解决方案2】:

最简单(不是最好)的方式是通过局域网共享仓库目录,使用git的file://协议(见man git)。

对我来说,最好的方法是使用gitolite(详细说明请参见gitolite docs)。

【讨论】:

    【解决方案3】:

    如果您有权访问共享目录,则可以(参见git clonegit remote):

    git clone --bare /path/to/your/laptop/repo /shared/path/to/desktop/repo.git
    git remote add desktop  /shared/path/to/desktop/repo.git
    

    这将创建一个bare repo,在您的本地存储库中引用为“桌面”。
    由于它是裸露的,你可以推到它(如果需要也可以从中拉出)

    git push desktop
    

    作为ProGit book mentions,git确实支持文件协议:

    最基本的是 Local 协议,其中远程存储库位于磁盘上的另一个目录中。
    如果团队中的每个人都可以访问共享文件系统(例如 NFS 挂载),或者在不太可能的情况下每个人都登录到同一台计算机,这通常会使用。

    【讨论】:

    • git push desktop 表示推送 from 笔记本电脑 to 台式机。绝对不需要bare repo。
    • @Timo 是的,自 2015 年以来,非裸回购是可能的:stackoverflow.com/a/30030236/6309
    • 如果我无权访问共享目录怎么办?我想做类似git clone . ssh://user@desktop:git/remoteRepo.git 的事情(来自我的本地仓库)。或者这只能通过登录我的桌面并在那里创建一个(裸)repo 来完成?
    • @AstroFloyd 8 年后,您可以使用 SSH,考虑到 Windows 10 带有 OpenSSH 服务器(bleepingcomputer.com/news/microsoft/…bleepingcomputer.com/news/microsoft/…
    • @VonC 这里不需要 Windows; OpenSSH 在 Linux 上已经存在了很长时间。但是git clone . ssh://user@desktop:git/remoteRepo.git 在名为 ssh:/user@desktop:git/remoteRepo.git 的子目录中创建了一个本地存储库,而我想要一个 remote 存储库 - 这就是重点。那么如何通过 ssh 连接到远程机器,而不是创建一个名为 ssh 的目录。
    猜你喜欢
    • 2022-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-27
    • 2011-05-07
    • 2012-11-07
    • 2023-04-01
    • 1970-01-01
    相关资源
    最近更新 更多