咱們知道git是分佈式的版本庫,也就是本地倉庫裏面包含了開發的所用內容,每一個人都是本地版本庫的主人,包括歷史記錄、文件內容。即便沒有和遠程代碼庫交換依舊能夠提交內容到本地倉庫,而後git push到遠程倉庫。
可使用git $commit --help查看每一個命令的html幫助文檔,例如git init --helphtml
git init能夠在本地建立一個空的本地倉庫。其經常使用命令行以下,
git init [-q | --quiet] [--bare] [directory]java
當咱們有了本地倉庫之後,須要對這個倉庫配置,須要配置用戶名和用戶的email和其餘的配置。
git config命令提供了三種級別的配置。分別是:git
git config [--add] name value ---添加或者修改配置項,默認的使用範圍爲本地倉庫,可使用--global、--system來指定範圍, 例如 git config user.name fenglxh、git config user.email fenglxh@126.com git config --unset name --取消該配置項,一樣可使用--global、--system來指定範圍,
使用git config [-l|--list]顯示配置windows
git存在大量命令,能夠對咱們常常使用命令,並且命令比較長的命令設置一個別名,也就是一個簡寫。
別名的配置也須要使用config命令,好比給 git status 設置別名 st:緩存
git config alias.st status -----以alias.開頭 git config --global alias.lg "log --color --graph --oneline --abbrev-commit"
這樣咱們之後使用的時候,直接用 git st 就能夠作 git status 的事了。bash
使用版本管理最經常使用的操做就是提交代碼,不過對git來講,若是咱們修改了文件內容提交的話必須先使用git add命令,而後才能使用git commit命令提交到本地倉庫。服務器
git add命令是把修改提交到暫存區中。dom
git add -A -----------懶人模式,把工做目錄下全部的更改提交到,包括刪除、添加、修改文件 git add gameoflife-acceptance-tests/\*.java -----------------------------把某個目錄下的全部java後綴的文件提交 git add *.java ------------------------------提交全部的java後綴的文件
git rm命令是把暫存區中的添加刪除,命令基本和git add相反,都是修改的暫存區分佈式
git rm --cached hello-word/README ---------------把 hello-word/README從暫存區移除 git rm -f hello-word/README ---------------把hello-word/README從暫存區移除,同時刪除工做目錄下的該文件 git rm --cached Documentation/*.txt ---------------把Documentation下的全部的txt文件從暫存區移除
git commit命令是提交暫存區中的修改。ide
git commit -m "commit message" --------------帶有提交註釋的提交 git commit --allow-empty -m "This is a empty commit" ----------當暫存區沒有變化的時候,是提交失敗的,能夠加上 --allow-empty運行空提交,此時這兩個提交的tree對象指向同一個。
當咱們修改了工做區的內容,可是還不能提交,此時須要更新代碼的時候,能夠把本地的修改存儲,使用git stash命令。這樣就會把工做區的修改(不包括新增)保存,並把工做區的內容切換到HEAD指向的提交中,這樣又是一個乾淨的工做區了。
git stash ---------------存儲 git stash pop -------------彈出存儲 git stash list --------顯示全部的存儲
實現原理:
當咱們使用git stash命令的時候,會生成.git/refs/stash文件,其內容爲stash的 sha1信息。能夠經過git cat-file查看這個SHA1的信息,會發現這個sha1是以當前的SHA1和工做區提交(建立的一個提交對象)爲父提交。
注:SHA-Stash爲.git/refs/stash文件中保存的sha1。sha-temp爲工做區的提交
當咱們屢次運行git stash的時候,.git/refs/stash文件中的sha永遠執行最近執行的stash對應的sha。在.git\logs\refs/stash文件中按順序保存全部的stash命令對應的sha
顯示工做目錄下的狀態。
當咱們不存在工做目錄修改的時候執行輸出以下信息: $ git status On branch master Your branch is up-to-date with 'origin/master'. nothing to commit, working tree clean 隨便對其中的某個文件修改,可是不提交暫存區: $ 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") 此時咱們把修改提交到緩存區,再查看狀態,會發現暫存區發生了變化。 $ 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 此時咱們再修改該文件,可是不執行git 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 會發現提示pom.xml文件修改了出現兩個地方,一個是暫存區的修改,一個是工做目錄的修改。並且其提示顏色並不相同
咱們可使用git status -s命令查看統計的信息(工做區的修改使用紅色字體,綠×××字體是暫存區的修改)。
git diff命令顯示工做區、提交、暫存區的差別
$ git diff ------顯示工做空間和暫存區的差別 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 ------------------------提交到暫存區 $git diff --cached ----------------------比較暫存區和提交的差別
git log命令能夠查看提交的信息。
git log -1 ----------------能夠查看最近的一次提交,-N表示最近的N次提交 git log --oneline -N --------------提交以一行信息顯示,等於--pretty=oneline git log --graph -----------以圖形的方式展現提交樹 git log --stat -----------------展現提交的彙總信息
git log的精細化輸出,--pretty選項。使用--pretty選項能夠精細化輸出commit的全部信息。
git log --oneline --------------等價於git log --pretty=oneline輸出內容爲<sha1> <title line> git log --pretty=short ------------輸出內容爲<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命令能夠根據模式按行查找內容。支持的pattern類型以下:--basic-regexp, --extended-regexp, --fixed-strings, or --perl-regexp。默認爲basic-regexp
git grep 'time_t' -- '*.[ch]' git grep -E 'public (class|interface) \w+[0-9]+' --------查找全部的java的類名包含數字的類 git grep -F 'fixed string'
git blame能夠查找文件每一行的提交信息,追溯文件的內容。git blame xxx.txt