Git系列二之數據管理

筆者Q:552408925、572891887
架構師羣:471443208
bjstack運維社區:524721466

1.Git基本管理

git常用的基本操作

1.1提交數據

我們可以簡單的把工作目錄理解成是一個被Git服務程序管理的目錄,Git會時刻的追蹤目錄內文件的改動,另外在安裝好了Git服務程序後,默認就會創建好了一個叫做master的分支,我們直接可以提交數據到了

1.創建本地工作目錄demo,並初始化爲git工作目錄

[root@git-node1 ~]# mkdir demo
[root@git-node1 ~]# cd demo/
[root@git-node1 git-demo]# git init
Initialized empty Git repository in /root/demo/.git/

2.創建readme.txt文件

[root@git-node1 demo]# touch index.html

3.查看git狀態

[root@git-node1 demo]# git status
# 位於分支 master
#
# 初始提交
#
# 未跟蹤的文件:
#   (使用 "git add <file>..." 以包含要提交的內容)
#
#   index.html
提交爲空,但是存在尚未跟蹤的文件(使用 "git add" 建立跟蹤)

4.提示使用git add添加文件至暫存區

[root@git-node1 demo]# git add index.html
[root@git-node1 demo]# git status  //查看變更狀態
# 位於分支 master
#
# 初始提交
#
# 要提交的變更:
#   (使用 "git rm --cached <file>..." 撤出暫存區)
#
#   新文件:    index.html

5.使用git cmmit提交暫存區文件至git版本倉庫 -m:提交描述信息(提交至遠程需添加遠程倉庫)

[root@git-node1 demo]# git commit -m "the first commit index.html"
[master(根提交) 85bd268] the first commit index.html
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 index.html

1.2移除數據

有些時候會將已經添加到暫存區的文件移除,但仍然希望文件在工作目錄中不丟失,換句話說,就是把文件從追蹤清單中刪除。

1.建立新文件

[root@git-node1 demo]# touch index.php

2.添加新文件至暫存區

[root@git-node1 demo]# git add index.php
[root@git-node1 demo]# git status
# 位於分支 master
# 要提交的變更:
#   (使用 "git reset HEAD <file>..." 撤出暫存區)
#
#   新文件:    index.php
#

3.將新文件從暫存區撤出(並不會刪除工作目錄內的數據)

[root@git-node1 demo]# git reset HEAD index.php
[root@git-node1 demo]# git status  //index.php文件狀態爲未跟蹤
# 位於分支 master
# 未跟蹤的文件:
#   (使用 "git add <file>..." 以包含要提交的內容)
#
#   index.php
提交爲空,但是存在尚未跟蹤的文件(使用 "git add" 建立跟蹤)
如果想將文件數據從git暫存區和工作目錄一起刪除,可以做如下操作。
[root@git-node1 demo]# git add index.php
[root@git-node1 demo]# git rm index.php //文件存已經放入暫存區域,防止誤刪
error: 'index.php' 有變更已暫存至索引中
(使用 --cached 保存文件,或用 -f 強制刪除)

[root@git-node1 demo]# git rm -f index.php  //使用追加強制刪除-f參數。或者—cache保存
rm 'index.php'
[root@git-node1 demo]# git status //查看當前狀態
# 位於分支 master
無文件要提交,乾淨的工作區
[root@git-node1 demo]# ls  //index.php文件已經被刪除
index.html

1.3移動數據

有些時候會將已經添加到暫存區的文件重新修改名稱,可使用如下操作完成。
1.git重新命名文件名稱,使用git mv操作。

[root@git-node1 demo]# git mv index.html index.php

2.使用git status查看更改狀態

[root@git-node1 demo]# git status
# 位於分支 master
# 要提交的變更:
#   (使用 "git reset HEAD <file>..." 撤出暫存區)
#
#   重命名:    index.html -> index.php
#

3.使用git commit 提交git倉庫,並描述提交信息

[root@git-node1 demo]# git commit -m "chnged name index.html->index.php"
[master 9573413] chnged name index.html->index.php
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename index.html => index.php (100%)

1.4歷史數據

在提交了若干更新之後,想回顧下提交歷史,可以使用 git log 命令查看提交歷史記錄。
1.查看提交歷史記錄

[root@git-node1 demo]# git log
commit 95734131860ef7c9078b8a208ff6437d0952380b
Author: xuliangwei <[email protected]>
Date:   Sun Nov 6 02:15:35 2016 +0800

    chnged name index.html->index.php

commit 85bd2680bd4b70aeded9dbd230c07ab086712ff9
Author: xuliangwei <[email protected]>
Date:   Sun Nov 6 01:51:22 2016 +0800

    the first commit index.html

2.查看歷史2次提交記錄

[root@git-node1 demo]# git log -10
commit 95734131860ef7c9078b8a208ff6437d0952380b
Author: xuliangwei <[email protected]>
Date:   Sun Nov 6 02:15:35 2016 +0800

    chnged name index.html->index.php

commit 85bd2680bd4b70aeded9dbd230c07ab086712ff9
Author: xuliangwei <[email protected]>
Date:   Sun Nov 6 01:51:22 2016 +0800

    the first commit index.html

3.顯示提交的內容差異,例如僅查看最近一次差異

[root@git-node1 demo]# git log -p -2
commit 95734131860ef7c9078b8a208ff6437d0952380b
Author: xuliangwei <[email protected]>
Date:   Sun Nov 6 02:15:35 2016 +0800

    chnged name index.html->index.php

diff --git a/index.html b/index.html
deleted file mode 100644
index e69de29..0000000
diff --git a/index.php b/index.php
new file mode 100644
index 0000000..e69de29

commit 85bd2680bd4b70aeded9dbd230c07ab086712ff9
Author: xuliangwei <[email protected]>
Date:   Sun Nov 6 01:51:22 2016 +0800

    the first commit index.html

diff --git a/index.html b/index.html
new file mode 100644
index 0000000..e69de29

4.簡要顯示數據增改行數

[root@git-node1 demo]# git log --stat
commit 95734131860ef7c9078b8a208ff6437d0952380b
Author: xuliangwei <[email protected]>
Date:   Sun Nov 6 02:15:35 2016 +0800

    chnged name index.html->index.php

 index.html | 0
 2 files changed, 0 insertions(+), 0 deletions(-)

commit 85bd2680bd4b70aeded9dbd230c07ab086712ff9
Author: xuliangwei <[email protected]>
Date:   Sun Nov 6 01:51:22 2016 +0800

    the first commit index.html

 index.html | 0
 1 file changed, 0 insertions(+), 0 deletions(-)

5.根據不同的格式展示提交的歷史信息

[root@git-node1 demo]# git log --pretty=oneline
95734131860ef7c9078b8a208ff6437d0952380b chnged name index.html->index.php
85bd2680bd4b70aeded9dbd230c07ab086712ff9 the first commit index.htm
可以使用format參數來指定具體的輸出格式,這樣非常便於後期編程的提取分析哦,常用的格式有:
%s  提交說明。
%cd 提交日期。
%an 作者的名字。
%cn 提交者的姓名。
%ce 提交者的電子郵件。
%H  提交對象的完整SHA-1哈希字串。
%h  提交對象的簡短SHA-1哈希字串。
%T  樹對象的完整SHA-1哈希字串。
%t  樹對象的簡短SHA-1哈希字串。
%P  父對象的完整SHA-1哈希字串。
%p  父對象的簡短SHA-1哈希字串。
%ad 作者的修訂時間。
定製詳細的日誌輸出
[root@git-node1 demo]# git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset %cn"' --abbrev-commit --date=relative

*   5f3f588 - (HEAD, origin/master, –r, tt, master) merge branch linux readme.txt (68 分鐘之前) xulia
|  
| * 4580c3b - (linux) touch readme.txt (80 分鐘之前) xuliangwei"
* | 4c7a145 - touch readme.txt (72 分鐘之前) xuliangwei"
|/ 
* 9573413 - (tag: v1.0.0) chnged name index.html->index.php (5 小時之前) xuliangwei"
* 85bd268 - the first commit index.html (5 小時之前) xuliangwei"
配置.git/config文件,增加如下2行,以後即可使用 git hlog代替如上覆雜命令
[root@git-node1 demo]# tail -2 .git/config
[alias]
    hlog = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset %cn' --abbrev-commit --date=relative

//以後使用git hlog即可實現複雜的命令

1.5還原數據

將文件boss good寫成了boss dou,但是已經提交至遠程倉庫,現在想要撤回重新提交。
(本小結涉及遠程倉庫,可先學習後面章節,在回頭學習本章節)
1.查看index.html文件

[root@git-node1 demo]# git checkout -b dev //內容存在dev分支
[root@git-node1 demo]# cat index.html
hello boss
[root@git-node1 demo]# echo "boos  doubi" >> index.html

2.追加內容至index.html,並提交至遠程倉庫

[root@git-node1 demo]# git add index.html
[root@git-node1 demo]# git commit -m "boss dou"
[master 1941990] boss dou
 1 file changed, 1 insertion(+)
[root@git-node1 demo]#  git push origin dev
Counting objects: 5, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 298 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@git-node1:root/git_demo.git
   507bf99..1941990  dev -> dev

3.此時覺得寫的不妥,想還原上一次提交的文件快照,找出歷史信息

[root@git-node1 demo]# git hlog -2
* 1941990 - (HEAD, origin/dev, dev) boss dou (2 分鐘之前) xuliangwei
* 507bf99 - hello (30 分鐘之前) xuliangwei

4.找到歷史還原點的值後,就可以還原(值不寫全,系統會自動匹配)

[root@git-node1 demo]# git reset --hard 507bf99
HEAD 現在位於 507bf99 hello
[root@git-node1 xuliangwei]# git push origin dev -f  //強制推送至dev分支
[root@git-node1 demo]# cat index.html
hello boss

1.6基本操作小結

相信大家對Git的基本操作有一定的瞭解,下面進行一下簡單的梳理總結:

命令 git config --global user.name "name"  #配置git使用用戶
# git config --global user.email "mail"  #配置git使用郵箱
# git config --global color.ui true #配置顏色
# git config --list #查看當前配置
# git init  #初始爲git工作目錄
# git status  #查看git狀態
# git reflog  #查看未來歷史更新點
# git reset --hard 4bf5b29  #找到歷史還原點的SHA-1值,就可以還原(值不寫全,系統會自動匹配)
# git checkout -- file #恢復暫存區至上一版本
# git add [file1] [file2] ... #添加指定文件至暫存區
# git add [dir] #添加指定目錄至暫存區,包括子目錄(遞歸添加)
# git add . #添加當前目錄所有文件至暫存區
# git rm [file1] [file2] ... #刪除工作區文件,並將這次刪除放入暫存區
# git rm –cached [file] #停止追蹤指定文件,但該文件會保留在工作區
# git mv [file-old] [file-new] #重命名文件,修改後放入暫存區
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章