天天看點

git部署及應用

IT環境部署 && 自動化

作業系統安裝          COBBLER

服務部署              SALTSTACK

應用代碼部署          saltstack && shell

監控配置              zabbix

加入運維叢集          LVS && haproxy

安裝部署git

[root@linux-node2 ~]# yum install git -y

設定本地的使用者名和郵箱

[root@linux-node2 ~]# git config --global user.name "chenjisong"

[root@linux-node2 ~]# git config --global user.email "[email protected]"

[root@linux-node2 ~]# git config --global color.ui true

[root@linux-node2 ~]# git config --list

user.name=chenjisong

[email protected]

color.ui=true

建立一個版本庫:

[root@linux-node2 ~]# mkdir oldboy

[root@linux-node2 ~]# cd oldboy/

[root@linux-node2 oldboy]# git init

Initialized empty Git repository in /root/oldboy/.git/

[root@linux-node2 oldboy]# echo "1 hehe" > readme.txt 

[root@linux-node2 oldboy]# cat readme.txt 

1 hehe

[root@linux-node2 oldboy]# git add readme.txt                      ---添加至版本庫

[root@linux-node2 oldboy]# git commit -m "the first commit"    ---送出

[root@linux-node2 oldboy]# cat deploy.sh 

#!/bin/bash

echo hehe

[root@linux-node2 oldboy]# git add deploy.sh 

[root@linux-node2 oldboy]# git commit -m "2th commit"

[root@linux-node2 oldboy]# git log    -----檢視送出記錄

[root@linux-node2 oldboy]# cat readme.txt        -----加了一行

2 haha  

[root@linux-node2 oldboy]# git status         再次檢視狀态,提示檔案改變了

[root@linux-node2 oldboy]# git diff readme.txt         git diff對比兩次檔案修改之處

diff --git a/readme.txt b/readme.txt

index 408e625..5293fff 100644

--- a/readme.txt

+++ b/readme.txt

@@ -1 +1,2 @@

 1 hehe

+2 haha  

[root@linux-node2 oldboy]# git add readme.txt

[root@linux-node2 oldboy]# git commit -m "add 2haha" 

[master f951bc2] add 2haha

 1 files changed, 1 insertions(+), 0 deletions(-)

[root@linux-node2 oldboy]# git log           ----檢視送出記錄

commit f951bc28b0d2c55d320983c76bcd0523f2101973

Author: chenjisong <[email protected]>

Date:   Fri Nov 27 14:23:02 2015 +0800

    add 2haha 

commit 44c16df44ab14f8769569bd31cc586256f579911

Date:   Fri Nov 27 14:18:38 2015 +0800

    2th commit

commit fea2e2d32e1684523684c6acd83ba0dfdc3b4d56

Date:   Fri Nov 27 14:16:12 2015 +0800

    the first commit

[root@linux-node2 oldboy]# git reset --hard HEAD^    git reset版本回退指令   HEAD^表示上一版本

HEAD is now at 44c16df 2th commit

[root@linux-node2 oldboy]# git reflog                git reflog列出每一個版本

44c16df HEAD@{0}: HEAD^: updating HEAD

f951bc2 HEAD@{1}: commit: add 2haha

44c16df HEAD@{2}: commit: 2th commit

fea2e2d HEAD@{3}: commit (initial): the first commit

[root@linux-node2 oldboy]# git reset --hard fea2e2d     後面直接接回退的版本則OK

HEAD is now at fea2e2d the first commit

[root@linux-node2 oldboy]# ll                    ---此處沒有deploy.sh檔案了

總用量 4

-rw-r--r-- 1 root root 7 11月 27 14:26 readme.txt

總結:

1 git add readme.txt                           git add加入到暫存區

2 git commit -m "add 2haha"                    git commit 送出到工作區

3 git log                                      git log 檢視送出日志

4 git reset --hard HRAD^                       git reset回退版本

5 git reflog                                   git reflog檢視曆史送出記錄

6 git reset --hard fea2e2d                     git reset根據commit id來回退版本

####################################################################################################

2 wo shi hehe                    ---新加的一行

[root@linux-node2 oldboy]# git commit -m "add 2"

# On branch master

# Changed but not updated:

#   (use "git add <file>..." to update what will be committed)

#   (use "git checkout -- <file>..." to discard changes in working directory)

#

#       modified:   readme.txt

no changes added to commit (use "git add" and/or "git commit -a")   沒有任何的改變去送出

注意:必須先送出到暫存區,然後才能送出到工作區

[root@linux-node2 oldboy]# git add readme.txt           ----先送出到暫存區

[root@linux-node2 oldboy]# git commit -m "add 2"        ----送出到工作區才能成功

[master 0125b6d] add 2

2 wo shi hehe

3 hehe hehe                      ---新加的一行

[root@linux-node2 oldboy]# vim readme.txt 

3 hehe hehe

4 haha                                    新加的一行

[root@linux-node2 oldboy]# git checkout readme.txt  -git checkout,對剛修改,暫未送出的檔案回退

遠端倉庫:

[root@linux-node2 oldboy]# git remote add origin [email protected]:chenjisong/test.git

-----添加至遠端版本庫

[root@linux-node2 oldboy]# cat .git/config 

[core]

        repositoryformatversion = 0

        filemode = true

        bare = false

        logallrefupdates = true

[remote "origin"]

        url = [email protected]:chenjisong/test.git

        fetch = +refs/heads/*:refs/remotes/origin/*

73  git push -u origin master

74  git pull

75  git pull origin master             ----先pull下來

76  ls -a

77  git push -u origin master          ----然後再push上去

[root@linux-node2 tmp]# git clone [email protected]:chenjisong/test.git  遠端克隆别人的東西

Initialized empty Git repository in /tmp/test/.git/

remote: Counting objects: 16, done.

remote: Compressing objects: 100% (8/8), done.

remote: Total 16 (delta 1), reused 12 (delta 1), pack-reused 0

Receiving objects: 100% (16/16), 5.43 KiB, done.

Resolving deltas: 100% (1/1), done.

branch管理

[root@linux-node2 oldboy]# git branch dev

[root@linux-node2 oldboy]# git checkout dev

Switched to branch 'dev'

[root@linux-node2 oldboy]# git branch

* dev

  master

合并分支:如果要将dev與master進行合并,首先切換到master分支

[root@linux-node2 oldboy]# git checkout master

Switched to branch 'master'

[root@linux-node2 oldboy]# git merge dev

Updating 5dfef63..ae15e52

Fast-forward

 dev.txt |    1 +

 create mode 100644 dev.txt

[root@linux-node2 oldboy]# ll

總用量 24

-rw-r--r-- 1 root root    13 11月 27 15:31 dev.txt       -----dev分支的内容,已合并

-rw-r--r-- 1 root root 11358 11月 27 15:10 LICENSE

-rw-r--r-- 1 root root    12 11月 27 15:10 README.md

-rw-r--r-- 1 root root    33 11月 27 14:49 readme.txt 

  dev

* master

[root@linux-node2 oldboy]# git branch -d dev           -d   删除分支

[root@linux-node2 oldboy]# git checkout -b test        -b   建立分支

[root@linux-node2 oldboy]# git tag v1.0               git tag 打标簽

[root@linux-node2 oldboy]# git tag

v1.0    

     本文轉自陳繼松 51CTO部落格,原文連結:http://blog.51cto.com/chenjisong/1726299,如需轉載請自行聯系原作者