關(guān)于Git的總結(jié):
一瞒斩、Git的安裝:
1抒蚜、系統(tǒng)環(huán)境準備
[root@git ~]# cat /etc/redhat-release #查看系統(tǒng)版本
CentOS Linux release 7.1.1503(core)
[root@git ~]# uname -r #查看內(nèi)核版本
3.10.0-229.e7.x86_64
[root@git ~]# gentenforce #確認SELinux關(guān)閉狀態(tài)
Disabled
[root@git ~]# systemctl stop firewalld #關(guān)閉防火墻
[root@git ~]# rpm -qa git? #查看git是否安裝
git-1.8.3.1-13.el7.x86_64
2.Git安裝部署
?#安裝Git
[root@git ~]# yum install git
[root@git ~]# git config
--global? ? 使用全局配置文件
--system? ? 使用系統(tǒng)級配置文件
--local? ? 使用版本庫級配置文件
#配置git使用用戶
[root@git ~]# git config --global user.name "zhangsan"
#配置git使用郵箱
[root@git ~]# git config --global user.email "zhangsan@mail.com"
#語法高亮
[root@git ~]# git config --global color.ui true
#查看當前的配置:
[root@git ~]# git config --list
user.name=zhangsan
user.email=zhangsan@mail.com
color.ui=true
#查看用戶:
[root@git ~]# cat .gitconfig
[user]
? ? ? name = zhangsan
? email = zhangsan@mail.com
[color]
? ? ? ui = true
3.Git初始化
初始化工作目錄,對已存在的目錄都可以進行初始化
mkdir git_data
cd git_data/
(1)#初始化
git init
(2)#查看工作區(qū)狀態(tài)
git status
#隱藏文件介紹
branches? #分支目錄
config? ? #定義項目特有的配置選項
description? #僅供git web 程序使用
HEAD? #指示當前的分支
hooks # 包含git鉤子文件
info? # 包含一個全局排除文件(exclude文件)
objects #存放所有數(shù)據(jù)內(nèi)容院究,有info和pack兩個子文件夾
refs? ? #存放指向數(shù)據(jù)(分支)的提交對象的指針
index? #保存暫存區(qū)信息洽瞬,在執(zhí)行g(shù)it init 的時候,這個文件還沒有
---------------------------------------------------------------
(3)#創(chuàng)建空目錄:
[root@git ~]# mkdir data
#進行初始化:
[root@git ~]# cd data/
[root@git data]# git init
#初始化了一個空的倉庫业汰,位于root下的data的.git
#查看代碼倉庫:
[root@git data]# ll -a
drwxr-xr-x 7 root root 225 Jan 24 16:36 .git?
#查看工作區(qū)的狀態(tài):
[root@git data]# git status
#On branch master
#Initial commit
nothing to commit
#git的工作目錄:
[root@git data]# pwd
/root/data
4.Git常規(guī)使用
(1)創(chuàng)建數(shù)據(jù)--提交數(shù)據(jù)
注意:
把代碼提交到本地倉庫伙窃,必須經(jīng)過 暫存區(qū)域。
只有提交到本地倉庫的代碼样漆,才被系統(tǒng)管理起來为障。
(2)git基礎(chǔ)命令:
#查看工作區(qū)的狀態(tài):
[root@git data]# git status
#位于分支 master
#初始提交
無文件要提交(創(chuàng)建/拷貝文件并使用"git add"建立跟蹤)
(3)創(chuàng)建文件:
[root@git data]# touch a b c
[root@git data]# git status
#位于分支 master
#初始提交
#未跟蹤的文件
#(使用"git add <file>..."以包含要提交的內(nèi)容)
#
#? a
#? b
#? c
提交為空,但是存在尚未跟蹤的文件(使用"git add"建立跟蹤)
=======================================================
(4)#把a文件提交到暫存區(qū):
[root@git data]# git add a
[root@git data]# git status
#位于分支 master
#初始提交
# 要提交的變更
# (使用 "git rm --cached <file>..."撤出暫存區(qū))
#? 新文件:? a
#未跟蹤的文件:
#(使用"git add <file>..."以包含要提交的內(nèi)容)
#
#?
#? b
#? c
=======================================================
(5)#把所有的文件提交到暫存區(qū)域:
[root@git data]# git add .
[root@git data]#
#查看狀態(tài):
[root@git data]# git status
#On branch master
#Initial commit
#Changes to be commited:
# (Use "git rm --cached <file>..." to unstage)
#
#? new file:? a
#? new file:? b
#? new file:? c
=======================================================
(6)#刪除文件
1.先從暫存區(qū)撤回到工作區(qū)放祟,然后直接刪除文件
git rm --cached c
rm -f c
2.直接從暫存區(qū)域同工作區(qū)域一同刪除文件命令
git rm -f b
(7)#把C文件從暫存區(qū)撤回來:
[root@git data]# git rm --cached c
rm 'c'
[root@git data]# ll
總用量 0
#查看狀態(tài):
[root@git data]# git status
#On branch master
#Initial commit
#Changes to be commited:
# (Use "git rm --cached <file>..." to unstage)
#
#? new file:? a
#? new file:? b
#
#Untracked files
# (use "git add<file>..." to include in what will be comminted)
#
#? c
=======================================================
(8)#提交到本地倉庫
[root@git data]# git commit -m "add newfile a"
[master (root-commit)295e997] add newfile a
1 file changed,0 insertions(+),0 deletions(-)
create mode 100644 a
[root@git data]# git status
#On branch master
nothing to commit,working directory clean
=======================================================
(9)修改文件名稱的兩種方法:
方法一:
[root@git data]# mv a a.txt
[root@git data]# git status
#從緩存區(qū)刪除a文件
[root@git data]# git rm --cached a
[root@git data]# git status
方法二:(推薦)
直接使用git命令重命名
#把工作區(qū)域和暫存區(qū)域的文件同時修改文件名稱
[root@git data]# git mv a.txt a
[root@git data]# git status
(10)查看兩個文件有什么不同:
[root@git ~]# echo 0123 > 1.txt
[root@git ~]# echo 7890 > 2.txt
[root@git ~]# diff 1.txt 2.txt
1c1
< 0123
---
> 7890?
=======================================================
(11)查看工作目錄和暫存區(qū)有啥不同:
[root@git data]# git diff
[root@git data]# ll
total 0
-rw-r--r-- 1 root root 0 Jan 24 16:46 a
--------------------------------------------------------
#對比暫存區(qū)和本地倉庫有什么不同:
#把index添加到a:(在工作目錄中)
[root@git data]# echo index > a
[root@git data]# cat a
index
[root@git data]# git diff
diff? --git a/a b/a
index e69de29..9015a7a 100644
--- a/a
+++ b/a
@@ -0,0 +1 @@
+index
---------------------------------------------------------
[root@git data]# git diff? --cached
[root@git data]# git diff
diff --git a/a b/a
index e69de29..9015a7a 100644
@@ -0,0 +1 @@
+index
[root@git data]# cat a
index
=======================================================
#出現(xiàn)暫存區(qū)和倉庫區(qū)鳍怨,不一致,需要把文件添加到倉庫區(qū)跪妥,就一致了鞋喇。
[root@git data]# git add a
[root@git data]# git diff
[root@git data]# git diff --cached
diff --git a/a b/a
index e69de29..9015a7a 100644
@@ -0,0 +1 @@
+index
[root@git data]# git commit -m "add index"
[master bd2dea9] add index
1 file change,1 insertion(+)
[root@git data]# git diff
[root@git data]# git diff --cached
-------------------------------------------------
#這兩段代碼是一致的:
[root@git data]# echo 123 >> a
[root@git data]# git add .
[root@git data]# git commit -m "XXX"
#(推薦使用)
[root@git data]# git commit -am "add 123 > a"
[master dcccbf1] add 123 > a
1 file change,1 insertion(+)
[root@git data]#
[root@git data]# git status
#On branch master
nothing to commit,working directory clean
=================================================
#提交后在對比暫存區(qū)和本地倉庫內(nèi)容相同:
[root@git data]# git commit -m "modified a"
[master 6r78bf1] modified a
[root@git data]# git diff --cached a
#相當于虛擬機的鏡像,任何操作都被做了一次快照眉撵,
可恢復(fù)到任意一個位置:
#git commit
----------------------------------------------------
#查看歷史的git commit快照操作
[root@git data]# git log
#一行簡單的顯示commit信息
[root@git data]# git log --oneline
#顯示當前的指針指向哪里:
[root@git data]# git log --oneline --decorate
#顯示具體內(nèi)容的變化
[root@git data]# git log -p
#只顯示1條內(nèi)容:
[root@git data]# git log -1
====================================================
5.恢復(fù)歷史數(shù)據(jù):
(1)只更改了當前目錄:
[root@git data]# echo "333" >> a
[root@git data]# git status
#位于分支 master
#尚未暫存區(qū)以備提交的變更:
# (使用"git add <file>..."更新要提交的內(nèi)容)
#看提示使用此命令覆蓋工作區(qū)的改動(解釋下面)
#? (使用"git checkout --<file>..."丟棄工作區(qū)的改動)
#
#? 修改:? a
#
修改尚未加入提交(使用 "git add"和/或"git commit -a")
#從暫存區(qū)覆蓋本地工作目錄
[root@git data]# git checkout -- a
[root@git data]# git status
#位于分支 master
無文件要提交侦香,干凈的工作區(qū)
[root@git ~]# cat a
aaa
(2)修改了本地目錄且同時提交到了暫存區(qū)
#添加新內(nèi)容:
[root@git data]# echo ccc >> a
#提交到暫存區(qū):
[root@git data]# git add .
#對比暫存區(qū)和本地倉庫的內(nèi)容
[root@git data]# git diff --cached
diff --git a/a b/a
index e69de29..9015a7a 100644
--- a/a
+++ b/a
@@ -1,+1,2 @@
aaa
ccc
------------------------------------------
[root@git data]# git status
#位于分支 master
#要提交的變更:
# (使用"git reset <file>..."撤出暫存區(qū))
#? 修改:? a
#本地倉庫覆蓋暫存區(qū)域
[root@git data]# git reset HEAD a
#重置后撤出暫存區(qū)的變更:
M? ? a
[root@git data]# git diff a
diff --git a/a b/a
index e69de29..9015a7a 100644
--- a/a
+++ b/a
@@ -1,+1,2 @@
aaa
+ccc
=========================================
(3)恢復(fù)原始數(shù)據(jù):
[root@git data]# git log --online
dcccbf1 add 123 > a
bd2dea9 add index
951adcc mv a.txt a
799bc0a modified a a.txt
295e997 add newfile a
[root@git data]# cat a
index
123
[root@git data]# git reset --hard 295e997
HEAD is now at 295e997 add newfile a
[root@git data]# git status
#On branch master
nothing to commit,working directory clean
#說明已經(jīng)回到了原始的a:
[root@git data]# git log
commit 295e997ff
Author:zhangsan<zhangsan@mail.com>
Date:?
? add newfile a
[root@git data]# cat a
[root@git data]# ll
total 0
-rw-r--r-- 1 root root 0 Jan 24 17:46 a
#發(fā)現(xiàn)沒有問題,想回到最后一次的修改:
[root@git data]# git log --online
295e997 add newfile a
[root@git data]# git reset --hard dcccbf1
HEAD is now at dcccbf1 add 123 > a
[root@git data]# ll
total 4
-rw-r--r-- 1 root root 0 Jan 24 17:47 a
[root@git data]# cat a
index
#查看所有的歷史操作:
[root@git data]# git relog
=====================(持續(xù)更新)=====================