【Linux】【Services】【VersionControl】Git基础概念及使用

1. 简介

1.1. 版本控制工具:

本地版本控制系统:

集中化版本控制系统:CVS,SVN

分布式版本控制系统: BitKeeper,Git

1.2. 官方网站:

https://git-scm.com

1.3. Git基本结构

工作区:Workding Directory

暂存库:Staging Area

版本库: Repository

1.4. Git仓库包含索引(暂存)和对象库(版本库)

1.5. 相关文档

免费的ebook: https://git-scm.com/book/zh/v2

1.6.Git的对象类型

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

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

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

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

1.7. Git的文件分类

已追踪的(tracked):已经在版本库中,或者已经使用git add命令添加至索引中的文件;

被忽略的(Ignored):在版本库中通过“忽略文件列表”明确声明为被忽略的文件;

未追踪的(untracked):上述两类之外的其他文件;

 

2. Git简单使用

2.1. git init

~]cd /root
~]mkdir myproject
~]cd myproject
~]git init
~]# 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

9 directories, 13 files

 

2.2. git config

##添加全局配置参数
git config --global user.sex male
git config --global user.name eric

##添加本地配置参数
git config --add user.mail eric@123

##这个是全局参数,在用户家目录下
~]# pwd
/root
~]# ls -al .gitconfig
-rw-r--r-- 1 root root 20 Aug  7 04:13 .gitconfig
~]# cat .gitconfig
[user]
        name = eric
        sex = male
~]# git config -l --global
user.name=eric
user.sex=male

##这个是本地参数,在项目目录下
~]# pwd
/root/myproject/.git
~]# ls -al config
-rw-r--r-- 1 root root 116 Aug  7 04:57 config
~]# git config -l --local
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
user.mail=eric@123
~]# cat config
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[user]
        mail = eric@123

#还有一个系统级的在/etc/gitconfig
~]# git config -l --system

 

2.3. git add

git add . #暂存当前目录所有
git add DIR #暂存指定DIR

 

2.4. git commit

提交的标识:

  引用:ID,reference,SHA1,绝对提交名

  符号引用:symbolic reference,symref;

    本地特性分支名称、远程跟踪分支名称、标签名;

    名称:

      refs/heads/REF:本地特性分支名称

      refs/remotes/REF:远程跟踪分支名称

      refs/tags/REF:标签名

 

git会自动维护几个特定目的的特殊符号引用:

  HEAD:始终指向当前分支的最近提交;或检出到其他分支时,目标分支的最近提交;

  ORIG_HEAD:合并操作时,新生成的提交之前的提交保存于此引用中;

  FETCHED_HEAD:

  MERGE_HEAD:合并操作时,其他分支的上一次提交;

#add不add都可以commit,但是commit之前要指定email和user
~]# git commit -m "v0.1"

*** Please tell me who you are.

Run

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

#再次提交
~]# git commit -m "v0.1"
[master (root-commit) 3c56c9b] v0.1
 1 file changed, 1 insertion(+)
 create mode 100644 readme.md

 

2.5. git clone

#可以clone本地或者远程的项目
~]# git clone myproject/ Newmyproject
Cloning into 'Newmyproject'...
done.

 

2.6. git status

# 已经提交或者刚创建
~]# git status
# On branch master
nothing to commit, working directory clean

# 修改一下
~]# echo "installation" >> install.sh
~]# git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       install.sh
nothing added to commit but untracked files present (use "git add" to track)

# 保存一下
~]# git add install.sh
~]# git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       new file:   install.sh
#

#提交一下
~]# git commit -m "v0.2"
[master 19cb183] v0.2
 1 file changed, 1 insertion(+)
 create mode 100644 install.sh
~]# git status
# On branch master
nothing to commit, working directory clean

  

2.7. 文件相关

git ls-files:默认显示索引中的文件列表的原始文件名;

      -s:显示暂存的文件信息,;权限、对象名、暂存号及原始文件名;

      -o:显示未被追踪的文件

      --unmerged:只查看未合并的文件

git cat-file

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

  --cached:只删除索引中的映射

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


~]# git ls-files -s 100644 3100e23cd38ef12583bfc792b5c62aea7e9dbaca 0 install.sh 100644 3b18e512dba79e4c8300dd08aeb37f8e728b8dad 0 readme.md ~]# git cat-file -p 3100e23cd38ef12583bfc792b5c62aea7e9dbaca installation

  

2.8. git log

git log --graph --pretty=oneline --abbrev-commit

~]# git log
commit 19cb183c7270e8fe4015f11e7d712372ba40c3b4
Author: eric <eric@1234.com>
Date:   Tue Aug 7 07:38:17 2018 +0200

    v0.2

commit 3c56c9bd0bf1600982e0af01540e96207276f136
Author: eric <eric@1234.com>
Date:   Tue Aug 7 05:11:50 2018 +0200

    v0.1

  

2.9. git diff

~]# git diff --color 3c56c9bd0bf1600982e0af01540e96207276f136 19cb183c7270e8fe4015f11e7d712372ba40c3b4
diff --git a/install.sh b/install.sh
new file mode 100644
index 0000000..3100e23
--- /dev/null #老文件
+++ b/install.sh #新文件
@@ -0,0 +1 @@
+installation

  

 2.10. git reset:撤销此前的操作;

  --soft:将HEAD引用指向给定的提交,但不影响索引和工作目录;

  --mixed:将HEAD引用指向指定的提交,并将索引内容改变为指定提交的快照;但不改变工作目录;

  --hard:将HEAD引用指向给定的提交、将索引内容改变为指定提交的快照,并改变工作目录中的内容反映指定提交的内容

 

2.11. git branch:列出、创建及删除分支

  git branch BRANCH_NAME [START_COMMIT]

  git branch -d BRANCH_NAME

 

2.12. git show-branch:查看分支及其相关的提交

 

2.13. git checkout:在分支间切换

    git checkout <branch>:检出分支

 

2.14. git merge BRANCK_NAME #需要checkout到master

 

3. git服务器

3.1. 支持的协议:本地协议(local)、HTTP/HTTPS协议、SSH协议、Git协议

3.2. 本地协议:

git clone file:///path/to/repo.git /path/to/local/server

git clone /path/to/repo.git /path/to/local/server

  

3.3. Git协议:由git-deamon程序提供,监听在tcp的9418端口;仅支持“读”操作,无任何认证功能;

git://host/path/to/repo.git

git://host/~user/path/to/repo.git

  

3.4. SSH协议:

ssh://[USER@]host[:port]/path/to/repo.git

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

[USER@]host/path/to/repo.git

  

3.5. HTTP/HTTPS协议:在1.6.5之前的版本为哑http协议,只能读不能写,不能认证,在1.6.6之后的版本为智能http协议,可读可写可认证

http://host/path/to/repo.git #这是一个映射的地址,并不是服务器真实的路径

  

3.6.引用远程版本库:

远程版本库是定义在配置文件中的一个实体,在配置文件中体现为[remote "NAME"],它由两部分组成,URL+refspec(定义一个版本库与其他版本库的名称空间的映射关系)

+source:destination
    refs/heads/NAME:本地分支
    refs/remotes/NMAE:远程跟踪分支

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

remote.publish.url
remote.publish.push

  

3.7. git remote命令:管理远程仓库

 

 

4. git服务器

4.1. 安装必要的包

yum install -y git-daemon httpd

  

4.2. 检查httpd安装

# 修改http配置文件
~]# sed  "s/\<ServerName/ServerName YOURSERVERIP:80/g" /etc/httpd/conf/httpd.conf

# alias,cgi,env这三个模块必须要有
~]# httpd -M |grep -Ei "\<(alias|cgi|env)"
 alias_module (shared)
 env_module (shared)
 cgi_module (shared)

~]# systemctl start httpd

  

4.3. 检查git-deamon安装

~]# cat /usr/lib/systemd/system/git@.service
[Unit]
Description=Git Repositories Server Daemon
Documentation=man:git-daemon(1)

[Service]
User=nobody
ExecStart=-/usr/libexec/git-core/git-daemon --base-path=/var/lib/git --export-all --user-path=public_git --syslog --inetd --verbose
StandardInput=socket

~]# systemctl start git.socket

  

 

posted @ 2018-08-07 10:01  炼狱腾蛇  阅读(177)  评论(0编辑  收藏  举报