1.版本控制系统
分布式相比于集中式的最大区别是:每个开发者通过克隆(git clone)方式,在本地机器上拷贝一个完整的Git仓库(版本库);后者是用“中央服务器”.
1.1 集中式版本控制系统
产品:CVS、SVN(免费), 收费ClearCase(IBM)、VSS(微软)
优点:
缺点:速度慢,须联网才能使用;分支的管控方式不灵活;
1.2 分布式版本控制系统
产品:Git
优点:支持离线,本地先修改,可以等网络连接后再push代码; 强大分支管理;
缺点:
注意:所有的版本控制系统,其实只能跟踪【文本文件】的改动,比如TXT文件,网页,所有的程序代码等.
2.安装和配置
2.1 安装
$ sudo apt-get install git
2.2 配置
(1)命令形式
$ git config --global user.name "Your Name"
$ git config --global user.email "Your Mail Account"
(2).gitconfig文件形式
$ cd ~
$ ls -la | grep git
$ vim .gitconfig
出现内容:
[user]
name = Your Name
email = Your Mail Account
[core]
editor = vim
[color]
ui = auto
2.3 生成key
$ cd ~/.ssh
$ gitconfig --global -l #查看email
$ ssh-keygen -t rsa -C "c_scong@qti.qualcomm.com" #生成key,需要输入的地方点击回车即可
Generating public/private rsa key pair.
Enter file in which to save the key (/home/tssh/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/tssh/.ssh/id_rsa.
Your public key has been saved in /home/tssh/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:pu6O12E8njVbbi4O5krSVRe/lsn18mV/Ix1lGnUaltY c_scong@qti.qualcomm.com
The key's randomart image is:
+---[RSA 2048]----+
| . ooo|
| +o+E|
| . ..+ +|
| . . . Oo|
| .S O.+|
| . += o ...++|
| . ++o= = . o+|
| =.o+.o o . o|
| .+=....+. |
+----[SHA256]-----+
$ ssh-add id_rsa #添加私钥 #这里好像没成功
Could not open a connection to your authentication agent.
$ cat id_rsa.pub #查看公钥
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCrXbWn+rUlxC3lcBk/FI9iL1H3sl6Tlv2v4AzJYkiUKewLsmWkxqNEBvt/hYy5OLjyGTho+3FxMCpOwpChglcA1jc3hdGB8Mf/St+O8wXgMa1a5uoqTdNbSrT+figULTDgG54QC3DW+eUpuZCvUJSSjl35DyI7T5c7ygsFDPmjvwN3nGix1Yi7b+obsWsr49jM0D1SmqQKEUVa9YwbIi9CjncPjCWqzZgtC7bgvd21uDCshBBwqSTiPpvt3Q1ZA9o8sSIhQ8RpnIWQTWiTh2hJsIz31JRWubawvm4upwzzqyiJjBwCs5uRP/I4zhNs28e1eKhWcPjgaSWq5vBCFUt9 c_scong@qti.qualcomm.com
3.远程仓库
电脑1:192.168.29.133
3.1 初始化一个Git仓库---git init
仓库(repository),又名版本库,简单理解成一个目录,这个目录里面的所有文件都可以被Git管理起来
$ sudo mkdir -p 8_Git_Learning/git_server
$ cd 8_Git_Learning/git_server/
$ sudo git init
Initialized empty Git repository in /work/8_Git_Learning/git_server/.git/
git init之后,在该目录下生成了.git
3.2 添加文件到仓库---git add <file>...
编写或拷贝1个文件,一定要放到git_server目录下(子目录也行).
$ sudo touch readme_git.txt
$ git status
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
readme_git.txt
$ sudo git add readme_git.txt
$ git status
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: readme_git.txt
3.3 修改记录---git commit
$ sudo git commit
$ git status
On branch master
nothing to commit, working directory clean
4.本地仓库
4.1 克隆远程仓库---git clone <url>
url支持多种协议,有:ssh, http...
(1)
本地电脑
一台电脑上也是可以克隆多个版本库的,只要不在同一个目录下
电脑1:192.168.29.133
#电脑1 克隆 电脑1
$ sudo mkdir -p 8_Git_Learning/git_client
$ cd 8_Git_Learning/git_client/
$ git clone ../git_server/.git/
Cloning into 'git_server'...
done.
(2)远程电脑
电脑2:192.168.29.163
下面ssh协议:git clone ssh://<username>@<host ip>:<port>/<path>/.git/
#电脑2 克隆 电脑1
$ sudo mkdir -p 8_Git_Learning/git_client
$ cd 8_Git_Learning/git_client/
$ git clone ssh://mszhou@192.168.29.133:22/work_sy/8_Git_Learning/git_server/.git/
Cloning into 'git_server'...
fatal: '/work_sy/8_Git_Learning/git_server/.git/' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
报上面问题:fatal Could not read from remote repository,其原因是:路径写错了,少了/home/mszhou/
$sudo git clone ssh://mszhou@192.168.29.133:22/home/mszhou/work_sy/8_Git_Learning/git_server
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.
Checking connectivity... done.
技巧:SSH 协议端口号设为 22,可以从/etc/ssh/sshd_config查看和修改
4.2 push到远程仓库
电脑2:192.168.29.163
$ cd 8_Git_Learning/git_client/git_server
$ sudo vim readme_git.txt
1st:this is git learning.
$ sudo git add readme_git.txt
$ sudo git commit
1st change readme_git.txt
$ sudo git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 279 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: error: refusing to update checked out branch: refs/heads/master
remote: error: By default, updating the current branch in a non-bare repository
remote: error: is denied, because it will make the index and work tree inconsistent
remote: error: with what you pushed, and will require 'git reset --hard' to match
remote: error: the work tree to HEAD.
remote: error:
remote: error: You can set 'receive.denyCurrentBranch' configuration variable to
remote: error: 'ignore' or 'warn' in the remote repository to allow pushing into
remote: error: its current branch; however, this is not recommended unless you
remote: error: arranged to update its work tree to match what you pushed in some
remote: error: other way.
remote: error:
remote: error: To squelch this message and still keep the default behaviour, set
remote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
To ssh://mszhou@192.168.29.133:22/home/mszhou/work_sy/8_Git_Learning/git_server/.git/
! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to 'ssh://mszhou@192.168.29.133:22/home/mszhou/work_sy/8_Git_Learning/git_server/.git/'
原因:By default, updating the current branch in a non-bare repository remote: error: is denied
大概意思就是:
如果本地仓库(git_client/git_server/.git)直接克隆于远程仓库(git_server/.git),并且将其添加为remote,那之后执行git push操作的时候是会报错的.因为本地仓库不知道远程仓库是否也在对工作副本进行了修改,直接push过去可能造成working copy的冲突
所以,需要创建bare仓库(git_server_bare/.git),bare仓库只记录仓库信息不保存working copy,作为一个中央仓库使用.
5.bare仓库
5.1 创建bare仓库---git clone --bare,并remote到bare仓库---git remote add origin
电脑1:192.168.29.133
作为"中央"仓库,其他机器(包括本机的原始仓库)往这里push,从这里pull
$ mkdir -p 8_Git_Learning/git_server_bare
$ cd git_server_bare/
$ git clone --bare ../git_server ./
Cloning into bare repository '.'...
done.
$ cd ../git_server
$ pwd
/home/mszhou/work_sy/8_Git_Learning/git_server
$ git remote rm origin #删除remote
$ git remote add origin ../git_server_bare #重点:添加remote
$ git remote -v
origin ../git_server_bare (fetch)
origin ../git_server_bare (push)
5.2 克隆bare仓库
电脑2:192.168.29.163
$ pwd
/work/8_Git_Learning/git_client
$ sudo rm -rf git_server/
$ git clone ssh://mszhou@192.168.29.133:22/home/mszhou/work_sy/8_Git_Learning/git_server_bare
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.
Checking connectivity... done.
$ cd git_server_bare
$ git remote -v
origin ssh://mszhou@192.168.29.133:22/home/mszhou/work_sy/8_Git_Learning/git_server_bare (fetch)
origin ssh://mszhou@192.168.29.133:22/home/mszhou/work_sy/8_Git_Learning/git_server_bare (push)
5.3 push到bare仓库
电脑2:192.168.29.163
$ vim readme_git.txt
1st:this is git learning- bare
$ git add .
$ git commit
1st change readme_git.txt-bare
$ git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 290 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To ssh://mszhou@192.168.29.133:22/home/mszhou/work_sy/8_Git_Learning/git_server_bare
338ba98..4c7d5b5 master -> master
5.4 从bare仓库Pull
(1)另一个clinet2做修改并push
$ pwd
/work/8_Git_Learning
$ mkdir git_cleint2
$ cd git_cleint2
$ sudo git clone ssh://mszhou@192.168.29.133:22/home/mszhou/work_sy/8_Git_Learning/git_server_bare
$ cd git_server_bare
$ git log
commit 4c7d5b59fcdcf6275b870501acdcd9feaed298b0
Author: c_scong
Date: Tue Feb 2 17:56:47 2021 +0800
1st change readme_git.txt-bare
commit 338ba981261ec1c28a1ea78ab78caeeee1a2a15e
Author: zhouhao0906
Date: Wed Jan 27 15:44:05 2021 +0800
add one file into server
$ vim readme_git.txt
$ git add .&
$ git commit
2nd change readme_git.txt-bare-client2
$ sudo git push origin master
(2)第一个clinet做pull
$ pwd
/work/8_Git_Learning/git_client/git_server_bare
#pull之前
$ git log
commit 4c7d5b59fcdcf6275b870501acdcd9feaed298b0
Author: c_scong <c_scong@qti.qualcomm.com>
Date: Tue Feb 2 17:56:47 2021 +0800
1st change readme_git.txt-bare
commit 338ba981261ec1c28a1ea78ab78caeeee1a2a15e
Author: zhouhao0906 <zhouhao0906@thundersoft.com>
Date: Wed Jan 27 15:44:05 2021 +0800
add one file into server
$ sudo git pull #做pull
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From ssh://192.168.29.133:22/home/mszhou/work_sy/8_Git_Learning/git_server_bare
4c7d5b5..d848cd8 master -> origin/master
Updating 4c7d5b5..d848cd8
Fast-forward
readme_git.txt | 1 +
1 file changed, 1 insertion(+)
#pull之后
$ git log
commit d848cd84b3dc617181aa0d9c6279274ec93194d1
Author: c_scong
Date: Tue Feb 2 18:19:47 2021 +0800
2nd change readme_git.txt-bare-client2
commit 4c7d5b59fcdcf6275b870501acdcd9feaed298b0
Author: c_scong
Date: Tue Feb 2 17:56:47 2021 +0800
1st change readme_git.txt-bare
commit 338ba981261ec1c28a1ea78ab78caeeee1a2a15e
Author: zhouhao0906
Date: Wed Jan 27 15:44:05 2021 +0800
add one file into server
参考:
https://www.liaoxuefeng.com/wiki/896043488029600 -> 廖雪峰git教程
https://www.cnblogs.com/hanrp/p/12531082.html -> 使用git在两台机器间同步代码