本篇主要記錄了Git基本的操作。初始版本庫,配置提交人,添加/刪除,提交,查看差異。摘錄於<<Git版本控制管理>>
1.創(chuàng)建初始版本庫
在項(xiàng)目目錄下執(zhí)行
git init
或從GitHub或GitLab上克隆一個(gè)版本庫
git clone ADDRESS
在本例中是從本地GitLab上克隆的。
2.配置提交人
Git支持不同層次的配置文件。按照優(yōu)先級遞減的順序,如下:
- .git/config
版本庫特定的配置文件,可用--file選項(xiàng)修改,爲(wèi)默認(rèn)選項(xiàng)。擁有最高優(yōu)先級。 - ~/.gitconfig
用戶特定的配置文件,可用--global選項(xiàng)修改。 - /etc/gitconfig
系統(tǒng)範(fàn)圍內(nèi)的配置設(shè)置,想用--system選項(xiàng)修改,需要有文件寫權(quán)限。
配置一個(gè)用戶名和email地址,用於你對所有版本庫的所有提交:
git config --global user.name "HANZO"
git config --global user.email "guixiong97@sina.cn"
移除設(shè)置:
git config --unset --global user.email
查看配置信息:
[hanzo@hanzo test-git]$ cat .git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote "origin"]
url = git@gitlab.hanzo.net:hanzo/test-git.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
[hanzo@hanzo test-git]$ git config -l
user.name=HANZO
user.email=guixiong97@sina.cn
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=git@gitlab.hanzo.net:hanzo/test-git.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master
3.將文件添加索引(index) -- git add
[hanzo@hanzo test-git]$ echo "<project></proect>" > pom.xml
[hanzo@hanzo test-git]$ git add pom.xml
[hanzo@hanzo test-git]$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: pom.xml
- git add pom.xml 將文件添加到索引中,用來暫存(stage)。
- git status 顯示中間狀態(tài)的pom.xml。下一次提交的時(shí)候?qū)om.xml添加到版本庫中。
4.提交(commit) -- git commit
[hanzo@hanzo test-git]$ git commit -m "Add file pom.xml"
[master 28fe320] Add file pom.xml
1 file changed, 1 insertion(+)
create mode 100644 pom.xml
- 一條完全限定的git commit命令必須提供日誌消息和提交人。如果沒有通過git config命令配置提交人,可以在之上命令追加
--author="HANZO <guixiong97@sina.cn>"。
如果既沒有配置提交人,也沒有指定,那麼可能會(huì)遇到一些奇怪的警告。
如果提交的時(shí)候沒有提供日誌消息,那麼將會(huì)在交互式編輯器會(huì)話期間創(chuàng)建消息,比如在vim中輸入日誌消息。編輯器可以通過設(shè)置GIT_EDITOR環(huán)境變量指定。 - 一個(gè)乾淨(jìng)(clean)的工作目錄,意味著工作目錄裏不包含任何於版本庫中不同的未知或這更改過的文件。
5.再次提交 -- git commit FILE
#修改pom.xml文件
[hanzo@hanzo test-git]$ git commit pom.xml
[master 8f60aeb] Update file pom.xml
1 file changed, 7 insertions(+), 1 deletion(-)
- 由於pom.xml文件已經(jīng)添加到版本庫中了,沒有必要再把這個(gè)文件告訴索引,即不需要在commit之前執(zhí)行
git add pom.xml。所以,新創(chuàng)建的不在索引中的文件必須先執(zhí)行git add FILE - 當(dāng)在命令行中提交一個(gè)指定名稱的文件時(shí),文件的變更會(huì)自動(dòng)捕捉。
6.查看提交 -- git log / git show / git show-branch
- git log
[hanzo@hanzo test-git]$ git log
commit 8f60aebed6a0f960e774e8c428696372439410dd #提交的內(nèi)部識(shí)別碼
Author: HANZO <guixiong97@sina.cn>
Date: Fri Sep 1 15:23:32 2017 +0800
Update file pom.xml
commit 28fe32043b5398701dfc5a40848b179522afcd44
Author: HANZO <guixiong97@sina.cn>
Date: Fri Sep 1 15:10:51 2017 +0800
Add file pom.xml
......
git log命令會(huì)產(chǎn)生版本庫裏一系列單獨(dú)提交的歷史。條目按照從最新的到最老的順序羅列出來。可在命令後追加git log --color,用以高亮顯示信息。
- git show
[hanzo@hanzo test-git]$ git show 28fe32043b5398701dfc5a40848b179522afcd44
commit 28fe32043b5398701dfc5a40848b179522afcd44
Author: HANZO <guixiong97@sina.cn>
Date: Fri Sep 1 15:10:51 2017 +0800
Add file pom.xml
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..aa33b42
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1 @@
+<project></proect>
產(chǎn)看特定提交的更加詳細(xì)信息。如果在執(zhí)行git show命令沒有顯式指定提交碼,它將顯示最近的一次提交的詳細(xì)信息(在這裏爲(wèi)8f60aebed6a0f960e774e8c428696372439410dd(Update file pom.xml))。
- git show-branch
[hanzo@hanzo test-git]$ git show-branch --more=10
[master] Update file pom.xml
[master^] Add file pom.xml
[master~2] Add README
查看當(dāng)前開發(fā)分支簡潔的但行摘要。參數(shù)“--more=10”表示額外10個(gè)版本,但是這裏僅有3個(gè)版本,所以只顯示了3行(默認(rèn)只顯示最新的提交)。master爲(wèi)默認(rèn)分支名。
7.刪除和重命名 -- git rm / git mv
- git rm
從版本庫中刪除一個(gè)文件於添加一個(gè)文件是類似的,除了使用的命令是git rm。
假設(shè)現(xiàn)在你要?jiǎng)h除一個(gè)已經(jīng)提交到版本庫中的文件:intr.txt,可以如下操作。
[hanzo@hanzo test-git]$ ls
intr.txt pom.xml README.md
[hanzo@hanzo test-git]$ git rm intr.txt
rm 'intr.txt'
[hanzo@hanzo test-git]$ git commit -m "Remove a intr.txt"
[master 4895651] Remove a intr.txt
1 file changed, 1 deletion(-)
delete mode 100644 intr.txt
git rm表示你想要?jiǎng)h除這個(gè)文件的意圖並暫存這個(gè)變更,接著git commit在版本庫裏實(shí)現(xiàn)這個(gè)變更。它會(huì)把文件從系統(tǒng)中永久刪除。
- git mv
通過git rm和git add組合命令來間接爲(wèi)一個(gè)文件衝命名。
[hanzo@hanzo test-git]$ ls
foo.html pom.xml README.md
[hanzo@hanzo test-git]$ mv foo.html bar.html
[hanzo@hanzo test-git]$ git rm foo.html
rm 'foo.html'
[hanzo@hanzo test-git]$ git add bar.html
[hanzo@hanzo test-git]$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 5 commits.
# (use "git push" to publish your local commits)
#
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# renamed: foo.html -> bar.html
之上是使用git mv的相同操作:
$ git mv foo.html bar.html
在任一情況下,暫存的變更必須隨後進(jìn)行提交。
$ git commit -m "Moved foo to bar"
8.查看提交差異 -- git diff
以下是三個(gè)可供樹或類樹對象使用git diff命令的基本來源:
- 整個(gè)提交圖中的任意樹對象;
- 工作目錄;
- 索引
通常,git diff命令進(jìn)行樹比較時(shí)可以通過提交名,分支名或者標(biāo)籤名。工作目錄的文件和目錄結(jié)構(gòu)還有在索引中暫存文件的完整結(jié)構(gòu),都可以被看作樹。
基於上述三種來源的組合,可以進(jìn)行如下4種基本比較。 - git diff
命令後面沒有任何參數(shù),它會(huì)顯示工作目錄和索引之間的差異。它不會(huì)顯示索引中的和永久版本庫中的文件的不同。
#修改pom.xml文件,增加了以行‘<name>TESTPROJECT</name>’
[hanzo@hanzo test-git]$ git diff
diff --git a/pom.xml b/pom.xml
index 7b3aa60..aaa1cae 100644
--- a/pom.xml
+++ b/pom.xml
@@ -4,4 +4,5 @@
<artifactId>AID</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
+ <name>TESTPROJECT</name>
</proect>
- git diff COMMIT
這個(gè)命令形式會(huì)顯示工作目錄和給定提交間的差異。常見的用法是用HEAD或者一個(gè)特定的分支名。HEAD始終指向當(dāng)前分支的最近提交。
#當(dāng)前工作目錄和第一個(gè)提交的比較
[hanzo@hanzo test-git]$ git diff 149feef576a8a9875cd13813048c686b525767ec
diff --git a/bar.html b/bar.html
new file mode 100644
index 0000000..257cc56
--- /dev/null
+++ b/bar.html
@@ -0,0 +1 @@
+foo
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..aaa1cae
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,8 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocat
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>GIT</groupId>
+ <artifactId>AID</artifactId>
+ <packaging>war</packaging>
+ <version>1.0-SNAPSHOT</version>
+ <name>TESTPROJECT</name>
+</proect>
- git diff --cached COMMIT
這個(gè)命令形式會(huì)顯示索引中的變更和給定提交中的變更之間的差異。如果省略COMMIT,則默認(rèn)爲(wèi)HEAD。--cached可以用--staged代替。
#索引和版本庫提交對象的比較
[hanzo@hanzo test-git]$ git diff --cached #==>git diff --cached HEAD
[hanzo@hanzo test-git]$ git add pom.xml #將變更添加到緩存中
[hanzo@hanzo test-git]$ git diff --cached
diff --git a/pom.xml b/pom.xml
index 7b3aa60..aaa1cae 100644
--- a/pom.xml
+++ b/pom.xml
@@ -4,4 +4,5 @@
<artifactId>AID</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
+ <name>TESTPROJECT</name>
</proect>
- git diff COMMIT1 COMMIT2
這個(gè)命令顯示兩個(gè)提交之間的差異。它會(huì)忽略索引和工作目錄。
#第一個(gè)和第二個(gè)提交的比較
[hanzo@hanzo test-git]$ git diff 149feef576a8a9875cd13813048c686b525767ec 28fe32043b5398701dfc5a40848b179522afcd44
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..aa33b42
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1 @@
+<project></proect>
git diff輸出信息說明
- --- 標(biāo)記原始文件;
- +++ 標(biāo)記新文件
- @@之間表示兩個(gè)不同文件版本的上下文行號(hào)。以-開始的行表示從源文件刪除該行以得到新文件。以+開始的行表示在原文件中添加該行以產(chǎn)生新文件。