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

tkclm6bt  于 2023-01-11  发布在  Git
关注(0)|答案(4)|浏览(144)

我正在尝试使用以下代码使本地存储库充当我PC上另一个本地存储库的远程存储库,其名称为bak

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

我尝试同步两个本地repos,其中一个配置为另一个名为bak的远程repos,然后发出git pull bak
做这件事最好的方法是什么?

lkaoscv7

lkaoscv71#

您颠倒了remote add命令的参数:

git remote add <NAME> <PATH>

因此,

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

有关详细信息,请参见git remote --help

wgeznvg7

wgeznvg72#

如果你的目标是保存一个本地存储库副本以方便备份,或者粘贴到外部驱动器上,或者通过云存储(Dropbox等)共享,你可能需要使用bare repository。这样你就可以创建一个没有工作目录的存储库副本,并为共享进行优化。
例如:

$ 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
cdmah0mi

cdmah0mi3#

我发布这个答案是为了提供一个脚本,其中包含了创建一个本地存储库的三种不同场景的解释。你可以运行整个脚本,它会在你的主文件夹中创建一个测试存储库(在windows git bash上测试过)。解释在脚本中,便于保存到你的个人笔记中,它非常可读,例如Visual Studio代码。
我还要感谢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
}
km0tfn4u

km0tfn4u4#

您的格式似乎不正确:
如果你想共享一个本地创建的仓库,或者你想从别人的仓库中获取贡献--如果你想以任何方式与一个新仓库交互,通常最简单的方法是将它添加为远程仓库,你可以运行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

相关问题