blob: ee1e0315ba60d500da474754cb757c1486f30583 [file] [log] [blame] [view]
andybons22afb312015-08-31 02:24:511# Git Tips
andybons3322f762015-08-24 21:37:092
andybons22afb312015-08-31 02:24:513When using Git, there are a few tips that are particularly useful when working
4on the Chromium codebase, especially due to its size.
andybons3322f762015-08-24 21:37:095
andybons22afb312015-08-31 02:24:516[TOC]
7
8## Remember the basic git convention:
9
10 git COMMAND [FLAGS] [ARGUMENTS]
11
12Various git commands have underlying executable with a hyphenated name, such as
13`git-grep`, but these can also be called via the `git` wrapper script as
14`git grep` (and `man` should work either way too).
andybons3322f762015-08-24 21:37:0915
16## Git references
17
18The following resources can provide background on how Git works:
19
andybons22afb312015-08-31 02:24:5120* [Git-SVN Crash Course](http://git-scm.com/course/svn.html) -- this crash
boushleyc9e974922017-03-31 20:20:4121 course is useful for Subversion users switching to Git.
andybons22afb312015-08-31 02:24:5122* [Think Like (a) Git](http://think-like-a-git.net/) -- does a great job of
23 explaining the main purpose of Git operations.
24* [Git User's Manual](http://schacon.github.com/git/user-manual.html) -- a
25 great resource to learn more about ho to use Git properly.
26* [A Visual Git Reference](http://marklodato.github.com/visual-git-guide/index-en.html)
boushleyc9e974922017-03-31 20:20:4127 -- a resource that explains various Git operations for visual learners.
andybons22afb312015-08-31 02:24:5128* [Git Cheat Sheet](http://cheat.errtheblog.com/s/git) -- now that you
29 understand Git, here's a cheat sheet to quickly remind you of all the
30 commands you need.
andybons3322f762015-08-24 21:37:0931
Colin Blundell4fcadea2017-06-13 14:44:1732## Configuring the output of "git log"
33
34By default, the date that "git log" displays is the "author date." In Chromium,
35this generally corresponds to the date that the committed patch was last
36uploaded. In most cases, however, the date that is of interest is the date that
37the patch was committed in the tree. To configure "git log" to instead display
38the latter date for your Chromium checkout, execute the following command:
39
40```shell
41git config format.pretty 'format:%C(auto,yellow)commit %H%C(auto)%d%nAuthor: %an <%ae>%nCommitted: %cd%n%n%w(0,4,4)%B%-%n'
42```
43
44If you want to change *all* your repos (e.g., because you have multiple Chromium
45checkouts and don't care about having the default for other repos), add
46"--global" after "config" in the above command.
47
andybons3322f762015-08-24 21:37:0948## Committing changes
andybons3322f762015-08-24 21:37:0949
andybons22afb312015-08-31 02:24:5150For a simple workflow (always commit all changed files, don't keep local
51revisions), the following script handles check; you may wish to call it `gci`
52(git commit) or similar.
53
54Amending a single revision is generally easier for various reasons, notably for
55rebasing and for checking that CLs have been committed. However, if you don't
56use local revisions (a local branch with multiple revisions), you should make
57sure to upload revisions periodically to code review if you ever need to go to
58an old version of a CL.
59
60```bash
andybons3322f762015-08-24 21:37:0961#!/bin/bash
62# Commit all, amending if not initial commit.
63if git status | grep -q "# Your branch is ahead of 'master' by 1 commit."
64then
65 git commit --all --amend
66else
67 git commit --all # initial, not amendment
68fi
69```
70
71## Listing and changing branches
andybons22afb312015-08-31 02:24:5172
73```shell
andybons3322f762015-08-24 21:37:0974git branch # list branches
75git checkout - # change to last branch
76```
andybons22afb312015-08-31 02:24:5177
78To quickly list the 5 most recent branches, add the following to `.gitconfig`
79in the `[alias]` section:
80
81```shell
82last5 = "!git for-each-ref --sort=committerdate refs/heads/ \
83 --format='%(committerdate:short) %(refname:short)' | tail -5 | cut -c 12-"
andybons3322f762015-08-24 21:37:0984```
85
andybons22afb312015-08-31 02:24:5186A nicely color-coded list, sorted in descending order by date, can be made by
87the following bash function:
88
89```bash
andybons3322f762015-08-24 21:37:0990git-list-branches-by-date() {
91 local current_branch=$(git rev-parse --symbolic-full-name --abbrev-ref HEAD)
92 local normal_text=$(echo -ne '\E[0m')
93 local yellow_text=$(echo -ne '\E[0;33m')
94 local yellow_bg=$(echo -ne '\E[7;33m')
95 git for-each-ref --sort=-committerdate \
andybons22afb312015-08-31 02:24:5196 --format=$' %(refname:short) \
97 \t%(committerdate:short)\t%(authorname)\t%(objectname:short)' \
98 refs/heads \
andybons3322f762015-08-24 21:37:0999 | column -t -s $'\t' -n \
100 | sed -E "s:^ (${current_branch}) :* ${yellow_bg}\1${normal_text} :" \
101 | sed -E "s:^ ([^ ]+): ${yellow_text}\1${normal_text}:"
102}
103```
104
105## Searching
andybons3322f762015-08-24 21:37:09106
andybons22afb312015-08-31 02:24:51107Use `git-grep` instead of `grep` and `git-ls-files` instead of `find`, as these
108search only files in the index or _tracked_ files in the work tree, rather than
109all files in the work tree.
110
111Note that `git-ls-files` is rather simpler than `find`, so you'll often need to
112use `xargs` instead of `-exec` if you want to process matching files.
andybons3322f762015-08-24 21:37:09113
114## Global changes
andybons3322f762015-08-24 21:37:09115
andybons22afb312015-08-31 02:24:51116To make global changes across the source tree, it's often easiest to use `sed`
117with `git-ls-files`, using `-i` for in-place changing (this is generally safe,
118as we don't use symlinks much, but there are few places that do). Remember that
119you don't need to use `xargs`, since sed can take multiple input files. E.g., to
120strip trailing whitespace from C++ and header files:
andybons3322f762015-08-24 21:37:09121
andybons22afb312015-08-31 02:24:51122 sed -i -E 's/\s+$//' $(git ls-files '*.cpp' '*.h')
123
124
125You may also find `git-grep` useful for limiting the scope of your changes,
126using `-l` for listing files.
127
128 sed -i -E '...' $(git grep -lw Foo '*.cpp' '*.h')
129
130Remember that you can restrict sed actions to matching (or non-matching) lines.
131For example, to skip lines with a line comment, use the following:
132
133 '\,//, ! s/foo/bar/g'
134
andybons3322f762015-08-24 21:37:09135## Diffs
andybons22afb312015-08-31 02:24:51136
137 git diff --shortstat
138
andybons3322f762015-08-24 21:37:09139Displays summary statistics, such as:
andybons22afb312015-08-31 02:24:51140
141 2104 files changed, 9309 insertions(+), 9309 deletions(-)