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

351

我正在尝试使用以下命令将本地存储库作为名为 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 命令。

最好的方法是什么?

4个回答

397

你把remote add命令的参数顺序颠倒了:

git remote add <NAME> <PATH>
所以:
git remote add bak /home/sas/dev/apps/smx/repo/bak/ontologybackend/.git

请查看git remote --help以获取更多信息。


12
需要在结尾加上 .git 吗? - user187676
5
这只是一条路径……Git 不在意它的命名。 - larsks
13
看起来你需要使用绝对路径,这对我来说不是很明显。当我尝试使用相对路径时,出现了"fatal: '../dir' does not appear to be a git repository"的错误提示。 - Keith Layne
10
重要的是在路径前面加上 file:// 并使用完整的本地存储库路径,以便客户端软件可以通过预期的协议访问它。而且,回答Erik上面的问题,路径末尾的 .git 显然是必需的。 - Scott Lahteine
7
至少在 git 2.25+(我安装的版本)中,您不需要设置包含 git 元数据(.git)的目录的完整路径。因此,在这个例子中,只需使用 git remote add bak /home/sas/dev/apps/smx/repo/bak/ontologybackend 即可。 - Mariano Ruiz
显示剩余4条评论

239
如果你的目标是为了轻松备份或将代码库存储到外部驱动器或通过云存储(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

19
这应该被接受为答案,因为它完美地适用于问题“最好的方法是什么?”。“本地存储库被视为远程存储库”,正如@opensas所称,确实是一个空目录(就像真正的远程存储库)。 - Jack'
2
我建议进行编辑:你应该使用“git remote add...”+“git push”还是只用“git clone”在这里有说明:https://dev59.com/ZWsz5IYBdhLWcg3w0rZC#31590993(adelphus的回答) - Jack'
2
@Jack - 你能详细说明一下你觉得什么地方有点困惑吗?我很乐意进行修改,但希望保持答案相对简洁。 - Matt Sanders
1
注意:裸仓库将您的文件隐藏在git blob中。要查看它们,请键入:git show $(git rev-parse HEAD):your_filename - SurpriseDog

15
我发布这个答案,提供一个带有解释的脚本,涵盖了创建具有本地远程的本地repo的三种不同情况。您可以运行整个脚本,它将在您的主文件夹中创建测试repo(在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
}


6
欢迎!感谢您抽出时间来做这件事。您愿意参与并回馈我们的社区是 SO 的关键所在。 - cb4
是的,在这种情况下,明确表达非常有帮助。 git clone 命令本身在幕后执行了一些中间步骤,逐个检查这些步骤有助于更好地理解详细的机制。 - dave campbell

5

看起来你的格式不正确:

如果你想分享一个本地创建的仓库,或者你想从别人的仓库中获取贡献 - 如果你想以任何方式与新仓库进行交互,通常最容易的方法是将其添加为远程仓库。你可以通过运行git remote add [alias] [url] 来实现。这会在本地远程名称为[alias]的位置下添加[url]。

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

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


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