1 查看信息
查看最近或某一次提交修改的文件列表相關(guān)命令整理。
每次修改的文件列表, 顯示狀態(tài)
git log --name-status
每次修改的文件列表
git log --name-only
每次修改的文件列表, 及文件修改的統(tǒng)計(jì)
git log --stat
每次修改的文件列表
git whatchanged
每次修改的文件列表, 及文件修改的統(tǒng)計(jì)
git whatchanged --stat
顯示最后一次的文件改變的具體內(nèi)容
git show
顯示最后 5 次的文件改變的具體內(nèi)容
git show -5
顯示某個(gè) commitid 改變的具體內(nèi)容
git show commitid
比較兩個(gè)版本之間的文件修改內(nèi)容
git diff commit1 commit2
比較兩個(gè)版本之間的DIR目錄下文件修改列表,不顯示狀態(tài)
git diff commit1 commit2 --name-only [DIR]
比較兩個(gè)版本之間的DIR目錄下文件修改列表,顯示狀態(tài)
git diff commit1 commit2 --name-status [DIR]
讓單個(gè)文件回退到指定的版本
git rest commit1 xxx.cpp
git checkout xxx.cpp
git commit
2 交互式提交
工作中經(jīng)常遇到調(diào)試一個(gè)bug時(shí)加了好多調(diào)試信息,但是提交修改只想提交文件的關(guān)鍵部分保留調(diào)試信息的情況,沒想到強(qiáng)大的git已經(jīng)提供了這種功能。
git 可以暫存文件的特定部分,例如,在samplegit.rb文件中做了3處修改,但只想暫存其中一處,可用git add -p(git add --patch)命令,交互式暫存此文件的特定部分。
$ git add -p lib/simplegit.rb
diff --git a/lib/simplegit.rb b/lib/simplegit.rb
index dd5ecc4..57399e0 100644
--- a/lib/simplegit.rb
+++ b/lib/simplegit.rb
@@ -22,7 +22,7 @@ class SimpleGit
end
def log(treeish = 'master')
- command("git log -n 25 #{treeish}")
+ command("git log -n 30 #{treeish}")
end
def blame(path)
(1/3) Stage this hunk [y,n,a,d,/,j,J,g,e,?]?
這時(shí)有很多選項(xiàng)。 輸入 ? 顯示所有可以使用的命令列表:
Stage this hunk [y,n,a,d,/,j,J,g,e,?]? ?
y - stage this hunk
n - do not stage this hunk
a - stage this and all the remaining hunks in the file
d - do not stage this hunk nor any of the remaining hunks in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
k - leave this hunk undecided, see previous undecided hunk
K - leave this hunk undecided, see previous hunk
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print help
通常,輸入y暫存此部分,或者輸入n跳過此部分就可以了。
3 patch操作
- 制作diff
git diff [--cache] > test.diff
git diff <commit1> <commit2> > test.diff - 制作pacth
git format-patch -1
git format-patch -<N> - 檢查patch文件:
git apply --stat newpatch.patch - 檢查能否應(yīng)用成功:
git apply --check newpatch.patch - 打補(bǔ)?。?br>
git apply test.diff
git am newpatch.patch
4 git clone
例如訪問高通私有庫網(wǎng)絡(luò)較慢,下載需較長時(shí)間;局域網(wǎng)其他PC已經(jīng)從遠(yuǎn)處服務(wù)器下載一份代碼,直接從局域網(wǎng)其他PC拉取,節(jié)省時(shí)間;
示例:
本地PC從rmt機(jī)器上拉取repo倉庫;(rmt@192.168.110.100:/media/hdd_p/8350_r30/temp/repo 倉庫是從高通網(wǎng)站clone的項(xiàng)目;)
- 方式1
mkdir project
cd project
git init
git remote add origin rmt@192.168.110.100:/media/hdd_p/8350_r30/temp/repo
#git remote set-url origin rmt@192.168.110.100:/media/hdd_p/8350_r30/temp/repo //若add時(shí)寫錯(cuò),修改url地址
git fetch
git check master
等價(jià)于:
git clone rmt@192.168.110.100:/media/hdd_p/8350_r30/temp/repo
- 方式2
#局域網(wǎng)PC(rmt):
cd /media/hdd_p/8350_r30/temp/repo
git remote -v
origin https://source.codeaurora.org/tools/repo.git (fetch)
origin https://source.codeaurora.org/tools/repo.git (push)
#更改url
git remote set-url origin ssh://rmt@192.168.110.100:/~/hdd_p/8350_r30/temp/repo/.git
git remote -v
origin ssh://rmt@192.168.110.100:/~/hdd_p/8350_r30/temp/repo/.git (fetch)
origin ssh://rmt@192.168.110.100:/~/hdd_p/8350_r30/temp/repo/.git (push)
#本地PC下載rmt的repo倉庫:
git clone ssh://rmt@192.168.110.100:/~/hdd_p/8350_r30/temp/repo/.git
#git remote set-url origin https://source.codeaurora.org/tools/repo.git
#克隆完成后,rmt機(jī)器恢復(fù)原來url:
git remote set-url origin https://source.codeaurora.org/tools/repo.git
- 如何將本地倉庫設(shè)置為遠(yuǎn)程倉庫
#創(chuàng)建倉庫
mkdir git_test
cd git_test
git init
echo 123 > test.txt
git add test.txt
git commit -m "temp commit"
#配置倉庫為遠(yuǎn)程倉庫
git remote add origin ssh://rmt@192.168.110.100:/~/hdd_p/8350_r30/temp/git_test/.git
#其他PC clone git_test倉庫:
git clone ssh://rmt@192.168.110.100:/~/hdd_p/8350_r30/temp/git_test/.git