溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

入門級(jí)的Git操作方法是什么

發(fā)布時(shí)間:2021-12-31 17:15:22 來源:億速云 閱讀:129 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要講解了“入門級(jí)的Git操作方法是什么”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“入門級(jí)的Git操作方法是什么”吧!

導(dǎo)讀Git是一個(gè)開源的分布式版本控制系統(tǒng),可以有效、高速地處理從很小到非常大的項(xiàng)目版本管理。

git全局配置

一般在新的系統(tǒng)上,我們都需要先配置下自己的Git工作環(huán)境。配置工作只需進(jìn)行一次,以后升級(jí)時(shí)還會(huì)沿用現(xiàn)在的配置。如果需要,你隨時(shí)可以用相同的 命令修改已有的配置:

git config --global user.name "Breeze Yan"    #配置全局用戶名
git config --global user.email "yanw02@mysoft.com.cn"  #配置全局用戶郵箱
git config --unset --global user.name "Breeze Yan" #取消全局用戶名配置
git config --unset --global user.email "breeze.yan@newbiiz.com" 
git config --list    #查看git配置
git config user.name
git config user.email

創(chuàng)建一個(gè)版本庫

#創(chuàng)建一個(gè)目錄:

mkdir git_test

#在項(xiàng)目目錄下執(zhí)行如下操作完成初始化操作:

git init

初始化完成以后,在項(xiàng)目目錄下會(huì)出現(xiàn)一個(gè).git的目錄,所有g(shù)it需要的數(shù)據(jù)和資源都存放在這個(gè)目錄中

git的常用操作

在工作目錄下面的所有文件都無外乎這兩種狀態(tài):已跟蹤或未跟蹤。已跟蹤的文件指的是已經(jīng)被納入版本控制管理的文件。已跟蹤的文件,它們的狀態(tài)可能是已修改,已暫存,或者未更新(未修改)。而未跟蹤的文件,它們既沒有上次更新的快照,也不在當(dāng)前的暫存區(qū)域,通常情況下它們就是那些在工作目錄下新創(chuàng)建的文件。

在對(duì)某些文件進(jìn)行編輯之后,git將這些文件標(biāo)記為已修改。我們會(huì)逐步把這些修改過的文件保存到暫存區(qū)域,直到最后一次一次性的提交所有這些位于暫存區(qū)域的文件,如此重復(fù)。所以使用Git時(shí)的文件狀態(tài)變化周期如圖所示

版本提交與回退

版本提交
在git_test目錄下創(chuàng)建一個(gè)文件code.txt,內(nèi)容如下:

this is the first line

通過如下 命令提交一個(gè)版本:

yanwei@ubuntu:~/git_test$ git add code.txt
yanwei@ubuntu:~/git_test$ git commit -m 'first commit'
[master (根提交) d66bdc0] first commit
 1 file changed, 1 insertion(+)
 create mode 100644 code.txt

使用如下命令可以查看版本記錄:

yanwei@ubuntu:~/git_test$ git log
commit d66bdc0189d3663db2feed6193c00751b277e80d (HEAD -> master)
Author: yanweiDate:   Sun Jul 15 22:35:33 2018 +0800

版本回退

再次提交一個(gè)版本:
# 在code.txt中再添加一行之后,內(nèi)容如下:

yanwei@ubuntu:~/git_test$ cat code.txt 
this is the first line
this is the second line

# 再次提交:

yanwei@ubuntu:~/git_test$ git add code.txt
yanwei@ubuntu:~/git_test$ git commit -m 'second commit'
[master 227ecaa] second commit
1 file changed, 1 insertion(+)
 
yanwei@ubuntu:~/git_test$ git log
commit 227ecaa7a5aeca38d392662263f2704c66e1e64a (HEAD -> master)
Author: yanweiDate:   Sun Jul 15 22:43:49 2018 +0800
second commit
commit d66bdc0189d3663db2feed6193c00751b277e80d
Author: yanweiDate:   Sun Jul 15 22:35:33 2018 +0800
first commit

現(xiàn)在若想回退到上一個(gè)版本,可以使用如下命令:

yanwei@ubuntu:~/git_test$ git reset --hard HEAD^
HEAD 現(xiàn)在位于 d66bdc0 first commit
yanwei@ubuntu:~/git_test$ git log
commit d66bdc0189d3663db2feed6193c00751b277e80d (HEAD -> master)
Author: yanweiDate:   Sun Jul 15 22:35:33 2018 +0800
first commit
    
yanwei@ubuntu:~/git_test$ cat code.txt 
this is the first line

其中HEAD表示當(dāng)前最新版本,HEAD^表示當(dāng)前版本的上一個(gè)版本,HEAD^^表示當(dāng)前版本的上上個(gè)版本,也可以使用HEAD~1表示當(dāng)前版本的前一個(gè)版本,HEAD~100表示當(dāng)前版本的前100版本。

假如這個(gè)時(shí)候,又需要回到second commit版本,可以使用如下命令:

# 通過如下命令找到操作記錄:

yanwei@ubuntu:~/git_test$ git reflog
d66bdc0 (HEAD -> master) HEAD@{0}: reset: moving to HEAD^
227ecaa HEAD@{1}: commit: second commit
d66bdc0 (HEAD -> master) HEAD@{2}: commit (initial): first commit

# 回退到second commit:

yanwei@ubuntu:~/git_test$ git reset --hard 227ecaa
HEAD 現(xiàn)在位于 227ecaa second commit
yanwei@ubuntu:~/git_test$ git log
commit 227ecaa7a5aeca38d392662263f2704c66e1e64a (HEAD -> master)
Author: yanweiDate:   Sun Jul 15 22:43:49 2018 +0800
second commit
commit d66bdc0189d3663db2feed6193c00751b277e80d
Author: yanweiDate:   Sun Jul 15 22:35:33 2018 +0800
first commit

工作區(qū)、版本庫與暫存區(qū)

工作區(qū)

我們操作文件的目錄,如git_test,就是一個(gè)工作區(qū)

版本庫

工作區(qū)有一個(gè)隱藏目錄.git,這個(gè)不是工作區(qū),而是git的版本庫。

git的版本庫里存了很多東西,其中最重要的就是稱為stage(或者叫index)的暫存區(qū),還有g(shù)it為我們自動(dòng)創(chuàng)建的第一個(gè)分支master,以及指向master的一個(gè)指針叫HEAD。

因?yàn)槲覀儎?chuàng)建git版本庫時(shí),git自動(dòng)為我們創(chuàng)建了唯一一個(gè)master分支,所以,現(xiàn)在,git commit就是往master分支上提交更改。

可以簡單理解為,需要提交的文件修改全部放到暫存區(qū),然后,一次性提交暫存區(qū)的所有修改。

work-stage-repo

前面我們把文件往git版本庫里添加的時(shí)候,是分兩步執(zhí)行的:

第一步是用git add把文件添加進(jìn)去,實(shí)際上就是把文件修改添加到暫存區(qū);

第二步是用git commit提交更改,實(shí)際上就是把暫存區(qū)的所有內(nèi)容提交到當(dāng)前分支。

下面,我們?cè)趃it_test目錄下再創(chuàng)建一個(gè)文件code2.txt,內(nèi)容如下:

yanwei@ubuntu:~/git_test$ cat code2.txt 
the code2 first line

然后再次編輯code.txt,在其中加入一行,編輯后內(nèi)容如下:

yanwei@ubuntu:~/git_test$ cat code.txt 
this is the first line
this is the second line
this is the third line

使用git status查看當(dāng)前工作樹的狀態(tài):

yanwei@ubuntu:~/git_test$ git status
位于分支 master
尚未暫存以備提交的變更:
  (使用 "git add <文件>..." 更新要提交的內(nèi)容)
  (使用 "git checkout -- <文件>..." 丟棄工作區(qū)的改動(dòng))
	修改:     code.txt
未跟蹤的文件:
  (使用 "git add <文件>..." 以包含要提交的內(nèi)容)
	code2.txt

修改尚未加入提交(使用 "git add" 和/或 "git commit -a")

上面提示code.txt被修改,而code2.txt沒有被跟蹤。

我們將code.txt和code2.txt加入到暫存區(qū),然后再次查看工作樹狀態(tài):

yanwei@ubuntu:~/git_test$ git add code.txt
yanwei@ubuntu:~/git_test$ git add code2.txt
yanwei@ubuntu:~/git_test$ git status
位于分支 master
要提交的變更:
  (使用 "git reset HEAD <文件>..." 以取消暫存)
	修改:     code.txt
	新文件:   code2.txt

然后,執(zhí)行g(shù)it commit就可以一次性把暫存區(qū)的所有修改提交到分支創(chuàng)建一個(gè)版本:

yanwei@ubuntu:~/git_test$ git commit -m 'third commit'
[master e4fb2aa] third commit
 2 files changed, 2 insertions(+)
 create mode 100644 code2.txt
yanwei@ubuntu:~/git_test$ git log
commit e4fb2aa04ca8aa3b6a32ef46a69fa5f97ae625fa (HEAD -> master)
Author: yanweiDate:   Sun Jul 15 23:16:56 2018 +0800
    third commit
commit 227ecaa7a5aeca38d392662263f2704c66e1e64a
Author: yanweiDate:   Sun Jul 15 22:43:49 2018 +0800
    second commit
commit d66bdc0189d3663db2feed6193c00751b277e80d
Author: yanweiDate:   Sun Jul 15 22:35:33 2018 +0800
    first commit

一旦提交后,在沒有再次對(duì)工作區(qū)作修改之前,那么工作區(qū)就是“干凈”的:

yanwei@ubuntu:~/git_test$ git status
位于分支 master
無文件要提交,干凈的工作區(qū)
現(xiàn)在版本庫變成了如下模樣:
work-stage-repo

管理文件的修改

修改文件
在code.txt中再次添加一行內(nèi)容,修改后的內(nèi)容如下:

yanwei@ubuntu:~/git_test$ cat code.txt 
this is the first line
this is the second line
this is the third line
this is the forth line

然后使用git add命令將其添加到暫存區(qū)

yanwei@ubuntu:~/git_test$ git add code.txt
yanwei@ubuntu:~/git_test$ git status
位于分支 master
要提交的變更:
  (使用 "git reset HEAD <文件>..." 以取消暫存)
	修改:     code.txt

再次修改該文件,添加一行內(nèi)容,修改后的內(nèi)容如下:

yanwei@ubuntu:~/git_test$ cat code.txt 
this is the first line
this is the second line
this is the third line
this is the forth line
this is the fifth line

通過git commit提交一個(gè)版本,并使用git status查看,發(fā)現(xiàn)第二次修改code.txt內(nèi)容之后,并沒有將其添加到暫存區(qū),所以創(chuàng)建新版本的時(shí)候,并沒有被提交:

yanwei@ubuntu:~/git_test$ git status
位于分支 master
要提交的變更:
  (使用 "git reset HEAD <文件>..." 以取消暫存)
	修改:     code.txt
尚未暫存以備提交的變更:
  (使用 "git add <文件>..." 更新要提交的內(nèi)容)
  (使用 "git checkout -- <文件>..." 丟棄工作區(qū)的改動(dòng))
	修改:     code.txt
yanwei@ubuntu:~/git_test$ git commit -m 'forth commit'
[master 0a96a0f] forth commit
 1 file changed, 1 insertion(+)
yanwei@ubuntu:~/git_test$ git status
位于分支 master
尚未暫存以備提交的變更:
  (使用 "git add <文件>..." 更新要提交的內(nèi)容)
  (使用 "git checkout -- <文件>..." 丟棄工作區(qū)的改動(dòng))
	修改:     code.txt

修改尚未加入提交(使用 "git add" 和/或 "git commit -a")

撤銷修改

丟棄工作區(qū)的改動(dòng)
可以使用git checkout -- <文件>來丟棄工作區(qū)的改動(dòng):

# 使用如下指令來撤銷我們第二次的修改

yanwei@ubuntu:~/git_test$ git checkout -- code.txt
yanwei@ubuntu:~/git_test$ git status

位于分支 master

無文件要提交,干凈的工作

# 再次查看code.txt文件,發(fā)現(xiàn)最后一次修改已被丟棄:

yanwei@ubuntu:~/git_test$ cat code.txt 
this is the first line
this is the second line
this is the third line
this is the forth line

撤銷暫存區(qū)的修改

我們?cè)俅尉庉媍ode.txt,添加一行內(nèi)容,并添加到暫存區(qū):

yanwei@ubuntu:~/git_test$ cat code.txt 
this is the first line
this is the second line
this is the third line
this is the forth line
new line
yanwei@ubuntu:~/git_test$ git add code.txt
yanwei@ubuntu:~/git_test$ git status
位于分支 master
要提交的變更:
  (使用 "git reset HEAD <文件>..." 以取消暫存)
	修改:     code.txt

通過如下命令,將暫存區(qū)的修改撤銷,重新放回工作區(qū):

yanwei@ubuntu:~/git_test$ git reset HEAD code.txt
重置后取消暫存的變更:
M	code.txt
yanwei@ubuntu:~/git_test$ git status
位于分支 master
尚未暫存以備提交的變更:
  (使用 "git add <文件>..." 更新要提交的內(nèi)容)
  (使用 "git checkout -- <文件>..." 丟棄工作區(qū)的改動(dòng))
	修改:     code.txt

修改尚未加入提交(使用 "git add" 和/或 "git commit -a")

最后可以通過上面的丟棄工作區(qū)的改動(dòng),來徹底放棄對(duì)文件的修改。

小結(jié)

當(dāng)你改亂了工作區(qū)某個(gè)文件的內(nèi)容,想直接丟棄工作區(qū)的修改時(shí),用命令git checkout -- file。

當(dāng)你不但改亂了工作區(qū)某個(gè)文件的內(nèi)容,還添加到了暫存區(qū)時(shí),想丟棄修改,分兩步,第一步用命令git reset HEAD file,就回到了上面那種情形,然后按照上面的情形操作已經(jīng)提交了不合適的修改到版本庫時(shí),想要撤銷本次提交,可直接回退版本庫

文件差異對(duì)比

差異對(duì)比常用指令

git diff    #查看工作目錄中當(dāng)前文件和暫存區(qū)域快照之間的差異
git diff --cached    #查看暫存文件與上次提交時(shí)的快照之間的差異,與git diff --staged相同
git diff HEAD    #查看在最后一次提交后所有變更
git diff v1.6 -- filename    #從一個(gè)特定點(diǎn)開始文件的修改情況
git diff v1.6 v1.7 --stat    #兩次提交的差異比對(duì)
git diff master...branchname    #在合并某分支前查看變更內(nèi)容
git diff --name-only v1.6 HEAD    #列出v1.6版本到當(dāng)前最新版本的差異文件,只顯示文件,不顯示差異內(nèi)容

具體操作

對(duì)比當(dāng)前工作區(qū)與HEAD版本的修改差異

# 修改當(dāng)前工作區(qū)的code.txt,內(nèi)容如下:

yanwei@ubuntu:~/git_test$ cat code.txt 
this is the first line
this is the second line
this is the third line
this is the forth line
new line

# 差異對(duì)比

yanwei@ubuntu:~/git_test$ git diff HEAD -- code.txt
diff --git a/code.txt b/code.txt
index 66f9219..9bc8cf3 100644
--- a/code.txt  # ---代表HEAD
+++ b/code.txt  # +++代表當(dāng)前工作區(qū)的文件
@@ -2,3 +2,4 @@ this is the first line
 this is the second line
 this is the third line
 this is the forth line
+new line       # 代表當(dāng)前工作區(qū)比HEAD多一行

對(duì)比兩個(gè)版本之間的差異

# 先查看歷史提交

yanwei@ubuntu:~/git_test$ git reflog
0a96a0f (HEAD -> master) HEAD@{0}: commit: forth commit
e4fb2aa HEAD@{1}: commit: third commit
227ecaa HEAD@{2}: reset: moving to 227ecaa
d66bdc0 HEAD@{3}: reset: moving to HEAD^
227ecaa HEAD@{4}: commit: second commit
d66bdc0 HEAD@{5}: commit (initial): first commit

# 對(duì)比second commit與third commit之間的差異

yanwei@ubuntu:~/git_test$ git diff 227ecaa e4fb2aa -- code.txt
diff --git a/code.txt b/code.txt
index 9899a76..01e1274 100644
--- a/code.txt
+++ b/code.txt
@@ -1,2 +1,3 @@
 this is the first line
 this is the second line
+this is the third line         # third commit比second commit多增加了一行內(nèi)容

刪除文件

簡單說明

要從Git中移除某個(gè)文件,就必須要從已跟蹤文件清單中移除(確切地說,是從暫存區(qū)域移除),然后提交??梢杂?git rm 命令完成此項(xiàng)工作,并連帶從工作目錄中刪除指定的文件。

如果只是簡單地從工作目錄中手工刪除文件,運(yùn)行g(shù)it status時(shí)就會(huì)在"Changed but not updated"部分。

如果刪除之前修改過并且已經(jīng)放到暫存區(qū)域的話,則必須要用強(qiáng)制刪除選項(xiàng) -f(譯注:即 force 的首字母),以防誤刪除文件后丟失修改的內(nèi)容。

如果只想刪除暫存區(qū)中的文件,而不刪除工作目錄中的文件,可以使用git rm --cached

具體操作示例

yanwei@ubuntu:~/git_test$ rm code2.txt 
yanwei@ubuntu:~/git_test$ git status
位于分支 master
尚未暫存以備提交的變更:
  (使用 "git add/rm <文件>..." 更新要提交的內(nèi)容)
  (使用 "git checkout -- <文件>..." 丟棄工作區(qū)的改動(dòng))
	刪除:     code2.txt

修改尚未加入提交(使用 "git add" 和/或 "git commit -a")

yanwei@ubuntu:~/git_test$ git rm code2.txt
rm 'code2.txt'
yanwei@ubuntu:~/git_test$ git status
位于分支 master
要提交的變更:
  (使用 "git reset HEAD <文件>..." 以取消暫存)
	刪除:     code2.txt

移動(dòng)文件

移動(dòng)一個(gè)文件的命令為:git mv

想對(duì)git中的文件改名,可以執(zhí)行

git mv file1 file2

其實(shí)運(yùn)行g(shù)it mv相當(dāng)于運(yùn)行了下面三條命令:

mv readme.txt readme
git rm readme.txt
git add readme

查看提交歷史

git log     #按提交時(shí)間列出所有的更新,最近的更新排在最上面
    -p -2   #-p展開顯示每次提交的內(nèi)容差異,-2則僅顯示最近的兩次更新
    --stat    #僅顯示簡要的增改行數(shù)統(tǒng)計(jì)
    --online    # 一行顯示一個(gè)版本
    --pretty='format'    #定制要顯示的記錄格式
    format的常用選項(xiàng)如下:
        %H 提交對(duì)象(commit)的完整哈希字串
        %h 提交對(duì)象的簡短哈希字串
        %T 樹對(duì)象(tree)的完整哈希字串   
        %t 樹對(duì)象的簡短哈希字串
        %P 父對(duì)象(parent)的完整哈希字串
        %p 父對(duì)象的簡短哈希字串
        %an 作者(author)的名字   
        %ae 作者的電子郵件地址
        %ad 作者修訂日期(可以用 -date= 選項(xiàng)定制格式)
        %ar 作者修訂日期,按多久以前的方式顯示
        %cn 提交者(committer)的名字   
        %ce 提交者的電子郵件地址
        %cd 提交日期
        %cr 提交日期,按多久以前的方式顯示
        %s 提交說明

需要說明的是,作者指的是實(shí)際作出修改的人,提交者為最后一次將些文件提交到的人。

git log --pretty=format:"%H - %an, %ar : %s"
git log --oneline
--shortstat 只顯示 --stat 中最后的行數(shù)修改添加移除統(tǒng)計(jì)。
--name-only 僅在提交信息后顯示已修改的文件清單。
--name-status 顯示新增、修改、刪除的文件清單。
--before="2 weeks ago" --after="2012-10-29" --pretty=oneline    #日期區(qū)間

感謝各位的閱讀,以上就是“入門級(jí)的Git操作方法是什么”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)入門級(jí)的Git操作方法是什么這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

git
AI