如何将本地Git仓库推送到另一台电脑?

67

我在我的笔记本电脑上设置了一个本地的Git仓库,我想将它推送到我的台式机上。

我应该如何操作呢?


4
Git应该在superusers.com上。 - vodkhang
1
你有读过手册吗?输入了 git help push 命令了吗? - crazyscot
15
不应该把它放在超级用户上。它是开发人员工具箱和编程环境的重要组成部分。 - VonC
嗯,那可能是对的。我只是认为因为它涉及到一些管理事项,所以应该放在那个页面上 :) - vodkhang
6
@vodkhang,我在serverfault.com上也问了一些关于git设置的问题,但我认为真正了解git的最好用户在这里。 - takeshin
3个回答

59
如果您有访问共享目录的权限,您可以执行以下操作(参见 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

这将创建一个裸库, 在您的本地存储库中引用为"desktop"。
由于它是裸的,您可以向其推送(如果需要,也可以从中拉取)

git push desktop

正如ProGit书籍所提到的那样,git支持文件协议:

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


@AstroFloyd 八年后,你可以使用SSH,因为Windows 10带有OpenSSH服务器(https://www.bleepingcomputer.com/news/microsoft/heres-how-to-enable-the-built-in-windows-10-openssh-client/,https://www.bleepingcomputer.com/news/microsoft/how-to-install-the-built-in-windows-10-openssh-server/) - VonC
@VonC 这里不需要Windows;OpenSSH在Linux上已经存在很长时间了。然而,git clone . ssh://user@desktop:git/remoteRepo.git会在名为ssh:/user@desktop:git/remoteRepo.git的子目录中创建一个本地repo,而我想要一个远程repo - 这就是整个问题所在。那么,我该如何通过ssh连接到远程机器,而不是创建一个名为ssh的目录呢? - AstroFloyd
@AstroFloyd,您没有提到您的操作系统。您可以尝试使用以下URL:ssh://user@desktop/full/path/to/git/remoteRepo.git(所以没有“:`”)。 - VonC
@VonC 抱歉,我在本地和远程都使用 Linux。这(完整命令为 git clone . ssh://user@desktop/home/user/path/to/git/remoteRepo.git)会在我的本地框中的当前目录下创建一个子目录树 ./ssh:/user@desktop/home/user/path/to/git/remoteRepo.git... :( - AstroFloyd
@AstroFloyd,你使用的是哪个版本的git?我假设在远程桌面上,路径/home/user/path/to/git/remoteRepo.git是存在的吗? - VonC
显示剩余4条评论

5
这是我编写的一个脚本,可以处理所有新的git仓库的常规初始化:
  1. 创建.gitignore文件
  2. 初始化.git
  3. 在服务器上创建裸git仓库
  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/

2
我建议您在答案中完整复制您的脚本(因为它不太大)。一旦 Stack Overflow 发布下一个 cc-wiki 转储(http://blog.stackoverflow.com/2009/06/stack-overflow-creative-commons-data-dump/),您就可以确保这个脚本将始终可用。否则,+1。 - VonC

2

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

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接