關(guān)于Git的實踐總結(jié)(一)

關(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ù)更新)=====================

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末纽疟,一起剝皮案震驚了整個濱河市罐韩,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌仰挣,老刑警劉巖伴逸,帶你破解...
    沈念sama閱讀 217,907評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件缠沈,死亡現(xiàn)場離奇詭異膘壶,居然都是意外死亡,警方通過查閱死者的電腦和手機洲愤,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,987評論 3 395
  • 文/潘曉璐 我一進店門颓芭,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人柬赐,你說我怎么就攤上這事亡问。” “怎么了?”我有些...
    開封第一講書人閱讀 164,298評論 0 354
  • 文/不壞的土叔 我叫張陵州藕,是天一觀的道長束世。 經(jīng)常有香客問我,道長床玻,這世上最難降的妖魔是什么毁涉? 我笑而不...
    開封第一講書人閱讀 58,586評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮锈死,結(jié)果婚禮上贫堰,老公的妹妹穿的比我還像新娘。我一直安慰自己待牵,他們只是感情好其屏,可當我...
    茶點故事閱讀 67,633評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著缨该,像睡著了一般偎行。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上贰拿,一...
    開封第一講書人閱讀 51,488評論 1 302
  • 那天睦优,我揣著相機與錄音,去河邊找鬼壮不。 笑死汗盘,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的询一。 我是一名探鬼主播隐孽,決...
    沈念sama閱讀 40,275評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼健蕊!你這毒婦竟也來了菱阵?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,176評論 0 276
  • 序言:老撾萬榮一對情侶失蹤缩功,失蹤者是張志新(化名)和其女友劉穎晴及,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體嫡锌,經(jīng)...
    沈念sama閱讀 45,619評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡虑稼,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,819評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了势木。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蛛倦。...
    茶點故事閱讀 39,932評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖啦桌,靈堂內(nèi)的尸體忽然破棺而出溯壶,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 35,655評論 5 346
  • 正文 年R本政府宣布且改,位于F島的核電站验烧,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏又跛。R本人自食惡果不足惜噪窘,卻給世界環(huán)境...
    茶點故事閱讀 41,265評論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望效扫。 院中可真熱鬧倔监,春花似錦、人聲如沸菌仁。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,871評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽济丘。三九已至谱秽,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間摹迷,已是汗流浹背疟赊。 一陣腳步聲響...
    開封第一講書人閱讀 32,994評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留峡碉,地道東北人近哟。 一個月前我還...
    沈念sama閱讀 48,095評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像鲫寄,于是被迫代替她去往敵國和親吉执。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,884評論 2 354

推薦閱讀更多精彩內(nèi)容

  • 一地来、基本概念: 注:對于git的分布式概念及其優(yōu)點戳玫,不重復(fù)說明,自己百度或谷歌未斑。本文中涉及到指令前面有$的咕宿,在cm...
    大廠offer閱讀 1,425評論 0 3
  • 大綱: 一、前言 二蜡秽、概述 三府阀、在Windows上安裝Git 四、創(chuàng)建本地倉庫 五载城、本地倉庫管理詳解 六肌似、總結(jié) 注...
    首席架構(gòu)師閱讀 365評論 1 3
  • 一 Git配置和倉庫初始化 下面會介紹Git的使用,每個小節(jié)里會講解各個功能在命令行中的實現(xiàn)方式,并在每小節(jié)的最后...
    Happioo閱讀 3,362評論 0 5
  • 以下筆記主要參考gitgot睬澡,大致了解git使用和原理固额。 第一部分我們從個人的視角去研究如何用好Git,并且揭示G...
    carolwhite閱讀 2,379評論 0 1
  • Git 是目前最流行的分布式版本控制系統(tǒng)之一煞聪。 版本控制指的是斗躏,記錄每次版本變更的內(nèi)容和時間等細節(jié),保留各版本之間...
    神齊閱讀 1,425評論 0 7