一、Git 服务器搭建
Github 公开的项目是免费的,2019 年开始 Github 私有存储库也可以无限制使用。
这当然我们也可以自己搭建一台 Git 服务器作为私有仓库使用。
接下来我们将以 Centos 为例搭建 Git 服务器。
1、安装Git
$ yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel
$ yum install git
接下来我们 创建一个git用户组和用户,用来运行git服务:
$ groupadd git
$ useradd git -g git
2、创建证书登录
收集所有需要登录的用户的公钥,公钥位于id_rsa.pub文件中,把我们的公钥导入到/home/git/.ssh/authorized_keys文件里,一行一个。
如果没有该文件创建它:
$ cd /home/git/
$ mkdir .ssh
$ chmod 755 .ssh
$ touch .ssh/authorized_keys
$ chmod 644 .ssh/authorized_keys
3、初始化Git仓库
首先我们选定一个目录作为Git仓库,假定是/home/gitrepo/runoob.git,在/home/gitrepo目录下输入命令:
$ cd /home
$ mkdir gitrepo
$ chown git:git gitrepo/
$ cd gitrepo
$ git init --bare runoob.git
Initialized empty Git repository in /home/gitrepo/runoob.git/
以上命令Git创建一个空仓库,服务器上的Git仓库通常都以.git结尾。然后,把仓库所属用户改为git:
$ chown -R git:git runoob.git
4、克隆仓库
$ git clone git@192.168.45.4:/home/gitrepo/runoob.git
Cloning into 'runoob'...
warning: You appear to have cloned an empty repository.
Checking connectivity... done.
192.168.45.4 为 Git 所在服务器 ip ,你需要将其修改为你自己的 Git 服务 ip。
这样我们的 Git 服务器安装就完成。
二、Git 基本操作
获取与创建项目命令git init
git config --global user.name "账户名称"
git config --global user.email "邮箱地址"
git config --global credential.helper store //本地记住账户
git add 命令可将该文件添加到缓存,如我们添加以下两个文件:
git status 以查看在你上次提交之后是否有修改。
git diff 来查看执行 git status 的结果的详细信息。
git commit 将缓存区内容添加到仓库中。
git commit -a 如果你觉得 git add 提交缓存的流程太过繁琐,Git 也允许你用 -a 选项跳过这一步。命令格式如下:
git rm -f 如果删除之前修改过并且已经放到暂存区域的话,则必须要用强制删除选项 -f
三、分支操作小结
Git鼓励大量使用分支:
查看分支:git branch
创建分支:git branch
切换分支:git checkout
创建+切换分支:git checkout -b
合并某分支到当前分支:git merge
删除分支:git branch -d
四、几种效果等价的git clone写法:
git clone http://github.com/CosmosHua/locate new
git clone http://github.com/CosmosHua/locate.git new
git clone git://github.com/CosmosHua/locate new
git clone git://github.com/CosmosHua/locate.git new
领取专属 10元无门槛券
私享最新 技术干货