Git - 版本庫一般操作(二)

2017-09-01  本文已影响0人  鬼王丶

本篇主要記錄了Git基本的操作。初始版本庫,配置提交人,添加/刪除,提交,查看差異。摘錄於<<Git版本控制管理>>

1.創建初始版本庫

在項目目錄下執行
git init
或從GitHub或GitLab上克隆一個版本庫
git clone ADDRESS
在本例中是從本地GitLab上克隆的。

2.配置提交人

Git支持不同層次的配置文件。按照優先級遞減的順序,如下:

配置一個用戶名和email地址,用於你對所有版本庫的所有提交:

git config --global user.name "HANZO"
git config --global user.email "guixiong97@sina.cn"

移除設置:
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

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

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(-)

6.查看提交 -- git log / git show / git show-branch

[hanzo@hanzo test-git]$ git log
commit 8f60aebed6a0f960e774e8c428696372439410dd #提交的內部識別碼
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命令會產生版本庫裏一系列單獨提交的歷史。條目按照從最新的到最老的順序羅列出來。可在命令後追加git log --color,用以高亮顯示信息。

[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>

產看特定提交的更加詳細信息。如果在執行git show命令沒有顯式指定提交碼,它將顯示最近的一次提交的詳細信息(在這裏爲8f60aebed6a0f960e774e8c428696372439410dd(Update file pom.xml))。

[hanzo@hanzo test-git]$ git show-branch --more=10
[master] Update file pom.xml
[master^] Add file pom.xml
[master~2] Add README

查看當前開發分支簡潔的但行摘要。參數“--more=10”表示額外10個版本,但是這裏僅有3個版本,所以只顯示了3行(默認只顯示最新的提交)。master爲默認分支名。

7.刪除和重命名 -- git rm / git mv

[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表示你想要刪除這個文件的意圖並暫存這個變更,接着git commit在版本庫裏實現這個變更。它會把文件從系統中永久刪除。

[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
在任一情況下,暫存的變更必須隨後進行提交。
$ git commit -m "Moved foo to bar"

8.查看提交差異 -- git diff

以下是三個可供樹或類樹對象使用git diff命令的基本來源:

#修改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>
#當前工作目錄和第一個提交的比較
[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>
#索引和版本庫提交對象的比較
[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>
#第一個和第二個提交的比較
[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輸出信息說明

上一篇 下一篇

猜你喜欢

热点阅读