Linux git 在自己的服务器上建立 git 仓库(repository)

Linux git 在自己的服务器上建立 git 仓库(repository)

服务器端:

在这里使用 ssh 方式登陆:

ssh [username]@server_address(建议用超级用户登陆)

提示输入密码:......

进入某个文件夹(如果没有 /git 文件夹,则自己创建)

cd /git 

在 /git 文件夹下创建 git 仓库文件夹

mkdir testRepository.git 

初始化服务端 git 仓库:

cd testRepository.git/

git --bare init

返回父目录:

cd ..

查看文件的详细信息:

ls -l 

修改 testRepository.git 的用户组:

chgrp [group] testRepository.git -R

ls -l

修改 testRepository.git 的所有者(如果有需要的话):

chown [user] testRepository.git -R

修改 testRepository.git 的 mod 权限

chmod  770 testRepository.git/ -R(770代表 owner权限:7,用户组权限:7,other用户权限:0)

chmod  775 testRepository.git/ -R(775 用于 shared 的仓库, 具体查阅 git-daemon)

(重要)设置默认新建的文件和文件夹同属于其父目录的用户组:

chmod g+s <directory> -R  #以后新push的内容都会默认为属于父目录的组

(重要)设置文件夹子目录和文件继承父目录访问权限:

set -R -d -m g:[group_name]:rwx <directory>  #以后在<directory>中创建的文件夹都会继承父目录的访问属性,这样在多人合作的git项目中push之后不会出现有时候不能访问的情况

本机端:

在自己的文件夹下(例如 testRepository),打开git bash

初始化为一个本地仓库:

cd testRepository

git init

这时候会生成一个 .git 的隐藏文件,打开里面的 config :

[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true

与连接到远程仓库,也就是让本地仓库与服务器仓库连接起来:

git remote add origin ssh://server_address/git/testRepository.git

这时候从新打开 config:

[core]
  repositoryformatversion = 0
  filemode = false
  bare = false
  logallrefupdates = true
  symlinks = false
  ignorecase = true
[remote "origin"]
  url = ssh://username@server_address/git/testRepository.git
  fetch = +refs/heads/*:refs/remotes/origin/*

随便添加一些内容,添加追踪文件:

git add -A

提交:

git commit -m 'first commit'

第一次把本地仓库的内容推送到服务器仓库,也就是设置 git push 的默认远程分支和本地分支:

git push -u origin master  #-u : --set-upstream,就是把本地一个分支和远程的一个分支关联起来,在 config 中会有相应的记录

以后修改提交就可以不用任何参数:

git push

 

原文地址:https://www.cnblogs.com/ibingshan/p/10006946.html