如何将本地 Git 存储库推送到另一台计算机?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2888029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to push a local Git repository to another computer?
提问by nubela
I have a local Git repository setup on my laptop. I would like to push it to my desktop.
我的笔记本电脑上有一个本地 Git 存储库设置。我想把它推送到我的桌面。
How can I do that?
我怎样才能做到这一点?
采纳答案by VonC
If you have access to a shared directory, you can (see git clone
and git remote
):
如果您有权访问共享目录,则可以(参见git clone
和git 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
That will create a bare repo, referenced in your local repo as "desktop".
Since it is bare, you can push to it (as well as pull from it if needed)
这将创建一个裸仓库,在您的本地仓库中称为“桌面”。
由于它是裸露的,您可以推到它(如果需要,也可以从它拉出)
git push desktop
As the ProGit book mentions, git does support the file protocol:
正如ProGit 书中提到的,git 确实支持文件协议:
The most basic is the Localprotocol, in which the remote repository is in another directory on disk.
This is often used if everyone on your team has access to a shared filesystem such as an NFS mount, or in the less likely case that everyone logs in to the same computer.
最基本的是本地协议,其中远程存储库位于磁盘上的另一个目录中。
如果团队中的每个人都可以访问共享文件系统(例如 NFS 挂载),或者在不太可能的情况下每个人都登录到同一台计算机,则通常会使用此方法。
回答by kubi
Here's a script that I wrote to do just this thing. The script handles all my usual initialization of new git repos
这是我写的一个脚本来做这件事。该脚本处理我对新 git 存储库的所有常用初始化
- creates .gitignore file
- initializes .git
- creates the bare git repo on the server
- sets up the local git repo to push to that remote repo
- 创建 .gitignore 文件
- 初始化 .git
- 在服务器上创建裸git repo
- 设置本地 git 存储库以推送到该远程存储库
You'll definitely have to modify it to suit whatever setup you've got, especially if you're dealing with Windows laptop/desktop.
您肯定必须修改它以适应您拥有的任何设置,特别是如果您使用的是 Windows 笔记本电脑/台式机。
Here is the full script:
这是完整的脚本:
#!/bin/bash
# Create Git Repository
# created by Jim Kubicek, 2009
# [email protected]
# 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/
回答by takeshin
The easiest (not the best) way is to share the repository directory via LAN, and use git's file://
protocol (see man git
).
最简单(不是最好)的方法是通过 LAN 共享存储库目录,并使用 git 的file://
协议(请参阅参考资料man git
)。
For me, the best way is to use gitolite
(see gitolite docsfor detailed instructions).
对我来说,最好的方法是使用gitolite
(有关详细说明,请参阅gitolite 文档)。