简体   繁体   English

如何正确初始化远程 git 存储库

[英]How to properly initialize a remote git repository

I have a local git server running on my NAS and I'm developing on my laptop and workstation, all in my local network.我有一个本地 git 服务器在我的 NAS 上运行,我正在我的笔记本电脑和工作站上进行开发,所有这些都在我的本地网络中。 So, if I want to start a project in a new empty repo, I'm following this answer and所以,如果我想在一个新的空仓库中开始一个项目,我会关注这个答案并且

  1. create a bare repo "mynewproject.git" on the NAS cd ${PROJECT}.git; git init --bare在 NAS cd ${PROJECT}.git; git init --bare上创建一个裸仓库“mynewproject.git” cd ${PROJECT}.git; git init --bare cd ${PROJECT}.git; git init --bare
  2. create an empty 'repo' "mynewproject", also on the NAS cd ${PROJECT}; git init创建一个空的“repo”“mynewproject”,也在 NAS cd ${PROJECT}; git init cd ${PROJECT}; git init
  3. make an initial commit in "newproject" git add . ; git commit -m "initial commit" -a在“newproject” git add . ; git commit -m "initial commit" -a进行初始提交git add . ; git commit -m "initial commit" -a git add . ; git commit -m "initial commit" -a
  4. make the bare repo the remote origin of the current folder git remote add origin ssh://${USER}@${REMOTEIP}${PROJECT}.git使裸git remote add origin ssh://${USER}@${REMOTEIP}${PROJECT}.git成为当前文件夹的远程源git remote add origin ssh://${USER}@${REMOTEIP}${PROJECT}.git
  5. push to master git push origin master推送到主git push origin master
  6. delete the 'project' directory rm -rf $PROJECT删除“项目”目录rm -rf $PROJECT

And then I can clone the ${PROJECT}.git repo from other machines.然后我可以从其他机器克隆${PROJECT}.git库。 This whole process seems overly complicated.这整个过程似乎过于复杂。 I mean, I scripted it,我的意思是,我编写了它,

HOSTIP=XXX.XXX.XXX.XXX
USER=YYYYY
PROJECT=$1

[[ -z "${PROJECT}" ]] && exit 1

PROJECTNAME=${PROJECT}
PROJECT=$(pwd)/${PROJECT}

# create project and .git folders
mkdir ${PROJECT}
mkdir ${PROJECT}.git

# initialize folders for git
cd ${PROJECT}.git
git init --bare
cd ${PROJECT}
git init

# create initial project directory
echo "#!/bin/bash" > ${PROJECT}/ENV_${PROJECTNAME}.sh
git add .
git commit -m "initial commit" -a

# 'link' to 'remote' .git folder 
git remote add origin ssh://${USER}@${HOSTIP}${PROJECT}.git

# push to master
git push origin master

# delete 'project' directory
rm -rf $PROJECT

echo "CREATED PROJECT $PROJECT.git"
echo "Clone with git clone ssh://${USER}@${HOSTIP}${PROJECT}"

exit 0

but still, is this the right way to go about this?但是,这是解决这个问题的正确方法吗?

Your process is mostly good but a bit overcomplcated.你的过程大多是好的,但有点过于复杂。 You don't need a non-bare repository on the NAS, so your workflow should be您不需要 NAS 上的非裸存储库,因此您的工作流程应该是

  1. Create a bare repo on the NAS: cd ${PROJECT}.git; git init --bare在 NAS 上创建一个裸仓库: cd ${PROJECT}.git; git init --bare cd ${PROJECT}.git; git init --bare . cd ${PROJECT}.git; git init --bare
  2. Clone the ${PROJECT}.git repo on another machine.在另一台机器上克隆${PROJECT}.git库。 This adds the remote to the non-bare repo.这会将遥控器添加到非裸存储库中。
  3. Instead of clonning you can create an empty non-bare repo on the other machine and add remote: cd ${PROJECT}; git init; git remote add origin ssh://${USER}@${HOSTIP}${PROJECT}.git您可以在另一台机器上创建一个空的非裸仓库,而不是克隆,并添加 remote: cd ${PROJECT}; git init; git remote add origin ssh://${USER}@${HOSTIP}${PROJECT}.git cd ${PROJECT}; git init; git remote add origin ssh://${USER}@${HOSTIP}${PROJECT}.git cd ${PROJECT}; git init; git remote add origin ssh://${USER}@${HOSTIP}${PROJECT}.git . cd ${PROJECT}; git init; git remote add origin ssh://${USER}@${HOSTIP}${PROJECT}.git
  4. Make an initial commit in the cloned non-bare repo on the other machine: git add . ; git commit -m "initial commit" -a在另一台机器上克隆的非裸存储库中进行初始提交: git add . ; git commit -m "initial commit" -a git add . ; git commit -m "initial commit" -a git add . ; git commit -m "initial commit" -a . git add . ; git commit -m "initial commit" -a
  5. Push the master from the other machine to the bare repo on the NAS: git push origin master .将 master 从另一台机器推送到 NAS 上的裸仓库: git push origin master

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM