学习使用git

2018-12-09  本文已影响7人  任总

一、Git概述

1、什么是Git

2、Git的特点

3、Git的用途

源代码版本控制分类

4、Git的工作区

5、Git配置文件:git config

(1)配置文件对应的有效范围
Git仓库特有配置文件:REPO/.git/config
Git全局配置文件:~/.gitconfig, --global
系统Git配置文件:/etc/git/gitconfig, -system
(2)必须设定配置中的两项:

示例:添加全局配置的用户名

#查询配置信息
[root@node-65 testproj]# git config -l
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
#添加全局配置的用户名
[root@node-65 testproj]# git config --global user.name hehe
#添加全局配置的用户邮箱
[root@node-65 testproj]# git config --global user.email hehe@hehe.com
[root@node-65 testproj]# git config -l
user.name=hehe
user.email=hehe@hehe.com
.....

#用户家目录下将生成.gitconfig文件
[root@node-65 testproj]# cat ~/.gitconfig 
[user]
    name = hehe
    email = hehe@hehe.com

二、Git的对象类型

Git的对象类型: .git/objects是个隐藏目录

1、块(blob)对象:文件的每个版本表现为一个块(blob);

blob对象

2、树(tree)对象:一个目录代表一层目录信息;

tree对象

3、提交(commit)对象:用于保存版本库一次变化的元数据,包括作者、邮箱、提交日期、日志;每个提交对象都指定一个目录树对象;

提交对象

4、标签(tag)对象:用于给一个特定对象一个易读的名称;

标签对象
提交流程中的对象种类
提交流程工作原理

三、安装Git和命令使用

1、Git的安装

[root@node-65 ~]# yum install git -y
[root@node-65 ~]# mkdir testproj
[root@node-65 ~]# cd testproj/
[root@node-65 testproj]# git init
Initialized empty Git repository in /root/testproj/.git/
[root@node-65 testproj]# ls -a
.  ..  .git
[root@node-65 testproj]# tree .git
.git
├── branches
├── config
├── description
├── HEAD
├── hooks
│?? ├── applypatch-msg.sample
│?? ├── commit-msg.sample
│?? ├── post-update.sample
│?? ├── pre-applypatch.sample
│?? ├── pre-commit.sample
│?? ├── prepare-commit-msg.sample
│?? ├── pre-push.sample
│?? ├── pre-rebase.sample
│?? └── update.sample
├── info
│?? └── exclude
├── objects
│?? ├── info
│?? └── pack
└── refs
    ├── heads
    └── tags



2、Git中的文件分类和命令

Git中的文件分为三类:

(1)、git ls-files:列出文件;

-s:列出暂存区(stage area)中的文件对象

(2)、git cat-file:查看文件;

-p:美观排版显示文件内容;

(3)、git hash-object:计算文件的hash码;
(4)、git write-tree:根据当前索引中的内容创建树对象;

3、Git暂存区命令

(1)、git add:暂存文件;
(2)、git ls-files:

默认显示索引中的文件列表的原始文件名;
-s:显示暂存的文件信息:权限、对象名、暂存号及原始文件名;
-o:显示未被追踪的文件;

示例:
[root@node-65 testproj]# ls
passwd  README
#把所在目录,存储到暂存区中
[root@node-65 testproj]# git add ./
[root@node-65 testproj]# tree .git
.git
├── branches
├── config
├── description
├── HEAD
├── hooks
│   ├── applypatch-msg.sample
│   ├── commit-msg.sample
│   ├── post-update.sample
│   ├── pre-applypatch.sample
│   ├── pre-commit.sample
│   ├── prepare-commit-msg.sample
│   ├── pre-push.sample
│   ├── pre-rebase.sample
│   └── update.sample
├── index
├── info
│   └── exclude
├── objects
│   ├── 20
│   │   └── 83afb61272a2406ba0705201ba9a956655ad2e
│   ├── e2
│   │   └── ebf1c73ae535813955ab394d92d92a2f1fb330
│   ├── info
│   └── pack
└── refs
    ├── heads
    └── tags
[root@node-65 testproj]# vim README 

#把文件存储到暂存中
[root@node-65 testproj]# git add README 
[root@node-65 testproj]# tree .git
.....
├── objects
│   ├── 20
│   │   └── 83afb61272a2406ba0705201ba9a956655ad2e
│   ├── 7f
│   │   └── 40d19f465256c6fedec561945ca5abfc497635
│   ├── e2
│   │   └── ebf1c73ae535813955ab394d92d92a2f1fb330
.......

#查询当前git状态
[root@node-65 testproj]# git status
# On branch master  #当前在主分支上
#
# Initial commit         #未提交过
#
# Changes to be committed: #如果提交,以下文件将会改变。
#   (use "git rm --cached <file>..." to unstage)#如果不想提交,可以用git rm命令删除暂存区的文件
#
#   new file:   README   #新文件夹名称
#   new file:   passwd  #新文件夹名称
(3)git rm 删除命令

git rm:删除工作目录中的文件,及索引中的映射;
git rm --cached:只删除索引中的映射;

示例:
#删除暂存中的文件
[root@node-65 testproj]# git rm passwd --cached
rm 'passwd'

[root@node-65 testproj]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#   new file:   README
#
# Untracked files:     #此文件还未保存到暂存区中,如果保存请使用git add命令
#   (use "git add <file>..." to include in what will be committed)
#
#   passwd

(4)git mv 移动命令

git mv:改变工作目录中的文件名,及索引中的映射;

示例:
#重命名
[root@node-65 testproj]# git add README.txt 
[root@node-65 testproj]# git mv README.txt  README
[root@node-65 testproj]# git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   modified:   README
#

4、Git提交至版本库命令

提交的标识:

相对提交名示例:

^:C6^表示当前分支的前一个版本, C6^2表示第二分支的前一个版本
~:C6~表示当前分支的前一个版本, C6~2表示当前分支的前二个版本,
提交的相对引用表示方法
提交的相对范围引用表示方法
(1)、git commit :Git提交
(2)、git log:查看提交日志;
示例:向本地版本库提交
[root@node-65 testproj]# git commit
#会自动弹出一个编辑器,出现以下字段,提示完善信息

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#       new file:   README
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       passwd
Initial version  #添加
version: 0.1.0  #添加

#提交成功后
[master (root-commit) 07dd1af] Initial version version: 0.1.0
 1 file changed, 2 insertions(+)
 create mode 100644 README

#git目录中
[root@node-65 ~]# tree /root/.git
├── logs
│   ├── HEAD
│   └── refs
│       └── heads
│           └── master    
├── objects
│   ├── 07
│   │   └── dd1afa3900a99afb944288f14ddc15c581fa80
│   ├── 20
│   │   └── 83afb61272a2406ba0705201ba9a956655ad2e
│   ├── 7f
│   │   └── 40d19f465256c6fedec561945ca5abfc497635
│   ├── dd
│   │   └── 3ba7c74fc18129c135dcffda303f3e6b5dc454
│   ├── e2
│   │   └── ebf1c73ae535813955ab394d92d92a2f1fb330
│   ├── info
│   └── pack
└── refs
    ├── heads
    │   └── master
    └── tags

#查看提交日志
[root@node-65 testproj]# git log
commit 07dd1afa3900a99afb944288f14ddc15c581fa80
Author: hehe <hehe@hehe.com>
Date:   Tue Dec 4 08:46:47 2018 +0800

    Initial version
    version: 0.1.0

#添加提交标签
[root@node-65 testproj]# git log
commit 07dd1afa3900a99afb944288f14ddc15c581fa80
Author: hehe <hehe@hehe.com>
Date:   Tue Dec 4 08:46:47 2018 +0800

    Initial version
    version: 0.1.0
[root@node-65 testproj]# git tag v0.1 07dd1afa
[root@node-65 testproj]# git tag -l
v0.1

示例:基于teg标签检出提交到本地目录
[root@node-65 testproj]# git checkout v0.1
M   README
Note: checking out 'v0.1'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at 07dd1af... Initial version version: 0.1.0

#提交0.2版本
[root@node-65 testproj]# git commit -m"version 0.2.0"
[detached HEAD fe3c24d] version 0.2.0
 1 file changed, 1 insertion(+), 1 deletion(-)
[root@node-65 testproj]# git log
commit fe3c24dda78882c3be3b0222a6231e0c7f486554
Author: hehe <hehe@hehe.com>
Date:   Tue Dec 4 17:11:40 2018 +0800

    version 0.2.0

commit 07dd1afa3900a99afb944288f14ddc15c581fa80
Author: hehe <hehe@hehe.com>
Date:   Tue Dec 4 08:46:47 2018 +0800

    Initial version
    version: 0.1.0
[root@node-65 testproj]# git tag v0.2 fe3c24d
[root@node-65 testproj]# git tag -l
v0.1
v0.2

5、git diff:比较提交、索引及工作目录;

--color:颜色区别

#工作目录与最近提交比较
[root@node-65 testproj]# git diff HEAD 
diff --git a/README b/README
index b736af3..7515de6 100644
--- a/README
+++ b/README
@@ -1,2 +1,3 @@
 this is readme
 1234567890
+0000000000

#工作目录与暂存区比较
[root@node-65 testproj]# git diff  --color
diff --git a/README b/README
index b736af3..7515de6 100644
--- a/README
+++ b/README
@@ -1,2 +1,3 @@
 this is readme
 1234567890
+0000000000

#保存到暂存区,暂存区文件和最近提交比较
[root@node-65 testproj]# git add README 
[root@node-65 testproj]# git diff --cached
diff --git a/README b/README
index b736af3..7515de6 100644
--- a/README
+++ b/README
@@ -1,2 +1,3 @@
 this is readme
 1234567890
+0000000000

git diff比较提交

6、git reset:撤消此前的操作;

--soft:将HEAD引用指向给定的提交,但不影响索引和工作目录;
--mixed:将HEAD引用指向给定的提交,并将索引内容改变为指定提交的快照;但不改变工作目录;
--hard:将HEAD引用指向给定的提交、将索引内容改变为指定提交的快照,并改变工作目录中的内容反映指定提交的内容(谨慎使用);

[root@node-65 testproj]# git log
commit 7acab53fe1cb2c436a468ceb652a29c09a34232a
Author: hehe <hehe@hehe.com>
Date:   Tue Dec 4 17:26:53 2018 +0800

    version 0.1.0
..............

#撤销指向给定的提交
[root@node-65 testproj]# git reset --mixed 7acab53

四、Git分支

1、分支命名法则:

  • 可以使用/,但不能以/结尾;
  • 不能以-开头;
  • 以位于/后面的组件,不能以.开头;
  • 不能使用连续的...;
  • 不能使用空白字符;
  • 不能使用^, ~, ?, *,[等;

2、分支相关命令

(1)git branch:列出、创建及删除分支;

格式: git branch BRANCH_NAME [START_COMMIT]
git branch -d BRANCH_NAME

示例:
#查询分支
[root@node-65 testproj]# git branch --list
* (detached from v0.1)
  master

#创建develop分支
[root@node-65 testproj]# git branch develop
[root@node-65 testproj]# git branch --list
* (detached from v0.1)
  develop
  master

#切换到develop分支
[root@node-65 testproj]# git checkout develop
Switched to branch 'develop'
[root@node-65 testproj]# git branch --list
* develop     #*代表当前分支
  master

(2)git checkout 检出分支:

格式:git checkout <branch>:

示例:
#检出文件和分支
[root@node-65 testproj]# git checkout README
[root@node-65 testproj]# ls 
README

#查询暂存区文件
[root@node-65 testproj]# git ls-files -s
100644 7f40d19f465256c6fedec561945ca5abfc497635 0     README

#计算出项目的哈希值
[root@node-65 testproj]# git hash-object README 
7f40d19f465256c6fedec561945ca5abfc497635

#查看文件内容
[root@node-65 testproj]# git cat-file -p 7f40d1
this is readme

(3)分支的合并:
#切换到develop分支
[root@node-65 testproj]# git branch --list
* develop
  master
#创建文件
[root@node-65 testproj]# vim group
#保存到暂存
[root@node-65 testproj]# git add group 
#提交到对象库
[root@node-65 testproj]# git commit -m"version 0.1.0"
[develop 7acab53] version 0.1.0
 1 file changed, 1 insertion(+)
 create mode 100644 group

[root@node-65 testproj]# git log
commit 7acab53fe1cb2c436a468ceb652a29c09a34232a
Author: hehe <hehe@hehe.com>
Date:   Tue Dec 4 17:26:53 2018 +0800

    version 0.1.0

#设定提交标签
[root@node-65 testproj]# git tag v0.3 7acab53
[root@node-65 testproj]# git tag -l
l
v0.1
v0.2
v0.3

#合并提交
#查询主分支文件
[root@node-65 testproj]# git checkout master
Switched to branch 'master'
[root@node-65 testproj]# ls
README
#查询develop支文件
[root@node-65 testproj]# git checkout develop
Switched to branch 'develop'
[root@node-65 testproj]# ls
group  README

#将develop分支合并到master分支上
[root@node-65 testproj]# git merge develop
Updating 07dd1af..7acab53
Fast-forward
 README | 2 +-
 group  | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)
 create mode 100644 group

(4)无冲突合并:
示例:
$ git checkout master
$ git status
$ git merge BRANCH_NAME
$  git log --graph --pretty=oneline --abbrev-commit
(5)有冲突合并:

解决步骤:手动解决冲突-->解决完成之后-->git add保存到暂存空间-->git commit提交到版本库

示例:
#切换到开发分支
[root@node-65 testproj]# git checkout develop
#修改文件
[root@node-65 testproj]# vim README 
#保存到暂存区
[root@node-65 testproj]# git add README 
#提交文件到版本库
[root@node-65 testproj]# git commit -m "version 0.2.1"
[develop d8dc96c] version 0.2.1
 1 file changed, 1 insertion(+)
#切换回master分支
[root@node-65 testproj]# git checkout master
Switched to branch 'master'
#合并开发分支
[root@node-65 testproj]# git merge develop
Auto-merging README
CONFLICT (content): Merge conflict in README #未能合并文件
Automatic merge failed; fix conflicts and then commit the result.#自动合并失败;解决冲突,然后提交结果。

#手动对比
[root@node-65 testproj]# git diff
diff --cc README
index 7515de6,63466c3..0000000
--- a/README
+++ b/README
@@@ -1,3 -1,3 +1,7 @@@
  this is readme
  1234567890
++<<<<<<< HEAD
 +0000000000
++=======
+ 1111111111
++>>>>>>> develop
#手动修改冲突文件
[root@node-65 testproj]# vim README 
this is readme
1234567890
<<<<<<< HEAD
0000000000
=======
1111111111
>>>>>>> develop

(6)合并的变基操作:

命令格式:git rebase

$ git checkout dev
$ git rebase master
$ git checkout master
$ git merge -m "MSG"
合并的变基操作

五、Git:分布式版本控制系统

Git分布式版本控制系统基于网络协议:http, https, ssh, git来实现


多客户端远程库协作流程

1、Git服务使用的协议

协议:本地协议(local)、HTTP/HTTPS协议、SSH协议、Git协议;

URL格式
例如:/path/to/repo.git
file:///path/to/repo.git

URL格式:
例如:git://host/path/to/repo.git
git://host/~user/path/to/repo.git

样式一:URL
ssh://[USER@]host[:port]/path/to/repo.git
ssh://[USER@]host[:port]/~USERNAME/path/to/repo.git

样式二:URL2
[USER@]hostpath/to/repo.git

第一种:1.6.5-:哑http协议
第二种:1.6.6+:智能http协议,普遍使用
URL格式
http://host/path/to/repo.git

2、引用远程版本库

本地创建与GitHub关联的版本库,用于提交和推送版本。
+source:destination
refs/heads/NAME:本地分支
refs/remotes/NAME:远程跟踪分支

[remote "publish"]
url = http://HOST/pub/repo_name.git
push = +refs/heads/*:refs/remotes/origin/*

remote.publish.url
remote.publish.push

3、引用远程库相关命令

格式:git pull <远程主机名> <远程分支名>:<本地分支名>

格式:git push <远程主机名> <本地分支名>:<远程分支名>

示例:
#创建本地库库
[root@node-65 ~]# git init --bare myproject.git
Initialized empty Git repository in /root/myproject.git/
[root@node-65 ~]# ls
anaconda-ks.cfg  myproject.git  testproj
[root@node-65 ~]# tree myproject.git/
myproject.git/
├── branches
├── config
├── description
├── HEAD
├── hooks
│   ├── applypatch-msg.sample
│   ├── commit-msg.sample
│   ├── post-update.sample
│   ├── pre-applypatch.sample
│   ├── pre-commit.sample
│   ├── prepare-commit-msg.sample
│   ├── pre-push.sample
│   ├── pre-rebase.sample
│   └── update.sample
├── info
│   └── exclude
├── objects
│   ├── info
│   └── pack
└── refs
    ├── heads
    └── tags

#把远程仓库和本地仓库建立关联关系
[root@node-65 ~]# git remote add myproject https://githup.com/hehe/myproject.git

#下载远程库内容并合并
[root@node-65 ~]#git pull myproject master
version 0.4.1 merged remote myproject

#上传本地仓库内容到远程仓库
[root@node-65 ~]# git push myproject master 
输入用户名
输入密码

#当本地仓库丢失时,可使用clone命令从远程服务器下载
[root@node-65 ~]# git clone https://github.com/xxxx/myproject.git

4、远程库的ssh访问方法

[root@node-65 .ssh]# ssh-keygen -t rsa -b 4096
[root@node-65 ~]# cd .ssh/
[root@node-65 .ssh]# cat id_rsa.pub
.....<秘钥>.....
然后,复制<秘钥>到github上生成ssh
#使用clone命令ssh方式从远程服务器下载
[root@node-65 ~]# git clone git@github.com:xxxx/myproject.git

#ssh方式上传本地仓库内容到远程仓库
[root@node-65 ~]# git push origin master 

5、git的http服务器:

仓库默认所在目录:
/var/www/git

示例:创建git的http服务器
root@node-65 ~]# yum install httpd -y
[root@node-65 ~]# yum install gitweb -y

[root@node-65 ~]# cd /var/www/git/

#建立并初始化puppetmodules库
[root@node-65 git]# git init --bare puppetmodules.git

#修改属主属组
[root@node-65 git]# chown -R apache.apache puppetmodules.git/

Initialized empty Git repository in /var/www/git/puppetmodules.git/
[root@node-65 git]# ls
gitweb.cgi  puppetmodules.git  static
[root@node-65 git]# cd /etc/httpd/conf.d
[root@node-65 conf.d]# mv git.conf git.conf.bak

#创建一个http虚拟机
[root@node-65 conf.d]# vim gitrepo.conf
<VirtualHost *:80>
      ServerName node-65
      SetEnv GIT_PROJECT_ROOT /var/www/git
      SetEnv GIT_HTTP_EXPORT_ALL
      ScriptAlias /git/ /usr/libexec/git-core/git-http-backend/
      <Directory "/usr/libexec/git-core/">
            Options ExecCGI Indexes
            Require all granted
      </Directory>
      <location /git>
            AuthType Basic
            AuthName "Private Git Repo"
            AuthUserFile /etc/httpd/conf.d/.gitpasswd
             Require valid-user
       </location>

 Alias /git/repo  /var/www/git

<Directory /var/www/git>
  Options +ExecCGI
  AddHandler cgi-script .cgi
  DirectoryIndex gitweb.cgi
</Directory>

</VirtualHost>


#新建立两git用户
[root@node-65 conf.d]# htpasswd -c -m ./.gitpasswd tom
New password: 
Re-type new password: 
Adding password for user tom
[root@node-65 conf.d]# htpasswd -c -m ./.gitpasswd jerry
New password: 
Re-type new password: 
Adding password for user jerry

[root@node-65 ~]# systemctl start httpd.service

客户机端

[root@node-64 ~]# yum install git -y

#克隆自建远程库
[root@node-64 ~]# git clone http://192.168.1.65/git/puppetmodules.git

[root@node-64 ~]# cd puppetmodules/
#建立puppet目录
[root@node-64 puppetmodules]# mkdir nginx/{files,manifests,templates,lib,spec,tests} -pv


[root@node-64 puppetmodules]# vim nginx/manifests/init.pp
class nginx {
         package{'nginx':
             ensure => latest,
          }
         service {'nginx':
               ensure => running,
               enable => true,
           }
}

#添加到暂存区
[root@node-64 puppetmodules]# git add .



[root@node-64 puppetmodules]# git config --global user.name tom
[root@node-64 puppetmodules]# git config --global user.email tom@hehe.com

##提交
[root@node-64 puppetmodules]# git commit -m "nginx module ok"
[master (root-commit) fe12d61] nginx module ok
 1 file changed, 9 insertions(+)
 create mode 100644 nginx/manifests/init.pp

[root@node-64 puppetmodules]# git commit -m "nginx module ok"

#推送到主机
[root@node-64 puppetmodules]# git push origin master

六、自建Git仓库--开源项目GitLab

官方网站:https://about.gitlab.com/
安装文件下载地址:https://packages.gitlab.com/gitlab

#下载GitLab
[root@node-65 ~]# wget --content-disposition https://packages.gitlab.com/gitlab/gitlab-ce/packages/el/7/gitlab-ce-10.0.0-ce.0.el7.x86_64.rpm/download.rpm

#安装GitLab
[root@node-65 ~]# yum install gitlab-ce-10.0.0-ce.0.el7.x86_64.rpm -y

#执行自动配置脚本
[root@node-65 ~]# gitlab-ctl reconfigure
........
Running handlers:
Running handlers complete

#启动
[root@node-65 bin]#  cd /opt/gitlab/bin
[root@node-65 bin]#  gitlab-ctl start
ok: run: gitaly: (pid 6907) 92s
ok: run: gitlab-monitor: (pid 6942) 90s
ok: run: gitlab-workhorse: (pid 6922) 91s
ok: run: logrotate: (pid 6645) 178s
ok: run: nginx: (pid 6628) 184s
ok: run: node-exporter: (pid 6683) 171s
ok: run: postgres-exporter: (pid 6998) 89s
ok: run: postgresql: (pid 6381) 250s
ok: run: prometheus: (pid 6986) 89s
ok: run: redis: (pid 6321) 256s
ok: run: redis-exporter: (pid 6712) 159s
ok: run: sidekiq: (pid 6538) 198s
ok: run: unicorn: (pid 6500) 204s

输入自定义密码
注册用户
登录成功你可以创建组和项目
上一篇 下一篇

猜你喜欢

热点阅读