简体   繁体   中英

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. 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 cd ${PROJECT}.git; git init --bare
  2. create an empty 'repo' "mynewproject", also on the NAS cd ${PROJECT}; git init cd ${PROJECT}; git init
  3. make an initial commit in "newproject" 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
  5. push to master git push origin master
  6. delete the 'project' directory rm -rf $PROJECT

And then I can clone the ${PROJECT}.git repo from other machines. 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

  1. Create a bare repo on the NAS: cd ${PROJECT}.git; git init --bare cd ${PROJECT}.git; git init --bare .
  2. Clone the ${PROJECT}.git repo on another machine. 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 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 .
  5. Push the master from the other machine to the bare repo on the NAS: git push origin master .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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