这篇博客总结的也不错:

8 Tips to help you work better with Git

git常用及进阶命令总结

Git与Repo入门

git-cheat-sheet.pdf

Git tips and tricks

一、初始環境配置

  1. git config --global user.name "John Doe"
  2. git config --global user.email johndoe@example.com
  3.  
  4. git config --global core.editor vim
  5. git config --global color.ui true
  6.  
  7. git config --global alias.co checkout
  8. git config --global alias.br branch
  9. git config --global alias.ci commit
  10. git config --global alias.st status
  11.  
  12. #Delete local branches that have been removed from remote on fetch/pull
  13.  
  14. git config --global fetch.prune true
  15.  
  16. git config --global alias.show-graph 'log --graph --abbrev-commit --pretty=oneline'

更多配置,请参考:Git tips and tricks

下面是一份配置好的.gitconfig:

  1. [user]
  2. name = Peng Donglin
  3. email = dolinux.peng@gmail.com
  4. [alias]
  5. ci = commit
  6. st = status
  7. co = checkout
  8. br = branch
  9. d = difftool
      lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset' --abbrev-commit --date=relative
      lol = log --graph --decorate --pretty=oneline --abbrev-commit
  10. [core]
  11. editor = vim
  12. [color]
  13. ui = true
  14. [sendemail]
  15. smtpserver = /usr/bin/msmtp
  16. confirm = auto
  17. [diff]
  18. tool = vimdiff
  19. [difftool]
  20. prompt = false
  21. [merge]
  22. tool = vimdiff
  23. [mergetool]
  24. prompt = false

使用vimdiff作为 git diff工具

  1. git config --global diff.tool vimdiff
  2. git config --global difftool.prompt false
  3. git config --global alias.d difftool

然后使用 git d 打开对比代码,然后用 :wq 继续比较下一个文件。

  1. git difftool $start_commit..$end_commit -- path/to/file

git merge时使用vimdiff

  1. git config --global merge.tool vimdiff
  2. git config --global mergetool.prompt false

二、常用的命令

  •  .gitignore文件配置的一些规则

*.a # 忽略所有 .a 结尾的文件
!lib.a # 但 lib.a 除外
/TODO # 仅仅忽略项目根目录下的 TODO 文件,不包括 subdir/TODO
build/ # 忽略 build/ 目录下的所有文件
doc/*.txt # 会忽略 doc/notes.txt 但不包括 doc/server/arch.txt

  • 查看已暫存起來的文件跟上次提交時的快照之間的差異

    git diff --cached

  • 查看還沒有暫存起來的改動

    git diff

  • 查看两次提交之间某个文件的差异
  1. git diff $start_commit..$end_commit -- path/to/file
  • 查看两次提交之间的差异
  1. git diff $start_commit..$end_commit
  • 删除在远程仓库已经不存在的本地分支

To remove all tracking branches that you have locally but are no more present in the remote repository (origin):

  1. git remote prune origin

Use the --dry-run flag to only see what branches will be pruned, but not actually prune them:

  1. git remote prune origin --dry-run
  • 將已經提交的文件從Git倉庫中刪除,但是本地還保留該文件,即取消跟蹤某個文件

    git rm --cached readme.txt

  • 查看某次提交改動了那些文件

    git log --stat

    git log --stat dd257933fa4b9fea66a1195f8a15111029810abc -1

  • 簡要列出最近的提交commet
  1. $git log --pretty=oneline
  2. 694d0d0bb2030d2e36df73e2d23d5770511dbc8d Linux 4.8-rc2
  3. 0043ee40f98d15a50721fbd5dbf7c7797f8d03cd Merge branch 'next' of git://git.kernel.org/pub/scm/linux/kernel/git/rzhang/linux
  4. 4ef870e373382f18de214f61e5d5eb83b62b7aa5 Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/gerg/m68knommu
  • 列出某人的提交記錄
  1. $git log --author="Linus Torvalds"
  2. commit 694d0d0bb2030d2e36df73e2d23d5770511dbc8d
  3. Author: Linus Torvalds <torvalds@linux-foundation.org>
  4. Date: Sun Aug :: -
  5.  
  6. Linux 4.8-rc2
  7.  
  8. commit 0043ee40f98d15a50721fbd5dbf7c7797f8d03cd
  9. Merge: 4ef870e 1577ddf
  10. Author: Linus Torvalds <torvalds@linux-foundation.org>
  11. Date: Sun Aug :: -
  12.  
  13. Merge branch 'next' of git://git.kernel.org/pub/scm/linux/kernel/git/rzhang/linux
  14.  
  15. Pull thermal updates from Zhang Rui:

  如果想過濾掉這個人的merge記錄,可以用 git log --author="Linus Torvalds" --no-merges

  • 取消已經暫存的某個文件

    git reset HEAD readme.txt

  • 查看遠程倉庫的信息

    git remote show origin

  • 查看某個文件的若干行的改動記錄

    git blame -L 12,22 simplegit.rb

  1. git blame -w # ignores white space
  2. git blame -M # ignores moving text
  3. git blame -C # ignores moving text into other files
  • 获得某次提交的某个文件

    git checkout commit-id  file_name

  • 将本地的tags全部push到远程仓库

    git push origin --tags

  • 将本地分支全部push到远程仓库

    git push --all origin

  • 批量删除分支
  1.    git branch |grep 'branchName' |xargs git branch -D
  • 删除不再跟踪的远程分支

  git config --global fetch.prune true

  • git clean的用法

    git clean -n      显示 将要 删除的 文件 和  目录

     git clean -f  删除当前目录下所有没有track过的文件. 他不会删除.gitignore文件里面指定的文件夹和文件, 不管这些文件有没有被track过.

     git clean -df     删除当前目录下没有被track过的文件和文件夹

     git clean -xfd   删除当前目录下所有没有track过的文件. 不管他是否是.gitignore文件里面指定的文件夹和文件.

三、配置git email

在给linux kernel提交patch的时候一般都使用git send-email。

可以用用下面的命令安装git send-mail:

sudo apt-get install git-email

使用网易的163邮箱配置的方法:

1. 打开网易邮箱的设置界面,查看一下smtp的服务器的地址,如下图所示:

2. 修改.gitconfig,如下:

  1. [user]
  2. email = pengdonglin137@.com
  3. name = Donglin Peng
  4. [sendemail]
  5. smtpserver = smtp..com
  6. smtpserverport =
  7. smtpuser = pengdonglin137@.com
  8. smtpencryption = ssl
  9. confirm = auto

3. 测试

  1. $git send-email -ASoC-dpcm-print-dai_link-name-of-BE-other-than-FE.patch --to pengdonglin137@.com
  2. -ASoC-dpcm-print-dai_link-name-of-BE-other-than-FE.patch
  3. (mbox) Adding cc: Donglin Peng <pengdonglin@smartisan.com> from line 'From: Donglin Peng <pengdonglin@smartisan.com>'
  4. (body) Adding cc: Donglin Peng <pengdonglin@smartisan.com> from line 'Signed-off-by: Donglin Peng <pengdonglin@smartisan.com>'
  5.  
  6. From: Donglin Peng <pengdonglin137@.com>
  7. To: pengdonglin137@.com
  8. Cc: Donglin Peng <pengdonglin@smartisan.com>
  9. Subject: [PATCH] ASoC: dpcm: print dai_link name of BE other than FE.
  10. Date: Mon, Sep :: +
  11. Message-Id: <20160926095536.5890--pengdonglin137@.com>
  12. X-Mailer: git-send-email 2.9.
  13.  
  14. Send this email? ([y]es|[n]o|[q]uit|[a]ll): y
  15. Password for 'smtp://pengdonglin137@163.com@smtp.163.com:587':
  16. OK. Log says:
  17. Server: smtp..com
  18. MAIL FROM:<pengdonglin137@.com>
  19. RCPT TO:<pengdonglin137@.com>
  20. RCPT TO:<pengdonglin@smartisan.com>
  21. From: Donglin Peng <pengdonglin137@.com>
  22. To: pengdonglin137@.com
  23. Cc: Donglin Peng <pengdonglin@smartisan.com>
  24. Subject: [PATCH] ASoC: dpcm: print dai_link name of BE other than FE.
  25. Date: Mon, Sep :: +
  26. Message-Id: <20160926095536.5890--pengdonglin137@.com>
  27. X-Mailer: git-send-email 2.9.
  28.  
  29. Result: Mail OK queued as smtp5,D9GowAAnL4ea8OhXVnd4Cw--.197S2

4. 下面是收件箱的内容:

5. 向linux社区提交patch的时候,发送的邮件必须是纯文本格式,而不能是HTML格式。

6. 可以参考内核文档:https://www.kernel.org/doc/Documentation/SubmittingPatches

7. 可以参考博客:

  http://files.cnblogs.com/files/pengdonglin137/Linux_community_and_Upstream_Linux_Codes.pdf

  http://blog.sina.com.cn/s/blog_936739790102v5eu.html

用Git打包文件

git archive --format=tar.gz --prefix=Linux-3.16/ v3.16 > Linux-3.16.tar.gz

git archive --format=tar.gz --prefix=Linux-4.17/ 29dcea88779c856c7dc92040a0c01233263101d4 > Linux-4.17.tar.gz

 repo 常用命令

  1. repo start <topic_name>

开启一个新的主题,其实就是每个Project都新建一个分支

  1. repo init -u <url> [OPTIONS]

在当前目录下初始化repo,会在当前目录生生成一个.repo目录,像Git Project下的.git一样,-u指定url,可以加参数-m指定manifest文件,默认是default.xml,.repo/manifests保存manifest文件。.repo/projects下有所有的project的数据信息,repo是一系列git project的集合,每个git project下的.git目录中的refs等目录都是链接到.repo/manifests下的。

  1. repo manifest

可以根据当前各Project的版本信息生成一个manifest文件

  1. repo sync [PROJECT1...PROJECTN]

同步Code。

  1. repo status

查看本地所有Project的修改,在每个修改的文件前有两个字符,第一个字符表示暂存区的状态。

- no change same in HEAD and index
A added not in HEAD, in index
M modified in HEAD, modified in index
D deleted in HEAD, not in index
R renamed not in HEAD, path changed in index
C copied not in HEAD, copied from another in index
T mode changed same content in HEAD and index, mode changed
U unmerged conflict between HEAD and index; resolution required

  每二个字符表示工作区的状态

letter meaning description
- new/unknown not in index, in work tree
m modified in index, in work tree, modified
d deleted in index, not in work tree
  1. repo prune <topic>

删除已经merge的分支

  1. repo abandon <topic>

删除分支,无论是否merged

  1. repo branchrepo branches

查看所有分支

  1. repo diff

查看修改

  1. repo upload

上传本地提交至服务器

  1. repo forall [PROJECT_LIST]-c COMMAND

对指定的Project列表或所有Project执行命令COMMAND,加上-p参数可打印出Project的路径。

  1. repo forall -c 'git reset --hard HEAD;git clean -df;git rebase --abort'

这个命令可以撤销整个工程的本地修改。

比如:

把所有的库切换分支:  repo forall  -c git checkout branch_name

删除所有库的某个分支: repo forall  -c git branch -D branch_name

实例: repo forall -c "git co aosp/o-preview -b o-preview"

==

Git 和 Repo常用命令的更多相关文章

  1. git 和 repo 常用命令

    一.git 1.回退到某个节点 git reset --hard f39043d1c0cd1cda45a4569556758d0c00bf329a 2.查看提交记录 git log git log - ...

  2. git/repo常用命令

    Git作为广受欢迎的一款版本控制工具,它该如何通过命令行使用呢?本文为你揭晓浓缩精华精华版:git常用命令一览,含部分repo操作. 代码下载 repo init -- -->初始化需要下载的分 ...

  3. Git安装以及常用命令(图文详解)

    **Git安装以及常用命令** 1.下载安装Git,傻瓜式安装相信大家都会. 官网下载地址:[https://git-scm.com/downloads] 2.Git基本操作 (1)git --ver ...

  4. Git的一些常用命令

    一:Git是什么? Git是目前世界上最先进的分布式版本控制系统. 简单的说就是托管代码的便于多人开发的管理系统. 二.Git的一些命令,我详细的说一下 我是基于github给大家说一下git的一些常 ...

  5. Git Bash Here常用命令以及使用步骤

    1.首先,要clone项目代码: git clone 链接地址 2.更新代码: git pull 3.添加修改过的文件.文件夹: git add 修改过的文件,文件夹 4.提交并注释: git com ...

  6. Git(Repo)常用命令收集

    (注意: 只记录工作中实际使用的命令) 同步android源码    repo sync:(可加-c,只取当前分支: 可加-j4,线程数量) 查看android源码下所有项目的git状态    rep ...

  7. Git笔记:Git介绍和常用命令汇总

    Git 是一个开源的分布式版本控制系统,与 CVS, Subversion 等不同,它采用了分布式版本库的方式,不需要服务器端软件支持. 工作流程 Git 的工作流程大致如下: 克隆 Git 资源作为 ...

  8. Git配置和常用命令

    Git配置 git config --global user.name "hunng" git config --global user.email "huangthin ...

  9. Git 、 Cocoapods常用命令

    Git常用命令 1.添加文件   git  add  xxx 2.提交更新到本地  git commit   -m  'local-repo' 3.提交更新    git  push master  ...

随机推荐

  1. twitter typeahead控件使用经历

    typeahead控件可以用于自动完成这个功能,在jQuery的UI中也有自动完成的控件.以前都是用jQuery UI中的自动完成的控件,但这次想用个轻量级的自动完成的控件,因此就调查了一下typeh ...

  2. 【Arduino】开发入门【十】Arduino蓝牙模块与Android实现通信

    [Arduino]开发入门[十]蓝牙模块 首先show一下新入手的蓝牙模块 蓝牙参数特点 1.蓝牙核心模块使用HC-06从模块,引出接口包括VCC,GND,TXD,RXD,预留LED状态输出脚,单片机 ...

  3. Python_部分内置函数

    内置函数:可以直接调用的函数 all():传入的列表,元组,等等,只要一个为假,就为假(fales)(所有的都为真才为真) # None, {}:空字典, []:空列表, 0:零,():空集合,“”: ...

  4. net core体系-Xamarin-1概要

    大家在开发领域都知道Java是跨平台的,但是很多人认为.NET是只能在Windows下运行,不具有跨平台的特性,这种说法其实是不妥的. .NET其实在设计之初也是考虑像Java一样跨平台的,我们知道. ...

  5. vue $mount 和 el的区别

    两者在使用效果上没有任何区别,都是为了将实例化后的vue挂载到指定的dom元素中. 如果在实例化vue的时候指定el,则该vue将会渲染在此el对应的dom中,反之,若没有指定el,则vue实例会处于 ...

  6. day 34 编程之补充内容

    生产消费者模型(必须要理解并且牢记,默写内容): from multiprocessing import Process,Queue import time,random,os def procduc ...

  7. 基于Keil软件的MCU环境搭建

    我们在开发一款新的MCU的时候,偶尔会遇到Keil软件没有对应的Device设备选型,以下,我们以STM32F407VGT6作为实例来演示整个环境的搭建过程: 一.如下所示,我需要选择的是ST公司的S ...

  8. 安卓编程资源文件string中对占位符的使用详解

    这里将为你详细介绍占位符的使用,将其学以致用,可以达到简化布局文件,减少字符串资源量. 1.在资源文件中的使用. 打开资源文件中的strings.xml文件,进行编辑.如下图所示: 图  1.0 2. ...

  9. 项目部署相关命令(pm2)

    普通方式启动后台服务: nohup npm start & 关闭服务,需要找到进程号: lsof -i :3000 kill -9 进程号 通过pm2启动项目,可实现关闭自启动: 安装pm2: ...

  10. 基于Ardalis.GuardClauses守卫组件的拓展

    在我们写程序的时候,经常会需要判断数据的是空值还是null值,基本上十个方法函数,八个要做这样的判断,因此我们很有必要拓展出来一个类来做监控,在这里我们使用一个简单地,可拓展的第三方组件:Ardali ...