Movatterモバイル変換


[0]ホーム

URL:


Google Git
Sign in
chromium /chromium /src /refs/heads/main /. /docs /git_tips.md
blob: b12419d77761c9c95ca2dfaed015115f6411942e [file] [log] [blame] [view]
andybons22afb312015-08-31 02:24:51[diff] [blame]1# Git Tips
andybons3322f762015-08-24 21:37:09[diff] [blame]2
andybons22afb312015-08-31 02:24:51[diff] [blame]3WhenusingGit, there are a few tips that are particularly usefulwhen working
4on theChromium codebase, especially due to its size.
andybons3322f762015-08-24 21:37:09[diff] [blame]5
andybons22afb312015-08-31 02:24:51[diff] [blame]6[TOC]
7
8## Remember the basic git convention:
9
10 git COMMAND[FLAGS][ARGUMENTS]
11
12Various git commands have underlying executablewith a hyphenated name, suchas
13`git-grep`, but these can also be called via the`git` wrapper scriptas
14`git grep`(and`man` should work either way too).
andybons3322f762015-08-24 21:37:09[diff] [blame]15
16## Git references
17
18The following resources can provide background on howGit works:
19
andybons22afb312015-08-31 02:24:51[diff] [blame]20*[Git-SVNCrashCourse](http://git-scm.com/course/svn.html) -- this crash
boushleyc9e974922017-03-31 20:20:41[diff] [blame]21 courseis usefulforSubversion users switching toGit.
andybons22afb312015-08-31 02:24:51[diff] [blame]22*[ThinkLike(a)Git](http://think-like-a-git.net/) -- does a great job of
23 explaining the main purpose ofGit operations.
24*[GitUser's Manual](http://schacon.github.com/git/user-manual.html) -- a
25 great resource to learn more about ho to use Git properly.
Ernesto Izquierdo Clua8fed53e2023-05-09 16:34:25[diff] [blame]26* [A Visual Git Reference](https://marklodato.github.io/visual-git-guide/index-en.html)
boushleyc9e974922017-03-31 20:20:41[diff] [blame]27 -- a resource that explains various Git operations for visual learners.
andybons22afb312015-08-31 02:24:51[diff] [blame]28* [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:09[diff] [blame]31
Hong Xu747ba9f2023-07-22 00:14:11[diff] [blame]32## Optimizing (Speeding up) Git for a Large Repository
33
34Git has numerous options, among which some are intended to optimizefor large
35repositories.
36[feature.manyFiles](https://git-scm.com/docs/git-config#Documentation/git-config.txt-featuremanyFiles)
37is a convenient option that turns on thegroup of options that optimizefor
38large repositories.Run the following inside theChromium git repository:
39
40 git config feature.manyFilestrue
41
Colin Blundell4fcadea2017-06-13 14:44:17[diff] [blame]42## Configuring the output of "git log"
43
44Bydefault, the date that"git log" displaysis the"author date."InChromium,
45this generally corresponds to the date that the committed patch waslast
46uploaded.In most cases, however, the date thatis of interestis the date that
47the patch was committedin the tree.To configure"git log" to instead display
48the latter datefor yourChromium checkout, execute the following command:
49
50```shell
51git 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'
52```
53
54If you want to change*all* your repos(e.g., because you have multipleChromium
55checkoutsand don't care about having the default for other repos), add
56"--global" after "config" in the above command.
57
andybons3322f762015-08-24 21:37:09[diff] [blame]58## Committing changes
andybons3322f762015-08-24 21:37:09[diff] [blame]59
andybons22afb312015-08-31 02:24:51[diff] [blame]60For a simple workflow (always commit all changed files, don't keeplocal
61revisions), the following script handles check; you may wish to call it`gci`
62(git commit)or similar.
63
64Amending a single revisionis generally easierfor various reasons, notablyfor
65rebasingandfor checking thatCLs have been committed.However,if you don't
66use local revisions (a local branch with multiple revisions), you should make
67sure to upload revisions periodically to code review if you ever need to go to
68an old version of a CL.
69
70```bash
andybons3322f762015-08-24 21:37:09[diff] [blame]71#!/bin/bash
72# Commit all, amending if not initial commit.
Andrew Williamsbbc1a1e2021-07-21 01:51:22[diff] [blame]73if git status | grep -q "Your branch is ahead of 'origin/main' by 1 commit."
andybons3322f762015-08-24 21:37:09[diff] [blame]74then
75 git commit --all --amend
76else
77 git commit --all # initial, not amendment
78fi
79```
80
81## Listing and changing branches
andybons22afb312015-08-31 02:24:51[diff] [blame]82
83```shell
andybons3322f762015-08-24 21:37:09[diff] [blame]84git branch # list branches
85git checkout - # change to last branch
86```
andybons22afb312015-08-31 02:24:51[diff] [blame]87
88To quickly list the 5 most recent branches, add the following to `.gitconfig`
89in the `[alias]` section:
90
91```shell
92last5 = "!git for-each-ref --sort=committerdate refs/heads/ \
93 --format='%(committerdate:short)%(refname:short)' | tail -5 | cut -c 12-"
andybons3322f762015-08-24 21:37:09[diff] [blame]94```
95
andybons22afb312015-08-31 02:24:51[diff] [blame]96A nicely color-coded list, sorted in descending order by date, can be made by
97the following bash function:
98
99```bash
andybons3322f762015-08-24 21:37:09[diff] [blame]100git-list-branches-by-date() {
101 local current_branch=$(git rev-parse --symbolic-full-name --abbrev-ref HEAD)
102 local normal_text=$(echo -ne '\E[0m')
103 local yellow_text=$(echo -ne '\E[0;33m')
104 local yellow_bg=$(echo -ne '\E[7;33m')
105 git for-each-ref --sort=-committerdate \
andybons22afb312015-08-31 02:24:51[diff] [blame]106 --format=$'%(refname:short) \
107 \t%(committerdate:short)\t%(authorname)\t%(objectname:short)' \
108 refs/heads \
andybons3322f762015-08-24 21:37:09[diff] [blame]109 | column -t -s $'\t' -n \
110 | sed -E "s:^ (${current_branch}) :* ${yellow_bg}\1${normal_text} :" \
111 | sed -E "s:^ ([^ ]+): ${yellow_text}\1${normal_text}:"
112}
113```
114
115## Searching
andybons3322f762015-08-24 21:37:09[diff] [blame]116
andybons22afb312015-08-31 02:24:51[diff] [blame]117Use `git-grep` instead of `grep` and `git-ls-files` instead of `find`, as these
118search only files in the index or _tracked_ files in the work tree, rather than
119all files in the work tree.
120
121Note that `git-ls-files` is rather simpler than `find`, so you'll often need to
122use`xargs` instead of`-exec`if you want to process matching files.
andybons3322f762015-08-24 21:37:09[diff] [blame]123
124## Global changes
andybons3322f762015-08-24 21:37:09[diff] [blame]125
andybons22afb312015-08-31 02:24:51[diff] [blame]126To makeglobal changes across the source tree, it's often easiest to use `sed`
127with `git-ls-files`, using `-i` for in-place changing (this is generally safe,
128as we don'tuse symlinks much, but there are few places thatdo).Remember that
129you don't need to use `xargs`, since sed can take multiple input files. E.g., to
130strip trailing whitespace from C++ and header files:
andybons3322f762015-08-24 21:37:09[diff] [blame]131
andybons22afb312015-08-31 02:24:51[diff] [blame]132 sed -i -E 's/\s+$//' $(git ls-files '*.cpp' '*.h')
133
134
135You may also find`git-grep` usefulfor limiting the scope of your changes,
136using`-l`for listing files.
137
138 sed-i-E'...' $(git grep-lwFoo'*.cpp''*.h')
139
140Remember that you can restrict sed actions to matching(or non-matching) lines.
141For example, to skip lineswith a line comment,use the following:
142
143'\,//, ! s/foo/bar/g'
144
andybons3322f762015-08-24 21:37:09[diff] [blame]145## Diffs
andybons22afb312015-08-31 02:24:51[diff] [blame]146
147 git diff--shortstat
148
andybons3322f762015-08-24 21:37:09[diff] [blame]149Displays summary statistics, suchas:
andybons22afb312015-08-31 02:24:51[diff] [blame]150
1512104 files changed,9309 insertions(+),9309 deletions(-)

[8]ページ先頭

©2009-2025 Movatter.jp