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

  • 一、GIT基礎   

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

(1)GIT生態node

GIT分佈式版本管理系統linux

Gitlab git私庫解決方案git

Github git公有庫解決方案github

 (2)Git安裝vim

Centos:bash

yum install -y gitcurl

Ubuntu:分佈式

apt-get install giturl

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
  • 二、分支管理

[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融合分支
相關文章
相關標籤/搜索