这篇博客总结的也不错:

8 Tips to help you work better with Git

git常用及进阶命令总结

Git与Repo入门

git-cheat-sheet.pdf

Git tips and tricks

一、初始環境配置

git config --global user.name "John Doe"
git config --global user.email johndoe@example.com git config --global core.editor vim
git config --global color.ui true git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status #Delete local branches that have been removed from remote on fetch/pull git config --global fetch.prune true git config --global alias.show-graph 'log --graph --abbrev-commit --pretty=oneline'

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

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

[user]
name = Peng Donglin
email = dolinux.peng@gmail.com
[alias]
ci = commit
st = status
co = checkout
br = branch
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
[core]
editor = vim
[color]
ui = true
[sendemail]
smtpserver = /usr/bin/msmtp
confirm = auto
[diff]
tool = vimdiff
[difftool]
prompt = false
[merge]
tool = vimdiff
[mergetool]
prompt = false

使用vimdiff作为 git diff工具

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

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

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

git merge时使用vimdiff

git config --global merge.tool vimdiff
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

  • 查看两次提交之间某个文件的差异
git diff $start_commit..$end_commit -- path/to/file
  • 查看两次提交之间的差异
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):

git remote prune origin

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

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

    git rm --cached readme.txt

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

    git log --stat

    git log --stat dd257933fa4b9fea66a1195f8a15111029810abc -1

  • 簡要列出最近的提交commet
$git log --pretty=oneline
694d0d0bb2030d2e36df73e2d23d5770511dbc8d Linux 4.8-rc2
0043ee40f98d15a50721fbd5dbf7c7797f8d03cd Merge branch 'next' of git://git.kernel.org/pub/scm/linux/kernel/git/rzhang/linux
4ef870e373382f18de214f61e5d5eb83b62b7aa5 Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/gerg/m68knommu
  • 列出某人的提交記錄
$git log --author="Linus Torvalds"
commit 694d0d0bb2030d2e36df73e2d23d5770511dbc8d
Author: Linus Torvalds <torvalds@linux-foundation.org>
Date: Sun Aug :: - Linux 4.8-rc2 commit 0043ee40f98d15a50721fbd5dbf7c7797f8d03cd
Merge: 4ef870e 1577ddf
Author: Linus Torvalds <torvalds@linux-foundation.org>
Date: Sun Aug :: - Merge branch 'next' of git://git.kernel.org/pub/scm/linux/kernel/git/rzhang/linux 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

git blame -w  # ignores white space
git blame -M # ignores moving text
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

  • 批量删除分支
   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,如下:

[user]
email = pengdonglin137@.com
name = Donglin Peng
[sendemail]
smtpserver = smtp..com
smtpserverport =
smtpuser = pengdonglin137@.com
smtpencryption = ssl
confirm = auto

3. 测试

$git send-email -ASoC-dpcm-print-dai_link-name-of-BE-other-than-FE.patch --to pengdonglin137@.com
-ASoC-dpcm-print-dai_link-name-of-BE-other-than-FE.patch
(mbox) Adding cc: Donglin Peng <pengdonglin@smartisan.com> from line 'From: Donglin Peng <pengdonglin@smartisan.com>'
(body) Adding cc: Donglin Peng <pengdonglin@smartisan.com> from line 'Signed-off-by: Donglin Peng <pengdonglin@smartisan.com>' From: Donglin Peng <pengdonglin137@.com>
To: pengdonglin137@.com
Cc: Donglin Peng <pengdonglin@smartisan.com>
Subject: [PATCH] ASoC: dpcm: print dai_link name of BE other than FE.
Date: Mon, Sep :: +
Message-Id: <20160926095536.5890--pengdonglin137@.com>
X-Mailer: git-send-email 2.9. Send this email? ([y]es|[n]o|[q]uit|[a]ll): y
Password for 'smtp://pengdonglin137@163.com@smtp.163.com:587':
OK. Log says:
Server: smtp..com
MAIL FROM:<pengdonglin137@.com>
RCPT TO:<pengdonglin137@.com>
RCPT TO:<pengdonglin@smartisan.com>
From: Donglin Peng <pengdonglin137@.com>
To: pengdonglin137@.com
Cc: Donglin Peng <pengdonglin@smartisan.com>
Subject: [PATCH] ASoC: dpcm: print dai_link name of BE other than FE.
Date: Mon, Sep :: +
Message-Id: <20160926095536.5890--pengdonglin137@.com>
X-Mailer: git-send-email 2.9. 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 常用命令

repo start <topic_name>

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

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下的。

repo manifest

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

repo sync [PROJECT1...PROJECTN]

同步Code。

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
repo prune <topic> 

删除已经merge的分支

repo abandon <topic>

删除分支,无论是否merged

repo branch或repo branches

查看所有分支

repo diff

查看修改

repo upload

上传本地提交至服务器

repo forall [PROJECT_LIST]-c COMMAND

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

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 常用命令的更多相关文章

  1. git常用命令(持续更新中)

    git常用命令(持续更新中) 本地仓库操作git int                                 初始化本地仓库git add .                       ...

  2. Git 常用命令详解

    Git 是一个很强大的分布式版本管理工具,它不但适用于管理大型开源软件的源代码(如:linux kernel),管理私人的文档和源代码也有很多优势(如:wsi-lgame-pro) Git 的更多介绍 ...

  3. Git 常用命令大全

    Git常用操作命令: 1) 远程仓库相关命令 检出仓库:$ git clone git://github.com/jquery/jquery.git 查看远程仓库:$ git remote -v 添加 ...

  4. Git常用命令总结

    Git常用命令总结 git init      在本地新建一个repo,进入一个项目目录,执行git init,会初始化一个repo,并在当前文件夹下创建一个.git文件夹.   git clone ...

  5. GIT常用命令备忘

    Git配置 git config --global user.name "storm" git config --global user.email "stormzhan ...

  6. Git 常用命令2

    Git 常用命令 Git 是一个很强大的分布式版本控制系统.它不但适用于管理大型开源软件的源代码,管理私人的文档和源代码也有很多优势. Git常用操作命令: 1) 远程仓库相关命令 检出仓库:$ gi ...

  7. git常用命令-基本操作

    git常用命令-基本操作 1)      新增文件 新增了Test_1.java git add Test_1.java git commit –m “新增了Test_1.java” git push ...

  8. 转收藏:Git常用命令速查表

    一. Git 常用命令速查 git branch 查看本地所有分支git status 查看当前状态 git commit 提交 git branch -a 查看所有的分支git branch -r ...

  9. git常用命令,git版本控制和Xcode结合使用,用Xcode提交到github,github客户端使用

    1.git常用命令 查看命令: 1.git --help 查看git所有命令 2.git clone -help 查看git clone命令的细节 3.git config -l   查看当前所有配置 ...

  10. Git 常用命令详解(二)

    Git 是一个很强大的分布式版本管理工具,它不但适用于管理大型开源软件的源代码(如:linux kernel),管理私人的文档和源代码也有很多优势(如:wsi-lgame-pro) Git 的更多介绍 ...

随机推荐

  1. ASP.NET MVC5+EF6+EasyUI 后台管理系统 (源码购买说明)

    系列目录 升级日志 !!!重大版本更新:于2016-12-20日完成了系统的结构重构并合并简化了T4(这是一次重要的更新,不需要修改现有功能的代码),代码总行数比上个版本又少了1/3.更新了代码生成器 ...

  2. Velocity笔记--使用Velocity获取动态Web项目名的问题

    以前使用jsp开发的时候,可以通过request很轻松的获取到根项目名,现在换到使用velocity渲染视图,因为已经不依赖servlet,request等一些类的环境,而Web项目的根项目名又不是写 ...

  3. Java FtpClient 实现文件上传服务

    一.Ubuntu 安装 Vsftpd 服务 1.安装 sudo apt-get install vsftpd 2.添加用户(uftp) sudo useradd -d /home/uftp -s /b ...

  4. 深入Java虚拟机--判断对象存活状态

    程序计数器,虚拟机栈和本地方法栈 首先我们先来看下垃圾回收中不会管理到的内存区域,在Java虚拟机的运行时数据区我们可以看到,程序计数器,虚拟机栈,本地方法栈这三个地方是比较特别的.这个三个部分的特点 ...

  5. 编写高质量代码:改善Java程序的151个建议(第8章:异常___建议114~117)

    建议114:不要在构造函数中抛出异常 Java异常的机制有三种: Error类及其子类表示的是错误,它是不需要程序员处理也不能处理的异常,比如VirtualMachineError虚拟机错误,Thre ...

  6. ASP.Net MVC——使用 ITextSharp 完美解决HTML转PDF(中文也可以)

    前言: 最近在做老师交代的一个在线写实验报告的小项目中,有这么个需求:把学生提交的实验报告(HTML形式)直接转成PDF,方便下载和打印. 以前都是直接用rdlc报表实现的,可这次牵扯到图片,并且更为 ...

  7. 基于注解的bean配置

    基于注解的bean配置,主要是进行applicationContext.xml配置.DAO层类注解.Service层类注解. 1.在applicationContext.xml文件中配置信息如下 &l ...

  8. Atiti.大企业病与小企业病 大公司病与小公司病

    Atiti.大企业病与小企业病 大公司病与小公司病 1. 大企业病,一般会符合机构臃肿 .多重领导 .人才流失的特点.1 2. 大企业病避免方法1 3. 小企业病 1 3.1.1. 表现1 4. 如何 ...

  9. 在Linux虚拟机下配置jdk的环境变量

    1.到Oracle公司的官网里下载好jdk,网址 http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133 ...

  10. could not initialize proxy - no Session

    这是一个精典的问题:因为我们在hibernate里面load一个对象出来时,用到的是代理对象,也就是说当我们在执行load方法时并没有发sql语句,而是返回一个proxy对象.只有当们具体用到哪个ge ...