git 如何添加本地仓库并将其视为远程仓库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10603671/
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 add a local repo and treat it as a remote repo
提问by opensas
I'm trying to make a local repo act as a remote with the name bak
for another local repo on my PC, using the following:
我正在尝试使用以下命令将本地存储库用作bak
我 PC 上另一个本地存储库名称的远程存储库:
git remote add /home/sas/dev/apps/smx/repo/bak/ontologybackend/.git bak
which gives this error:
这给出了这个错误:
fatal: '/home/sas/dev/apps/smx/repo/bak/ontologybackend/.git' is not a valid remote name
I'm trying to sync two local repos, with one configured as a remote named bak
for the other, and then issuing git pull bak
.
我正在尝试同步两个本地存储库,其中一个配置为以bak
另一个命名的远程存储库,然后发出git pull bak
.
What is the best way to do it?
最好的方法是什么?
Edit:
编辑:
Sorry, silly me, I've just realized the remote add should be:
对不起,我傻了,我刚刚意识到远程添加应该是:
git remote add bak /home/sas/dev/apps/smx/repo/bak/ontologybackend/.git
the name of the remote goes beforethe address.
遥控器的名称在地址之前。
回答by larsks
You have your arguments to the remote add
command reversed:
您对remote add
命令的参数进行了颠倒:
git remote add <NAME> <PATH>
So:
所以:
git remote add bak /home/sas/dev/apps/smx/repo/bak/ontologybackend/.git
See git remote --help
for more information.
有关git remote --help
更多信息,请参阅。
回答by Matt Sanders
If your goal is to keep a local copy of the repository for easy backup or for sticking onto an external drive or sharing via cloud storage (Dropbox, etc) you may want to use a bare repository. This allows you to create a copy of the repository without a working directory, optimized for sharing.
如果您的目标是保留存储库的本地副本以便于备份或粘贴到外部驱动器或通过云存储(Dropbox 等)共享,您可能需要使用裸存储库。这允许您在没有工作目录的情况下创建存储库的副本,并针对共享进行了优化。
For example:
例如:
$ git init --bare ~/repos/myproject.git
$ cd /path/to/existing/repo
$ git remote add origin ~/repos/myproject.git
$ git push origin master
Similarly you can clone as if this were a remote repo:
同样,您可以像克隆一个远程仓库一样进行克隆:
$ git clone ~/repos/myproject.git
回答by Kristian
It appears that your format is incorrect:
看来你的格式不正确:
If you want to share a locally created repository, or you want to take contributions from someone elses repository - if you want to interact in any way with a new repository, it's generally easiest to add it as a remote. You do that by running git remote add [alias] [url]. That adds [url] under a local remote named [alias].
如果您想共享一个本地创建的存储库,或者您想从其他人的存储库中获取贡献 - 如果您想以任何方式与新存储库交互,通常最容易将其添加为远程存储库。你可以通过运行 git remote add [alias] [url] 来做到这一点。这会在名为 [alias] 的本地远程下添加 [url]。
#example
$ git remote
$ git remote add github [email protected]:schacon/hw.git
$ git remote -v
回答by Jarek
I am posting this answer to provide a script with explanations that covers three different scenarios of creating a local repo that has a local remote. You can run the entire script and it will create the test repos in your home folder (tested on windows git bash). The explanations are inside the script for easier saving to your personal notes, its very readable from, e.g. Visual Studio Code.
我发布此答案是为了提供一个脚本,其中包含创建具有本地远程的本地存储库的三种不同场景的解释。您可以运行整个脚本,它将在您的主文件夹中创建测试存储库(在 Windows git bash 上测试)。解释在脚本内部,以便于保存到您的个人笔记中,它的可读性非常强,例如 Visual Studio Code。
I would also like to thank Hymanfor linking to this answerwhere adelphushas good, detailed, hands on explanations on the topic.
我还要感谢Hyman链接到这个答案,其中adelphus对这个主题有很好的、详细的、动手的解释。
This is my first post here so please advise what should be improved.
这是我在这里的第一篇文章,所以请建议应该改进什么。
## 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
}