您好,登錄后才能下訂單哦!
我們知道git是分布式的版本庫,也就是本地倉庫里面包含了開發(fā)的所用內(nèi)容,每個人都是本地版本庫的主人,包括歷史記錄、文件內(nèi)容。即使沒有和遠(yuǎn)程代碼庫交換依舊可以提交內(nèi)容到本地倉庫,然后git push到遠(yuǎn)程倉庫。
可以使用git $commit --help查看每個命令的html幫助文檔,例如git init --help
git init可以在本地創(chuàng)建一個空的本地倉庫。其常用命令行如下,
git init [-q | --quiet] [--bare] [directory]
當(dāng)我們有了本地倉庫以后,需要對這個倉庫配置,需要配置用戶名和用戶的email和其他的配置。
git config命令提供了三種級別的配置。分別是:
git config [--add] name value ---添加或者修改配置項,默認(rèn)的使用范圍為本地倉庫,可以使用--global、--system來指定范圍,
例如 git config user.name fenglxh、git config user.email fenglxh@126.com
git config --unset name --取消該配置項,同樣可以使用--global、--system來指定范圍,
使用git config [-l|--list]顯示配置
git存在大量命令,可以對我們經(jīng)常使用命令,而且命令比較長的命令設(shè)置一個別名,也就是一個簡寫。
別名的配置也需要使用config命令,比如給 git status 設(shè)置別名 st:
git config alias.st status -----以alias.開頭
git config --global alias.lg "log --color --graph --oneline --abbrev-commit"
這樣我們以后使用的時候,直接用 git st 就可以做 git status 的事了。
使用版本管理最常用的操作就是提交代碼,不過對git來說,如果我們修改了文件內(nèi)容提交的話必須先使用git add命令,然后才能使用git commit命令提交到本地倉庫。
git add命令是把修改提交到暫存區(qū)中。
git add -A -----------懶人模式,把工作目錄下所有的更改提交到,包括刪除、添加、修改文件
git add gameoflife-acceptance-tests/\*.java -----------------------------把某個目錄下的所有java后綴的文件提交
git add *.java ------------------------------提交所有的java后綴的文件
git rm命令是把暫存區(qū)中的添加刪除,命令基本和git add相反,都是修改的暫存區(qū)
git rm --cached hello-word/README ---------------把 hello-word/README從暫存區(qū)移除
git rm -f hello-word/README ---------------把hello-word/README從暫存區(qū)移除,同時刪除工作目錄下的該文件
git rm --cached Documentation/*.txt ---------------把Documentation下的所有的txt文件從暫存區(qū)移除
git commit命令是提交暫存區(qū)中的修改。
git commit -m "commit message" --------------帶有提交注釋的提交
git commit --allow-empty -m "This is a empty commit" ----------當(dāng)暫存區(qū)沒有變化的時候,是提交失敗的,可以加上 --allow-empty運行空提交,此時這兩個提交的tree對象指向同一個。
當(dāng)我們修改了工作區(qū)的內(nèi)容,但是還不能提交,此時需要更新代碼的時候,可以把本地的修改存儲,使用git stash命令。這樣就會把工作區(qū)的修改(不包括新增)保存,并把工作區(qū)的內(nèi)容切換到HEAD指向的提交中,這樣又是一個干凈的工作區(qū)了。
git stash ---------------存儲
git stash pop -------------彈出存儲
git stash list --------顯示所有的存儲
實現(xiàn)原理:
當(dāng)我們使用git stash命令的時候,會生成.git/refs/stash文件,其內(nèi)容為stash的 sha1信息??梢酝ㄟ^git cat-file查看這個SHA1的信息,會發(fā)現(xiàn)這個sha1是以當(dāng)前的SHA1和工作區(qū)提交(創(chuàng)建的一個提交對象)為父提交。
注:SHA-Stash為.git/refs/stash文件中保存的sha1。sha-temp為工作區(qū)的提交
當(dāng)我們多次運行g(shù)it stash的時候,.git/refs/stash文件中的sha永遠(yuǎn)執(zhí)行最近執(zhí)行的stash對應(yīng)的sha。在.git\logs\refs/stash文件中按順序保存所有的stash命令對應(yīng)的sha
顯示工作目錄下的狀態(tài)。
當(dāng)我們不存在工作目錄修改的時候執(zhí)行輸出如下信息:
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working tree clean
隨便對其中的某個文件修改,但是不提交暫存區(qū):
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: pom.xml
no changes added to commit (use "git add" and/or "git commit -a")
此時我們把修改提交到緩存區(qū),再查看狀態(tài),會發(fā)現(xiàn)暫存區(qū)發(fā)生了變化。
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: pom.xml
此時我們再修改該文件,但是不執(zhí)行g(shù)it add命令。
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: pom.xml
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: pom.xml
會發(fā)現(xiàn)提示pom.xml文件修改了出現(xiàn)兩個地方,一個是暫存區(qū)的修改,一個是工作目錄的修改。而且其提示顏色并不相同
我們可以使用git status -s命令查看統(tǒng)計的信息(工作區(qū)的修改使用紅色字體,綠×××字體是暫存區(qū)的修改)。
git diff命令顯示工作區(qū)、提交、暫存區(qū)的差異
$ git diff ------顯示工作空間和暫存區(qū)的差異
diff --git a/pom.xml b/pom.xml
index 0ec4374..3f64500 100644
--- a/pom.xml
+++ b/pom.xml
@@ -10,7 +10,7 @@ ----文件的第十行開始的起航
<properties>
<build.number>SNAPSHOT</build.number>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <easyb.version>1.4</easyb.version>
+ <easyb.version>1.5</easyb.version>
<cobertura.version>2.6</cobertura.version>
<!-- A workaround for a bug in PMD -->
<sourceJdk>1.7</sourceJdk>
@@ -178,6 +178,12 @@
<version>${easyb.version}</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>dom4j</groupId>
+ <artifactId>dom4j</artifactId>
+ <version>1.6.2</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</dependencyManagement>
<dependencies>
$git add pom.xml ------------------------提交到暫存區(qū)
$git diff --cached ----------------------比較暫存區(qū)和提交的差異
git log命令可以查看提交的信息。
git log -1 ----------------可以查看最近的一次提交,-N表示最近的N次提交
git log --oneline -N --------------提交以一行信息顯示,等于--pretty=oneline
git log --graph -----------以圖形的方式展示提交樹
git log --stat -----------------展示提交的匯總信息
git log的精細(xì)化輸出,--pretty選項。使用--pretty選項可以精細(xì)化輸出commit的所有信息。
git log --oneline --------------等價于git log --pretty=oneline輸出內(nèi)容為<sha1> <title line>
git log --pretty=short ------------輸出內(nèi)容為<sha1>
<author>
<title line>
git log --pretty=medium/full/fuller/email
git log --pretty=raw 暫時提交的樹、父提交等比較全的信息
git --pretty=format:<string> 其中string是可以格式化的,支持占位符。常用的占位符如下:
git log --pretty=format:"The author of %h was %an, %ar%nThe title was >>%s<<%n" -------輸出大概如下:
The author of fe6e0ee was Junio C Hamano, 23 hours ago
The title was >>t4119: test autocomputing -p<n> for traditional diff input.<<
git grep命令可以根據(jù)模式按行查找內(nèi)容。支持的pattern類型如下:--basic-regexp, --extended-regexp, --fixed-strings, or --perl-regexp。默認(rèn)為basic-regexp
git grep 'time_t' -- '*.[ch]'
git grep -E 'public (class|interface) \w+[0-9]+' --------查找所有的java的類名包含數(shù)字的類
git grep -F 'fixed string'
git blame可以查找文件每一行的提交信息,追溯文件的內(nèi)容。
git blame xxx.txt
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。