天天看點

Git 一些細節

一、對于任何一個檔案,在Git 内都隻有三

種狀态:已送出(committed),已修改(modified)和已暫存(staged)。已送出表示該

檔案已經被安全地儲存在本地資料庫中了;已修改表示修改了某個檔案,但還沒有送出保

存;已暫存表示把已修改的檔案放在下次送出時要儲存的清單中。

二、Git工作變量的設定

1、/etc/gitconfig 檔案:系統中對所有使用者都普遍适用的配置。若使用git config 時

用--system 選項,讀寫的就是這個檔案。

2、~/.gitconfig 檔案:使用者目錄下的配置檔案隻适用于該使用者。若使用git config 時

用--global 選項,讀寫的就是這個檔案。

3、目前項目的git 目錄中的配置檔案(也就是工作目錄中的.git/config 檔案):這

裡的配置僅僅針對目前項目有效。每一個級别的配置都會覆寫上層的相同配置,是以

.git/config 裡的配置會覆寫/etc/gitconfig 中的同名變量。

1

2

3

4

5

6

7

<code>#使用者資訊</code>

<code>git config --global user.name </code><code>"John Doe"</code>

<code>git config --global user.email [email protected]</code>

<code>#預設使用的文本編輯器</code>

<code>git config --global core.editor vim</code>

<code>#差異分析工具</code>

<code>git config --global merge.tool vimdiff</code>

三、送出,隻有在<code>Changes to be committed下的檔案才會被送出。</code>

8

9

10

11

12

13

14

<code>git status</code>

<code># On branch master</code>

<code># Your branch is ahead of 'origin/master' by 3 commits.</code>

<code>#</code>

<code># Changes to be committed:</code>

<code>#   (use "git reset HEAD &lt;file&gt;..." to unstage)</code>

<code>#   modified:   a.txt</code>

<code># Changes not staged for commit:</code>

<code>#   (use "git add &lt;file&gt;..." to update what will be committed)</code>

<code>#   (use "git checkout -- &lt;file&gt;..." to discard changes in working directory)</code>

跳過暫存區使用:git commit -a -m 'reason...'

四、git diff

git diff:此指令比較的是工作目錄中目前檔案和暫存區域快照之間的差異,也就是修改之後還沒有

暫存起來的變化内容。

若要看已經暫存起來的檔案和上次送出時的快照之間的差異,可以用git diff --cached

五、移除檔案

git rm :删除檔案,同時删除跟蹤

git rm --cached filename :删除跟蹤,但保留本地檔案。

六、察看送出記錄

git log -p:察看細節差異

git log -2:記錄條數

git log --stat:顯示簡要的增改行數統計

<code>選項說明</code>

<code>-(n) 僅顯示最近的n 條送出</code>

<code>--since, --after 僅顯示指定時間之後的送出。</code>

<code>--</code><code>until</code><code>, --before 僅顯示指定時間之前的送出。</code>

<code>--author 僅顯示指定作者相關的送出。</code>

<code>--committer 僅顯示指定送出者相關的送出。</code>

git log --pretty=format:"%h - %an, %ar : %s" 設定展示格式

15

<code>%H 送出對象(commit)的完整哈希字串</code>

<code>%h 送出對象的簡短哈希字串</code>

<code>%T 樹對象(tree)的完整哈希字串</code>

<code>%t 樹對象的簡短哈希字串</code>

<code>%P 父對象(parent)的完整哈希字串</code>

<code>%p 父對象的簡短哈希字串</code>

<code>%an 作者(author)的名字</code>

<code>%ae 作者的電子郵件位址</code>

<code>%ad 作者修訂日期(可以用-</code><code>date</code><code>= 選項定制格式)</code>

<code>%ar 作者修訂日期,按多久以前的方式顯示</code>

<code>%cn 送出者(committer)的名字</code>

<code>%ce 送出者的電子郵件位址</code>

<code>%</code><code>cd</code> <code>送出日期</code>

<code>%cr 送出日期,按多久以前的方式顯示</code>

<code>%s 送出說明</code>

七、復原

1、git reset HEAD filename : 撤銷暫存區的檔案

2、git checkout -- filename : 撤銷本地修改

八、遠端倉庫

察看有哪些遠端倉庫: git remote -v

添加遠端倉庫:git remote add pb git://github.com/paulboone/ticgit.git

從遠端倉庫抓取資料:git fetch pb

如果設定了某個分支用于跟蹤某個遠端倉庫的分支,可以使

用git pull 指令自動抓取資料下來,然後将遠端分支自動合并到本地倉庫中目前分支.

推送資料到遠端倉庫:git push origin master

檢視遠端倉庫資訊 

16

17

18

19

20

21

22

23

24

25

26

<code>$ git remote show origin</code>

<code>* remote origin</code>

<code>URL: [email protected]:defunkt</code><code>/github</code><code>.git</code>

<code>#運作git pull 時将自動合并哪些分支</code>

<code>Remote branch merged with </code><code>'git pull'</code> <code>while</code> <code>on branch issues</code>

<code>issues</code>

<code>Remote branch merged with </code><code>'git pull'</code> <code>while</code> <code>on branch master</code>

<code>master</code>

<code>#有哪些遠端分支還沒有同步到本地</code>

<code>New remote branches (next fetch will store </code><code>in</code> <code>remotes</code><code>/origin</code><code>)</code>

<code>caching</code>

<code>#已同步到本地的遠端分支在遠端伺服器上已被删除</code>

<code>Stale tracking branches (use </code><code>'git remote prune'</code><code>)</code>

<code>libwalker</code>

<code>walker2</code>

<code>Tracked remote branches</code>

<code>acl</code>

<code>apiv2</code>

<code>dashboard2</code>

<code>postgres</code>

<code>#運作git push 時預設推送的分支是什麼</code>

<code>Local branch pushed with </code><code>'git push'</code>

<code>master:master</code>

九、打标簽 輕量級的(lightweight)和含附注的(annotated)

git tag

git tag -l 'v1.4.2.*' 列出特定标簽

含附注的标簽 git tag -a v0.8.0 -m 'my tag v0.8.0'

察看标簽:git show v0.8.0

補打标簽:git tag -a v1.2 9fceb02(校驗數)

分享标簽:git push origin --tags/v0.8.0

十、使用技巧

1、自動完成

下載下傳Git 的源

代碼,進入contrib/completion 目錄,會看到一個git-completion.bash 檔案。将此

檔案複制到你自己的使用者主目錄中(譯注:按照下面的示例,還應改名加上點:cp gitcompletion.

bash ~/.git-completion.bash),并把下面一行内容添加到你的.bashrc 文

件中:

source ~/.git-completion.bash

在輸入Git 指令的時候可以敲兩次跳格鍵(Tab),就會看到列出所有比對的可用指令建

議:

$ git co&lt;tab&gt;&lt;tab&gt;

commit config

2、Git 指令别名

<code>$ git config --global </code><code>alias</code><code>.co checkout</code>

<code>$ git config --global </code><code>alias</code><code>.br branch</code>

<code>$ git config --global </code><code>alias</code><code>.ci commit</code>

<code>$ git config --global </code><code>alias</code><code>.st status</code>

<code></code>

本文轉自shayang8851CTO部落格,原文連結:http://blog.51cto.com/janephp/1301099,如需轉載請自行聯系原作者