Gitlab+Jenkins學習之路(一)之Git基礎


  • 1、GIT基礎   

GIT是一個分布式版本管理系統,速度快,適合大規模,跨地區多人協同開。SVN是一個集中式版本管理系統。

(1)GIT生態

GIT分布式版本管理系統

Gitlab git私庫解決方案

Github git公有庫解決方案

 (2)Git安裝

Centos:

yum install -y git

Ubuntu:

apt-get install git

Windows安裝git bash

Linux編譯安裝

注意不要使用git 1.8以下版本,推薦使用2.7版本

①編譯安裝git
[root@linux-node1 ~]# yum install -y epel-release 安裝依賴包: [root@linux-node1 ~]# yum install -y curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-ExtUtils-MakeMaker [root@linux-node1 ~]# wget https://github.com/git/git/archive/v2.7.4.zip [root@linux-node1 ~]# yum install -y unzip [root@linux-node1 ~]# unzip git-v2.7.4.zip [root@linux-node1 ~]# cd git-2.7.4 [root@linux-node1 git-2.7.4]# make prefix=/usr/local/git all [root@linux-node1 git-2.7.4]# make prefix=/usr/local/git install [root@linux-node1 git-2.7.4]# rm -rf /usr/bin/git [root@linux-node1 git-2.7.4]# ln -s /usr/local/git/bin/git /usr/bin/git [root@linux-node1 git-2.7.4]# git --version git version 2.7.4 ②初始化倉庫 [root@linux-node1 ~]# mkdir test [root@linux-node1 ~]# cd test [root@linux-node1 test]# git init #將test目錄初始化倉庫 [root@linux-node1 test]# git config --global user.name"*****" [root@linux-node1 test]# git config --global user.email *******@qq.com 四個區域: 遠程倉庫<-->本地倉庫<-->暫存區域<-->工作目錄 四種狀態 Untracked、Unmodified、Modified、Staged Untracked(工作目錄)-->git add -->Staged(暫存區)-->git commit版本-->Unmodified(本地倉庫)-->Edit file-->Modified-->Stage the file-->Staged ③常用命令: git add 加入暫存 git status 查看狀態 git status -s 狀態概覽 git diff 尚未暫存的文件 git diff --staged 暫存區文件 git commit 提交更新 git reset 回滾 git rm 從版本庫中移除 git rm --cached README 從暫存區中移除 git mv 相當於mv git rm git add 三個命令 使用演示: [root@linux-node1 test]# touch index.html ==>創建文件 [root@linux-node1 test]# vim index.html [root@linux-node1 test]# git status ==>此時文件處於工作目錄中 位於分支 master 初始提交 未跟蹤的文件: (使用 "git add <文件>..." 以包含要提交的內容) index.html 提交為空,但是存在尚未跟蹤的文件(使用 "git add" 建立跟蹤) [root@linux-node1 test]# git add index.html ==>加入代碼庫 [root@linux-node1 test]# git status ==>此時文件處於暫存區 位於分支 master 初始提交 要提交的變更: (使用 "git rm --cached <文件>..." 以取消暫存) 新文件: index.html [root@linux-node1 test]# git commit -m "first commit" ==>提交到本地倉庫 [master(根提交) c6bc04f] first commit 1 file changed, 3 insertions(+) create mode 100644 index.html [root@linux-node1 test]# git status 位於分支 master 無文件要提交,干凈的工作區 [root@linux-node1 test]# git log commit c6bc04f90d4ef442e2c4d5bc788b21de239332da ==>回滾需要的id Author: ****** <******@qq.com> Date: Fri Dec 8 22:37:40 2017 +0800 first commit [root@linux-node1 test]# touch pay.html [root@linux-node1 test]# vim pay.html [root@linux-node1 test]# git add pay.html [root@linux-node1 test]# git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: pay.html [root@linux-node1 test]# touch news.html [root@linux-node1 test]# echo "123" > news.html [root@linux-node1 test]# git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: pay.html Untracked files: (use "git add <file>..." to include in what will be committed) news.html [root@linux-node1 test]# git add news.html [root@linux-node1 test]# git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: news.html new file: pay.html [root@linux-node1 test]# git rm --cached pay.html #將pay.html從暫存區移除 rm 'pay.html' [root@linux-node1 test]# git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: news.html Untracked files: (use "git add <file>..." to include in what will be committed) pay.html [root@linux-node1 test]# git commit -m "news" [master d83603a] news 1 file changed, 1 insertion(+) create mode 100644 news.html [root@linux-node1 test]# git status On branch master Untracked files: (use "git add <file>..." to include in what will be committed) pay.html nothing added to commit but untracked files present (use "git add" to track) [root@linux-node1 test]# git log commit d83603a56b8926630d31b46898e4b6d69293d946 Author:******** Date: Fri Dec 8 22:48:37 2017 +0800 news commit c6bc04f90d4ef442e2c4d5bc788b21de239332da Author: ***************** Date: Fri Dec 8 22:37:40 2017 +0800 first commit [root@linux-node1 test]# echo "66666" >> pay.html [root@linux-node1 test]# git status On branch master Untracked files: (use "git add <file>..." to include in what will be committed) pay.html nothing added to commit but untracked files present (use "git add" to track) [root@linux-node1 test]# git add pay.html [root@linux-node1 test]# git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: pay.html [root@linux-node1 test]# git commit -m "pay modelue" [master e55a302] pay modelue 1 file changed, 2 insertions(+) create mode 100644 pay.html [root@linux-node1 test]# git status On branch master nothing to commit, working directory clean [root@linux-node1 test]# git log commit e55a302e11d967fd25eac1cce8b6c7bed732019b Author:****************** Date: Fri Dec 8 22:49:57 2017 +0800 pay modelue commit d83603a56b8926630d31b46898e4b6d69293d946 Author: ****************** Date: Fri Dec 8 22:48:37 2017 +0800 news commit c6bc04f90d4ef442e2c4d5bc788b21de239332da Author: ****************** Date: Fri Dec 8 22:37:40 2017 +0800 first commit
  • 2、分支管理

[root@linux-node1 test]# git status
On branch master
nothing to commit, working directory clean

建分支,開發新功能是不能在master分支上開發
[root@linux-node1 test]# git branch about    #創建分支
[root@linux-node1 test]# git status
On branch master
nothing to commit, working directory clean
[root@linux-node1 test]# git checkout about     #切換分支
Switched to branch 'about'
[root@linux-node1 test]# git status
On branch about
nothing to commit, working directory clean
[root@linux-node1 test]# git log    #創建的分支是在master分支當前的狀態進行創建的。所以在about分支上也會有master的記錄
commit e55a302e11d967fd25eac1cce8b6c7bed732019b
Author: ************
Date:   Fri Dec 8 22:49:57 2017 +0800

    pay modelue

commit d83603a56b8926630d31b46898e4b6d69293d946
Author: ************
Date:   Fri Dec 8 22:48:37 2017 +0800

    news

commit c6bc04f90d4ef442e2c4d5bc788b21de239332da
Author: ************
Date:   Fri Dec 8 22:37:40 2017 +0800

    first commit
    
[root@linux-node1 test]# git branch
* about
  master
[root@linux-node1 test]# touch about.html
[root@linux-node1 test]# echo "about us" >> about.html
[root@linux-node1 test]# git add .
[root@linux-node1 test]# git commit -m "about"
[about 08b200a] about
 1 file changed, 1 insertion(+)
 create mode 100644 about.html
[root@linux-node1 test]# git log
[root@linux-node1 test]# git checkout master    #切換到master分支
Switched to branch 'master'
[root@linux-node1 test]# git log
[root@linux-node1 test]# git checkout about    #切換到about分支
[root@linux-node1 test]# echo "about2" > about2.html
[root@linux-node1 test]# git add .
[root@linux-node1 test]# git commit -m "about2"
[root@linux-node1 test]# git log
[root@linux-node1 test]# git checkout master
[root@linux-node1 test]# git log
[root@linux-node1 test]# git merged about    #在master分支上合並about分支
[root@linux-node1 test]# git log
[root@linux-node1 test]# git branch test    #創建test分支
[root@linux-node1 test]# git checkout test
[root@linux-node1 test]# touch "test" > test.html
[root@linux-node1 test]# git add .
[root@linux-node1 test]# git commit -m "test"
[root@linux-node1 test]# git checkout master
[root@linux-node1 test]# git branch --merged    #查看已經合並的分支
[root@linux-node1 test]# git branch --no-merged    #查看未合並的分支
 
分支命令
git branch例出分支
git branch -v
git branch --merged查看哪些分支被合並
git branch --no-merged查看哪些分支未被合並
git branch -d testling刪除分支
git checkout切換分支
git merged融合分支

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM