GIT&GITHUB

Git&GitHub

版本控制

我的版本控制

在进行开发的时候,程序须要常常性的修改,那么修改的时候就存在备份问题,当代码量较大修改频繁时,备份的及时性和备份的困难度就增长了。python

团队版本控制

当团队中多人协同开发同一个功能时,可能存在开发人员之间的版本互相覆盖问题。linux

版本控制特色

  1. 协同修改git

    对人能够并行地修改同一个文件。github

  2. 数据备份算法

    不只保存数据当前地实际状态,还能保存文件提交地历史状态,能够回滚操做。centos

  3. 版本管理缓存

    保存版本时不保存重复数据,节约存储,提升运行效率。bash

  4. 权限控制服务器

    对团队中参与开发的人员进行权限控制,对其余开发人员贡献的代码进行审核。ssh

  5. 历史记录

    查看提交修改历史,版本回滚

  6. 分支管理
    容许开发团队在工做过程当中多条生产线同时推动任务,提升效率。

工具简介

集中式:SVN、CVS、VSS。以服务器为核心,容易出现单点故障问题。

img

分布式:GIT、Mercurial、Bazaar、Darcs,避免了单点故障。

img

GIT

Git

优点

  1. 版本控制可在本地完成
  2. 完整性保证
  3. 添加大于删除和修改
  4. 分支操做快捷流畅
  5. bash融合

结构

img

img

  • Directory:使用Git管理的一个目录,也就是一个仓库,包含咱们的工做空间和Git的管理空间。
  • WorkSpace:须要经过Git进行版本控制的目录和文件,这些目录和文件组成了工做空间,除了.git以外的都属于工做区。
  • .git:存放Git管理信息的目录,初始化仓库的时候自动建立。
  • Index/Stage:暂存区,或者叫待提交更新区,在提交进入repo以前,咱们能够把全部的更新放在暂存区。
  • Local Repo:本地仓库,一个存放在本地的版本库;HEAD会只是当前的开发分支(branch)。
  • Stash:是一个工做状态保存栈,用于保存/恢复WorkSpace中的临时状态。

本地操做

初始化本地库

git init                    //在目录中将建立一个.git目录

设置签名

设置一个用户信息,用于惟一标示身份,仅用于区分开发人员,信息和远程仓库无关。

签名分为项目级别和系统级别,签名的做用域不一样,项目/仓库级别在本仓库中有效,优先使用仓库级别。签名必须设置才能执行操做。

git config user.name xxx
git config user.email xxx@xxx.com

git config --global user.name xxx
git config user.email xxx@xxx.com
$ cat ~/.gitconfig
[user]
        email = 1958862281@qq.com
        name = Wan9huan
        
//全局签名存储在全局配置文件中
//仓库签名保存在.git/config中

经常使用命令

git status                          #查看状态
git add                             #添加文件到缓存区
git remove --cached                 #移除已经add的文件
git commit                          #提交
git commit -a -m"This is Message"   #提交附带注释
git log                             #查看日志
git log --oneline                   #简洁查看
git relog                           #查看日志和回滚步数
git reset --hard HEAD^              #后退一步
git reset --hard HEAD~3             #后退三步
git reset --hard 2fedc85            #回到指定版本
------------------------------------------------------------------------
$ git status

On branch master

No commits yet

nothing to commit (create/copy files and use "git add" to track)

------------------------------------------------------------------------
$ git status

On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        ReactDemo/
        2017-09-07.png

nothing added to commit but untracked files present (use "git add" to track)
------------------------------------------------------------------------
$ git add 2017-09-07.png
$ git status

On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)      // 从缓存区移除
            
        new file:   2017-09-07.png

Untracked files:
  (use "git add <file>..." to include in what will be committed)
------------------------------------------------------------------------
$ git commit

[master (root-commit) c40dd30]  Changes to be committed: new file:   2017-09-07.png
                                            //版本号
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 2017-09-07.png
 -----------------------------------------------------------------------
 $ git status
 
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean
------------------------------------------------------------------------
$ git log --oneline

8b6ba42 (HEAD -> master) Create A Book Text
37c34e8         deleted:    Photoshop_Set-Up.exe
4cfa483 (origin/master) last
a2858e8 new
4b3e03e aa
dc03544 Merge branch 'master' of https://github.com/wan9huan/myfiles
9b3d010 all
9382437 Delete background.png
bb31bd9         Changes to be committed:        new file:   icon 2.png
b386c61 a
4366632 a
fc86b6a png for background
------------------------------------------------------------------------
$ git reflog

8b6ba42 (HEAD -> master) HEAD@{0}: commit: Create A Book Text
37c34e8 HEAD@{1}: commit: deleted: Photoshop_Set-Up.exe
4cfa483 (origin/master) HEAD@{2}: commit: last
a2858e8 HEAD@{3}: commit: new
4b3e03e HEAD@{4}: commit: aa
dc03544 HEAD@{5}: pull: Merge made by the 'recursive' strategy.
9b3d010 HEAD@{6}: commit: all
bb31bd9 HEAD@{7}: commit: Changes to be committed:
b386c61 HEAD@{8}: commit: a
4366632 HEAD@{9}: commit: a
fc86b6a HEAD@{10}: commit (initial): png for background
------------------------------------------------------------------------
$ git reflog book.txt
5997266 (HEAD -> master) HEAD@{0}: commit: 第四次修改
2fedc85 HEAD@{1}: commit: 第三次修改
af72196 HEAD@{2}: commit: add a string
8b6ba42 HEAD@{3}: commit: Create A Book Text
------------------------------------------------------------------------
//回滚、回到指定版本
$ git reflog book.txt
5997266 (HEAD -> master) HEAD@{0}: commit: 第四次修改
2fedc85 HEAD@{1}: commit: 第三次修改
af72196 HEAD@{2}: commit: add a string
8b6ba42 HEAD@{3}: commit: Create A Book Text
$ cat book.txt
This is a Book Text
333333333333333
444444444444444

$ git reset --hard af72196
HEAD is now at af72196 add a string
$ cat book.txt
This is a Book Text

$ git reset --hard 2fedc85
HEAD is now at 2fedc85 第三次修改
$ cat book.txt
This is a Book Text
333333333333333
------------------------------------------------------------------------
$ git reset --hard HEAD^    后退一步
$ git reset --hard HEAD~3 后退三步

Rest指令

git rest --soft             #移动本地库指针
git rest --mixed            #移动本地库、重置暂存区
git rest --hard             #移动本地库、重置暂存区、重置工做区

比较文件差别

git diff                            #暂存区比较
git diff Head                       #比较某一版本
------------------------------------------------------------------------
$ git diff book.txt

diff --git a/book.txt b/book.txt
index 48d8508..f2259a8 100644
--- a/book.txt
+++ b/book.txt
@@ -1,2 +1,4 @@
 This is a Book Text
-333333333333333
\ No newline at end of file
+333333333333333
+444444444444444
+555555555555555
\ No newline at end of file

分支

  1. 并行推动多个开发
  2. 分支彼此独立
  3. 热修复
git status                                  #查看当前分支
git branch -v                               #查看全部分支
git branch xxx                              #建立分支
git checkout xxx                            #切换分支
------------------------------------------------------------------------
$ git status -v

On branch master                //主分支
Your branch is ahead of 'origin/master' by 5 commits.
  (use "git push" to publish your local commits)
nothing to commit, working tree clean

$ git branch -v
* master 828b1ab [ahead 5] commit all

$ git branch hot_fix

$ git checkout hot_fix
Switched to branch 'hot_fix'
------------------------------------------------------------------------
#分支合并
1.切换到主分支
2.执行 git merge xxxx
--------------------------------------------------------------------------------
git checkout xxxx
git merge xxxx
------------------------------------------------------------------------

冲突解决

  1. 发生文件冲突,进入冲突解决状态
  2. 打开文件能够查看到冲突内容
  3. 删除冲突标记并修改文件
  4. 提交完成合并分支

远程操做

基本操做

git remote -v                                           #查看远程仓库
git remote add(remove) xxx ht..com/xxx.git              #添加远程仓库
git push xxx master(填写推送分支)                         #推送到分支
git clone https://git....../xxx/git                     #克隆远程库
git fetch xxx master                                    #下载远程内容
git checkout xxx/master                                 #切换到某个远程分支
git checkout master                                     #切换到本地分支
git merge xxx/maser                                     #合并本地和远程
git pull                                                #直接合并远程到本地
------------------------------------------------------------------------
$ git remote -v

origin  https://github.com/wan9huan/myfiles.git (fetch)
origin  https://github.com/wan9huan/myfiles.git (push)

$ git push origin master

Enumerating objects: 15, done.
Counting objects: 100% (15/15), done.
Delta compression using up to 4 threads.
Compressing objects: 100% (11/11), done.
Writing objects: 100% (14/14), 1.22 KiB | 138.00 KiB/s, done.
Total 14 (delta 5), reused 0 (delta 0)
remote: Resolving deltas: 100% (5/5), completed with 1 local object.
To https://github.com/wan9huan/myfiles.git
   4cfa483..828b1ab  master -> master
------------------------------------------------------------------------
#必须是远程仓库的成员才能够进行推送

冲突解决

#当不是基于最新版作出的修改没法进行推送
#拉去远程资源进入冲突解决状态,解决冲突
#提交再推送便可成功完成推送操做

跨团队协做

  1. Fork一个远程仓库
  2. 进行修改开发
  3. 发起Pull Request
  4. 原仓库全部人审核
  5. 审核完毕,合并代码

SSH

ssh-keygen -t rsa -C|github帐号
# 生成 .ssh/ 目录 生成 id_rsa id_rsa.pub
# 将公钥添加到远程端

git remote add xxxx ssh版本的.git
git push xxxx master
#使用 ssh push

Linux搭建

依赖安装

yum -y install policycoreutils openssh-server openssh-clients postfix policycoreutils-python

#启动项
systemctl enable postfix && systemctl start postfix

下载安装包

https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum  #选择一个安装包

wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-11.0.6-ce.0.el7.x86_64.rpm
#选择本身的安装包 e16 centos6 e17 centos7
rpm -i gitlab-ce-11.0.6-ce.0.el7.x86_64.rpm


       *.                  *.
      ***                 ***
     *****               *****
    .******             *******
    ********            ********
   ,,,,,,,,,***********,,,,,,,,,
  ,,,,,,,,,,,*********,,,,,,,,,,,
  .,,,,,,,,,,,*******,,,,,,,,,,,,
      ,,,,,,,,,*****,,,,,,,,,.
         ,,,,,,,****,,,,,,
            .,,,***,,,,
                ,*,.



     _______ __  __          __
    / ____(_) /_/ /   ____ _/ /_
   / / __/ / __/ /   / __ `/ __ \
  / /_/ / / /_/ /___/ /_/ / /_/ /
  \____/_/\__/_____/\__,_/_.___/


Thank you for installing GitLab!

gitlab-ctl reconfigure
//须要等待较长时间

gitlab-clt restart

CentOS 参考方案

原理

Hash算法

哈希加密算法,无论运算前的数据有多大,进行哈希运算后,能获得固定长度的加密结果,一样的数据通过相同的算法,都能输出一样的结果,不一样的数据获得同一个哈希值的可能性极小,不可逆。

版本控制机制

数据保存为小型文件系统的一组快照,建立分支时,新建一个指向当前版本的指针。

相关文章
相关标签/搜索