1 查看信息
查看最近或某一次提交修改的文件列表相关命令整理。
每次修改的文件列表, 显示状态
git log --name-status
每次修改的文件列表
git log --name-only
每次修改的文件列表, 及文件修改的统计
git log --stat
每次修改的文件列表
git whatchanged
每次修改的文件列表, 及文件修改的统计
git whatchanged --stat
显示最后一次的文件改变的具体内容
git show
显示最后 5 次的文件改变的具体内容
git show -5
显示某个 commitid 改变的具体内容
git show commitid
比较两个版本之间的文件修改内容
git diff commit1 commit2
比较两个版本之间的DIR目录下文件修改列表,不显示状态
git diff commit1 commit2 --name-only [DIR]
比较两个版本之间的DIR目录下文件修改列表,显示状态
git diff commit1 commit2 --name-status [DIR]
让单个文件回退到指定的版本
git rest commit1 xxx.cpp
git checkout xxx.cpp
git commit
2 交互式提交
工作中经常遇到调试一个bug时加了好多调试信息,但是提交修改只想提交文件的关键部分保留调试信息的情况,没想到强大的git已经提供了这种功能。
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,?]?
这时有很多选项。 输入 ? 显示所有可以使用的命令列表:
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
- 检查能否应用成功:
git apply --check newpatch.patch
- 打补丁:
git apply test.diff
git am newpatch.patch
4 git clone
例如访问高通私有库网络较慢,下载需较长时间;局域网其他PC已经从远处服务器下载一份代码,直接从局域网其他PC拉取,节省时间;
示例:
本地PC从rmt机器上拉取repo仓库;(rmt@192.168.110.100:/media/hdd_p/8350_r30/temp/repo 仓库是从高通网站clone的项目;)
- 方式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时写错,修改url地址
git fetch
git check master
等价于:
git clone rmt@192.168.110.100:/media/hdd_p/8350_r30/temp/repo
- 方式2
#局域网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机器恢复原来url:
git remote set-url origin https://source.codeaurora.org/tools/repo.git
- 如何将本地仓库设置为远程仓库
#创建仓库
mkdir git_test
cd git_test
git init
echo 123 > test.txt
git add test.txt
git commit -m "temp commit"
#配置仓库为远程仓库
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