Git使用

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
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 使用webstorm中的git 右键菜单 先来最熟知的右键操作,分为两类:文件夹(或者工程)右键和文件右键,先看看...
    略略略_98f4阅读 2,240评论 1 0
  • Git安装 地址:https://git-scm.com/downloads 下载对应平台的git版本安装即可 G...
    foxleezh阅读 4,082评论 0 11
  • 前言 Git简介 实用主义 深入探索 总结 参考资料 Git简介 Git 是一种分布式版本控制系统,它可以不受网络...
    夜沐下的星雨阅读 273评论 0 1
  • 原文地址主要用到的命令: git config user.name 设置用户名 git config user....
    AFinalStone阅读 549评论 0 2
  • 一:Git是什么? Git是目前世界上最先进的分布式版本控制系统。 二:SVN与Git的最主要的区别? SVN...
    JSXL阅读 1,902评论 0 2

友情链接更多精彩内容