如何添加本地存储库并将其视为远程存储库


234

我正在尝试使用以下命令使本地存储库充当远程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

遥控器的名称在地址之前

Answers:


273

您可以将remote add命令的参数取反:

git remote add <NAME> <PATH>

所以:

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

请参阅git remote --help以获取更多信息。


6
最后是否.git特别要求?
Erik Aigner

5
这只是一条路... Git不在乎它的名字。
larsks 2013年

2
传统上,@ ErikAigner的裸存储区将以“ .git”后缀结尾。尽管通常不是它自己的目录,而是:“ /path/to/projectname.git”。-除此之外,没有什么区别。
Atli

7
看来您需要使用绝对路径,这对我而言并不明显。当我尝试相对路径时,我得到了fatal: '../dir' does not appear to be a git repository
基思·莱恩

1
重要的是放在file://路径的最前面,并使用本地存储库的完整路径,以便客户端软件可以通过预期的协议访问它。回答上面Erik的问题,.git显然需要路径的尽头。
Scott Lahteine

158

如果您的目标是保留存储库的本地副本以方便备份,粘贴到外部驱动器或通过云存储(Dropbox等)共享,则可能要使用裸存储库。这使您可以创建没有工作目录的存储库副本,该目录针对共享进行了优化。

例如:

$ 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

9
这应该是公认的答案,因为它完全适合以下问题:“最好的方法是什么?”。作为@opensas把它称为“视为远程回购本地回购”,确实是一个裸露的目录(就像一个真正的远程存储库)
杰克

1
我建议编辑:你是否应该使用“混帐REMOT加..” +“混帐推”或只是“混帐克隆”在这里表示:stackoverflow.com/a/31590993/5446285(阿德尔菲亚斯的回答)
杰克

1
@Jack-您能详细说明感到困惑的地方吗?我很乐意修改,但想保持答案相对简洁。
马特·桑德斯

6

看来您的格式不正确:

如果要共享在本地创建的存储库,或者要从其他人的存储库中获取内容-如果要以任何方式与新存储库进行交互,通常最简单的方法是将其添加为远程存储库。您可以通过运行git remote add [alias] [url]来实现。这会将[url]添加到名为[alias]的本地远程目录下。

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

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


0

我将发布此答案,以提供带有说明的脚本,该脚本涵盖创建具有本地遥控器的本地存储库的三种不同方案。您可以运行整个脚本,它将在您的主文件夹中创建测试存储库(在Windows git bash上进行了测试)。解释在脚本内,可以更轻松地保存到您的个人笔记中,例如,从Visual Studio Code即可读取。

我还要感谢Jack链接到这个答案,其中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
}

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.