Git Command之Code Review
准备
- Step 1. Create a team and add a teammate
- Step 2. Create a repository with some content
应用
Clone and make a change on a new branch
- Step 1. Clone your repository to your local system
- Step 2. Create a branch and pull in locally
- Step 3. Make a change to the branch
Create a pull request to merge your change
- Step 1. Create the pull request
- Step 2. Merge your pull request
Git Log
If you don’t like the default git log format, you can use git config’s aliasing functionality to create a shortcut for any of the formatting options discussed below
which git log’s output can be formatted
Oneline:The
--onelineflag condenses each commit to a single line.- By default, it displays only the commit ID and the first line of the commit message.
- This is very useful for getting a high-level overview of your project.
0e25143 Merge branch 'feature'
ad8621a Fix a bug in the feature
16b36c6 Add a new feature
23ad9ad Add the initial code base
Decorating:The
--decorateflag makesgit logdisplay all of the references (e.g., branches, tags, etc) that point to each commit.- Many times it’s useful to know which branch or tag each commit is associated with.
- This can be combined with other configuration options.
- For example, running
git log --oneline --decoratewill format the commit history like so:- This lets you know that the top commit is also checked out (denoted by
HEAD) and that it is also the tip of themasterbranch. The second commit has another branch pointing to it calledfeature, and finally the 4th commit is tagged asv0.9. 0e25143 (HEAD, master) Merge branch 'feature'
ad8621a (feature) Fix a bug in the feature
16b36c6 Add a new feature
23ad9ad (tag: v0.9) Add the initial code base
- This lets you know that the top commit is also checked out (denoted by
Branches, tags,
HEAD, and the commit history are almost all of the information contained in your Git repository, so this gives you a more complete view of the logical structure of your repository.
Diffs:The
git logcommand includes many options for displaying diffs with each commit.- Two of the most common options are
--statand-p.- The
--statoption displays the number of insertions and deletions to each file altered by each commit (note that modifying a line is represented as 1 insertion and 1 deletion).- This is useful when you want a brief summary of the changes introduced by each commit.
- For example, the following commit added 67 lines to the
hello.pyfile and removed 38 lines:- The amount of
+and-signs next to the file name show the relative number of changes to each file altered by the commit. - This gives you an idea of where the changes for each commit can be found.
commit f2a238924e89ca1d4947662928218a06d39068c3
Author: John <john@example.com>
Date: Fri Jun 25 17:30:28 2014 -0500 Add a new feature hello.py | 105 ++++++++++++++++++++++++-----------------
1 file changed, 67 insertion(+), 38 deletions(-)
- The amount of
This gives you an idea of where the changes for each commit can be found.you can pass the
-poption togit log.- This outputs the entire patch representing that commit:
- For commits with a lot of changes, the resulting output can become quite long and unwieldy. More often than not, if you’re displaying a full patch, you’re probably searching for a specific change. For this, you want to use the pickaxe option.
commit 16b36c697eb2d24302f89aa22d9170dfe609855b
Author: Mary <mary@example.com>
Date: Fri Jun 25 17:31:57 2014 -0500 Fix a bug in the feature diff --git a/hello.py b/hello.py
index 18ca709..c673b40 100644
--- a/hello.py
+++ b/hello.py
@@ -13,14 +13,14 @@ B
-print("Hello, World!")
+print("Hello, Git!")
- The
- Two of the most common options are
The Shortlog:The
git shortlogcommand is a special version ofgit logintended for creating release announcements.- It groups each commit by author and displays the first line of each commit message.This is an easy way to see who’s been working on what.
- For example, if two developers have contributed 5 commits to a project, the
git shortlogoutput might look like the following:Mary (2):
Fix a bug in the feature
Fix a serious security hole in our framework John (3):
Add the initial code base
Add a new feature
Merge branch 'feature'
- By default,
git shortlogsorts the output by author name, but you can also pass the-noption to sort by the number of commits per author.
Graphs:The
--graphoption draws an ASCII graph representing the branch structure of the commit history.- This is commonly used in conjunction with the
--onelineand--decoratecommands to make it easier to see which commit belongs to which branch: - For a simple repository with just 2 branches, this will produce the following:
git log --graph --oneline --decorate * 0e25143 (HEAD, master) Merge branch 'feature'
|\
| * 16b36c6 Fix a bug in the new feature
| * 23ad9ad Start a new feature
* | ad8621a Fix a critical security issue
|/
* 400e4b7 Fix typos in the documentation
* 160e224 Add the initial code base- The asterisk shows which branch the commit was on, so the above graph tells us that the
23ad9adand16b36c6commits are on a topic branch and the rest are on themasterbranch.
- While this is a nice option for simple repositories, you’re probably better off with a more full-featured visualization tool like
gitkor Sourcetree for projects that are heavily branched.
- This is commonly used in conjunction with the
Custom Formatting:For all of your other
git logformatting needs, you can use the--pretty=format:"<string>"option.- This lets you display each commit however you want using
printf-style placeholders. - For example, the
%cn,%hand%cdcharacters in the following command are replaced with the committer name, abbreviated commit hash, and the committer date, respectively.git log --pretty=format:"%cn committed %h on %cd" John committed 400e4b7 on Fri Jun 24 12:30:04 2014 -0500
John committed 89ab2cf on Thu Jun 23 17:09:42 2014 -0500
Mary committed 180e223 on Wed Jun 22 17:21:19 2014 -0500
John committed f12ca28 on Wed Jun 22 13:50:31 2014 -0500
- The complete list of placeholders can be found in the Pretty Formats section of the
git logmanual page. - Aside from letting you view only the information that you’re interested in, the
--pretty=format:"<string>"option is particularly useful when you’re trying to pipegit logoutput into another command.
- This lets you display each commit however you want using
Filtering the Commit History
By Amount:The most basic filtering option for
git logis to limit the number of commits that are displayed.- When you’re only interested in the last few commits, this saves you the trouble of viewing all the commits in a page.
- You can limit
git log’s output by including the-<n>option. - For example, the following command will display only the 3 most recent commits.
git log -3
By Date:If you’re looking for a commit from a specific time frame, you can use the
--afteror--beforeflags for filtering commits by date.- These both accept a variety of date formats as a parameter.
- For example, the following command only shows commits that were created after July 1st, 2014 (inclusive):
git log --after="2014-7-1" git log --after="yesterday" git log --after="2014-7-1" --before="2014-7-4"
- You can also pass in relative references like
"1 week ago"and"yesterday": - To search for a commits that were created between two dates, you can provide both a
--beforeand--afterdate. - Note that the
--sinceand--untilflags are synonymous with--afterand--before, respectively.
By Author:When you’re only looking for commits created by a particular user, use the
--authorflag.- This accepts a regular expression, and returns all commits whose author matches that pattern.
- If you know exactly who you’re looking for, you can use a plain old string instead of a regular expression
git log --author="John"
- This displays all commits whose author includes the name John. The author name doesn’t need to be an exact match—it just needs to contain the specified phrase.
- You can also use regular expressions to create more complex searches.
- For example, the following command searches for commits by either Mary or John.
git log --author="John\|Mary"
- Note that the author’s email is also included with the author’s name, so you can use this option to search by email, too.
- For example, the following command searches for commits by either Mary or John.
- If your workflow separates committers from authors, the
--committerflag operates in the same fashion.
By Message:To filter commits by their commit message, use the
--grepflag.- This works just like the
--authorflag discussed above, but it matches against the commit message instead of the author. - For example, if your team includes relevant issue numbers in each commit message, you can use something like the following to pull out all of the commits related to that issue:
git log --grep="JRA-224:"
- You can also pass in the
-iparameter togit logto make it ignore case differences while pattern matching.
- This works just like the
By File:Many times, you’re only interested in changes that happened to a particular file.
- To show the history related to a file, all you have to do is pass in the file path.
- For example, the following returns all commits that affected either the
foo.pyor thebar.pyfile:git log -- foo.py bar.py
- The
--parameter is used to tellgit logthat subsequent arguments are file paths and not branch names.- If there’s no chance of mixing it up with a branch, you can omit the
--.
- If there’s no chance of mixing it up with a branch, you can omit the
By Content:
- It’s also possible to search for commits that introduce or remove a particular line of source code.This is called a pickaxe, and it takes the form of
-S"<string>". - For example, if you want to know when the string Hello, World! was added to any file in the project, you would use the following command:
git log -S"Hello, World!"
- If you want to search using a regular expression instead of a string, you can use the
-G"<regex>"flag instead. - This is a very powerful debugging tool, as it lets you locate all of the commits that affect a particular line of code. It can even show you when a line was copied or moved to another file.
- It’s also possible to search for commits that introduce or remove a particular line of source code.This is called a pickaxe, and it takes the form of
By Range:You can pass a range of commits to
git logto show only the commits contained in that range.- The range is specified in the following format, where
<since>and<until>are commit references: git log <since>..<until>
- This command is particularly useful when you use branch references as the parameters.
- It’s a simple way to show the differences between 2 branches.
git log master..feature
- The
master..featurerange contains all of the commits that are in thefeaturebranch, but aren’t in themasterbranch. In other words, this is how farfeaturehas progressed since it forked off ofmaster. 
- Note that if you switch the order of the range (
feature..master), you will get all of the commits inmaster, but not infeature.
- It’s a simple way to show the differences between 2 branches.
- If
git logoutputs commits for both versions, this tells you that your history has diverged.
- The range is specified in the following format, where
Filtering Merge Commits
- By default,
git logincludes merge commits in its output. - But, if your team has an always-merge policy (that is, you merge upstream changes into topic branches instead of rebasing the topic branch onto the upstream branch), you’ll have a lot of extraneous merge commits in your project history
- You can prevent
git logfrom displaying these merge commits by passing the--no-mergesflag:- git log --no-merges
- You can prevent
- On the other hand, if you’re only interested in the merge commits, you can use the
--mergesflag:git log --merges
- This returns all commits that have at least two parents.
- By default,
These new skills are an important part of your Git toolkit, but remember that git log is often used in conjunction other Git commands.
Once you’ve found the commit you’re looking for, you typically pass it off to git checkout, git revert, or some other tool for manipulating your commit history.
Git log 应用
- Git command may help during the code review:
#Hint: sample script in git bash (List the files commited by Xuan in platform v2.3 branch in the week):
git log --branches=*v2.3* --since 2019-07-15 --pretty=format:"%an %h %aD %s " --reverse |grep -i "xuan"|cut -d " " -f2| xargs -P 0 -n 1 -I {} git diff-tree --no-commit-id --name-only -r {}|sort|uniq
#-P 0 注释:The -P switch should be turn off since it may impact performance.git log [<options>] [<revision range>] [[--] <path>…]
- options:
- options applicable to the
git rev-listcommand to control what is shown and how - options applicable to the
git diff-*commands to control how the changes each commit introduces are shown.
- options applicable to the
- -i(--regexp-ignore-case):Match the regular expression limiting patterns without regard to letter case.
- -L <start>,<end>:<file>
- -L :<funcname>:<file>
- --branches[=<pattern>]
- Pretend as if all the refs in
refs/headsare listed on the command line as <commit>. If <pattern> is given, limit branches to ones matching given shell glob. If pattern lacks ?, *, or [, /* at the end is implied.
- Pretend as if all the refs in
- --since=<date>
- --after=<date>
- Show commits more recent than a specific date.
- --pretty[=<format>]
- --format=<format>
- Pretty-print the contents of the commit logs in a given format, where <format> can be one of oneline, short, medium, full, fuller, email, raw, format:<string> and tformat:<string>. When <format> is none of the above, and has %placeholder in it, it acts as if --pretty=tformat:<format> were given.
- See the "PRETTY FORMATS" section for some additional details for each format. When =<format> part is omitted, it defaults to medium.
- Note: you can specify the default pretty format in the repository configuration (see git-config(1)).
- --reverse
- Output the commits chosen to be shown (see Commit Limiting section above) in reverse order. Cannot be combined with
--walk-reflogs.
- Output the commits chosen to be shown (see Commit Limiting section above) in reverse order. Cannot be combined with
- options:
To get the commit number, add the following command to the previous pipe:
cut -d “ “ -f2
New files should get high priority. For modified files, you can just compare between the beginning of this week and the current snapshot.
- It’ll be better if you can submit you report in .md format which is supported by Visual Code.
- 用Visual Studio Code+ Markdown All in One插件修改。 关于md格式,请参考:基础mdbasic,高级mdextended
Git Command之Code Review的更多相关文章
- Git和Code Review流程
Code Review流程1.根据开发任务,建立git分支, 分支名称模式为feature/任务名,比如关于API相关的一项任务,建立分支feature/api.git checkout -b fea ...
- 从code review到Git commit log
最近在读一本技术类的书:朱赟——<跃迁:从技术到管理的硅谷路径>,其中聊了很多很有趣的观点,比如:技术管理.技术实践.硅谷文化.个人成长等. 读到关于硅谷人如何做code review这一 ...
- 在win7下使用git和gitlab进行code review
1.安装 Git-2.6.3-64-bit.exe 下载地址:http://pan.baidu.com/s/1hqGvwnq 2.根据收到的邮件进入gitlab网站,并修改密码登陆 3.新建一个文件 ...
- IDEA工具java开发之 常用插件 git插件 追加提交 Code Review==代码评审插件 撤销提交 撤销提交 关联远程仓库 设置git 本地操作
◆git 插件 请先安装git for windows ,git客户端工具 平时开发中,git的使用都是用可视化界面,git命令需要不时复习,以备不时之需 1.环境准备 (1)设置git (2)本地操 ...
- Git Gerrit Code Review
Gerrit Code Review | Gerrit Code Reviewhttps://www.gerritcodereview.com/
- jenkins + gerrit 自动code review
最近有需求要push review以后自动跑一些测试,如果通过就自动+2 不通过就-2,目前做法如下(jenkins gerrit均已配置好,Jenkins可以连接gerrit并拉代码): 1. Je ...
- 我们是怎么做Code Review的
前几天看了<Code Review 程序员的寄望与哀伤>,想到我们团队开展Code Review也有2年了,结果还算比较满意,有些经验应该可以和大家一起分享.探讨.我们为什么要推行Code ...
- 如何搭建开源code review gerrit服务器
搭建环境:Ubuntu 14.04 一.环境准备 1.Java环境 gerrit依赖,用于安装gerrit环境. 下载:jdk-7u79-linux-x64.tar.gz http://www.ora ...
- Code Review Tools
Code Review中文应该译作“代码审查”或是“代码评审”,这是一个流程,当开发人员写好代码后,需要让别人来review一下他的代码,这是一种有效发现BUG的方法.由此,我们可以审查代码的风格.逻 ...
随机推荐
- mariadb-server安装问题(Error: MariaDB-common conflicts with 1:mariadb-libs-5.5.60-1.el7_5.x86_64)
问题:今天在安装mariadb-server包时,提示错误,无法正确安装linux自带的mariadb包,提示错误很明确,是由于MariaDB-common包与mariadb-libs包冲突. 解决办 ...
- jmeter——http、jdbc、soap请求
1.jmeter——http 请求 1.1添加线程组 1.2添加http请求 1.3发起http请求 1.协议:通常一个http请求都会有相对应的协议,如HTTP,HTTPS等.这里除非有特殊要求,一 ...
- mybatise 设置全局变量实例
前言 在平时的工作中有时候是需要在配置文件中配置全局变量的,因为这些东西是不会变的,并且每个mapper都传参的话也显得有点繁琐,还好mybatis本身是支持全局变量的,今天工作中用到了,记录一下. ...
- 如何判断PHP空间是否支持curl、gzip等功能
在网站根目录新建v.php,输入以下代码: <?php $f=@trim($_GET['f']); if(function_exists($f)) echo '支持'.$f; else echo ...
- Warning: (1260, 'Row xxx was cut by GROUP_CONCAT()')
MySql数据库查询时,使用group_concat报错“Row XXX was cut by GROUP_CONCAT()”,查了下是因为group_concat有个最大长度的限制,超过最大长度就会 ...
- SQL的左连接与右链接
1.准备工作 Oracle 外连接(OUTER JOIN)包括以下: 左外连接(左边的表不加限制) 右外连接(右边的表不加限制) 全外连接(左右两表都不加限制) 对应SQL:LEFT/RIGHT/F ...
- 8、组件注册-@Import-给容器中快速导入一个组件
8.组件注册-@Import-给容器中快速导入一个组件 8.1 给容器中注册组建的方式 包扫描+组建标注注解(@Controller.@Service.@Repository.@Component)[ ...
- [Luogu P1658] 购物
题目链接 这道题的主要思想是贪心. 题目的要求用几个硬币将1~x的数都能够凑出的最少硬币个数.这里注意一下是都凑出而不是同时凑出. 先讨论什么时候无解.所有的自然数都可以用1堆砌而成.换而言之只要有1 ...
- 为什么说Redis是单线程的?
一.前言 近乎所有与Java相关的面试都会问到缓存的问题,基础一点的会问到什么是“二八定律”.什么是“热数据和冷数据” ,复杂一点的会问到缓存雪崩.缓存穿透.缓存预热.缓存更新.缓存降级等问题,这些看 ...
- 031_检测 MySQL 服务是否存活
#!/bin/bash#host 为你需要检测的 MySQL 主机的 IP 地址,user 为 MySQL 账户名,passwd 为密码#这些信息需要根据实际情况修改后方可使用 host=127.0. ...