andybons | 22afb31 | 2015-08-31 02:24:51 | [diff] [blame] | 1 | # Git Tips |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 2 | |
andybons | 22afb31 | 2015-08-31 02:24:51 | [diff] [blame] | 3 | When using Git, there are a few tips that are particularly useful when working |
| 4 | on the Chromium codebase, especially due to its size. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 5 | |
andybons | 22afb31 | 2015-08-31 02:24:51 | [diff] [blame] | 6 | [TOC] |
| 7 | |
| 8 | ## Remember the basic git convention: |
| 9 | |
| 10 | git COMMAND [FLAGS] [ARGUMENTS] |
| 11 | |
| 12 | Various 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). |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 15 | |
| 16 | ## Git references |
| 17 | |
| 18 | The following resources can provide background on how Git works: |
| 19 | |
andybons | 22afb31 | 2015-08-31 02:24:51 | [diff] [blame] | 20 | * [Git-SVN Crash Course](http://git-scm.com/course/svn.html) -- this crash |
boushley | c9e97492 | 2017-03-31 20:20:41 | [diff] [blame] | 21 | course is useful for Subversion users switching to Git. |
andybons | 22afb31 | 2015-08-31 02:24:51 | [diff] [blame] | 22 | * [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. |
Ernesto Izquierdo Clua | 8fed53e | 2023-05-09 16:34:25 | [diff] [blame] | 26 | * [A Visual Git Reference](https://marklodato.github.io/visual-git-guide/index-en.html) |
boushley | c9e97492 | 2017-03-31 20:20:41 | [diff] [blame] | 27 | -- a resource that explains various Git operations for visual learners. |
andybons | 22afb31 | 2015-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. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 31 | |
Hong Xu | 747ba9f | 2023-07-22 00:14:11 | [diff] [blame] | 32 | ## Optimizing (Speeding up) Git for a Large Repository |
| 33 | |
| 34 | Git has numerous options, among which some are intended to optimize for large |
| 35 | repositories. |
| 36 | [feature.manyFiles](https://git-scm.com/docs/git-config#Documentation/git-config.txt-featuremanyFiles) |
| 37 | is a convenient option that turns on the group of options that optimize for |
| 38 | large repositories. Run the following inside the Chromium git repository: |
| 39 | |
| 40 | git config feature.manyFiles true |
| 41 | |
Colin Blundell | 4fcadea | 2017-06-13 14:44:17 | [diff] [blame] | 42 | ## Configuring the output of "git log" |
| 43 | |
| 44 | By default, the date that "git log" displays is the "author date." In Chromium, |
| 45 | this generally corresponds to the date that the committed patch was last |
| 46 | uploaded. In most cases, however, the date that is of interest is the date that |
| 47 | the patch was committed in the tree. To configure "git log" to instead display |
| 48 | the latter date for your Chromium checkout, execute the following command: |
| 49 | |
| 50 | ```shell |
| 51 | git 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 | |
| 54 | If you want to change *all* your repos (e.g., because you have multiple Chromium |
| 55 | checkouts and don't care about having the default for other repos), add |
| 56 | "--global" after "config" in the above command. |
| 57 | |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 58 | ## Committing changes |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 59 | |
andybons | 22afb31 | 2015-08-31 02:24:51 | [diff] [blame] | 60 | For a simple workflow (always commit all changed files, don't keep local |
| 61 | revisions), the following script handles check; you may wish to call it `gci` |
| 62 | (git commit) or similar. |
| 63 | |
| 64 | Amending a single revision is generally easier for various reasons, notably for |
| 65 | rebasing and for checking that CLs have been committed. However, if you don't |
| 66 | use local revisions (a local branch with multiple revisions), you should make |
| 67 | sure to upload revisions periodically to code review if you ever need to go to |
| 68 | an old version of a CL. |
| 69 | |
| 70 | ```bash |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 71 | #!/bin/bash |
| 72 | # Commit all, amending if not initial commit. |
Andrew Williams | bbc1a1e | 2021-07-21 01:51:22 | [diff] [blame] | 73 | if git status | grep -q "Your branch is ahead of 'origin/main' by 1 commit." |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 74 | then |
| 75 | git commit --all --amend |
| 76 | else |
| 77 | git commit --all # initial, not amendment |
| 78 | fi |
| 79 | ``` |
| 80 | |
| 81 | ## Listing and changing branches |
andybons | 22afb31 | 2015-08-31 02:24:51 | [diff] [blame] | 82 | |
| 83 | ```shell |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 84 | git branch # list branches |
| 85 | git checkout - # change to last branch |
| 86 | ``` |
andybons | 22afb31 | 2015-08-31 02:24:51 | [diff] [blame] | 87 | |
| 88 | To quickly list the 5 most recent branches, add the following to `.gitconfig` |
| 89 | in the `[alias]` section: |
| 90 | |
| 91 | ```shell |
| 92 | last5 = "!git for-each-ref --sort=committerdate refs/heads/ \ |
| 93 | --format='%(committerdate:short) %(refname:short)' | tail -5 | cut -c 12-" |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 94 | ``` |
| 95 | |
andybons | 22afb31 | 2015-08-31 02:24:51 | [diff] [blame] | 96 | A nicely color-coded list, sorted in descending order by date, can be made by |
| 97 | the following bash function: |
| 98 | |
| 99 | ```bash |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 100 | git-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 \ |
andybons | 22afb31 | 2015-08-31 02:24:51 | [diff] [blame] | 106 | --format=$' %(refname:short) \ |
| 107 | \t%(committerdate:short)\t%(authorname)\t%(objectname:short)' \ |
| 108 | refs/heads \ |
andybons | 3322f76 | 2015-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 |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 116 | |
andybons | 22afb31 | 2015-08-31 02:24:51 | [diff] [blame] | 117 | Use `git-grep` instead of `grep` and `git-ls-files` instead of `find`, as these |
| 118 | search only files in the index or _tracked_ files in the work tree, rather than |
| 119 | all files in the work tree. |
| 120 | |
| 121 | Note that `git-ls-files` is rather simpler than `find`, so you'll often need to |
| 122 | use `xargs` instead of `-exec` if you want to process matching files. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 123 | |
| 124 | ## Global changes |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 125 | |
andybons | 22afb31 | 2015-08-31 02:24:51 | [diff] [blame] | 126 | To make global changes across the source tree, it's often easiest to use `sed` |
| 127 | with `git-ls-files`, using `-i` for in-place changing (this is generally safe, |
| 128 | as we don't use symlinks much, but there are few places that do). Remember that |
| 129 | you don't need to use `xargs`, since sed can take multiple input files. E.g., to |
| 130 | strip trailing whitespace from C++ and header files: |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 131 | |
andybons | 22afb31 | 2015-08-31 02:24:51 | [diff] [blame] | 132 | sed -i -E 's/\s+$//' $(git ls-files '*.cpp' '*.h') |
| 133 | |
| 134 | |
| 135 | You may also find `git-grep` useful for limiting the scope of your changes, |
| 136 | using `-l` for listing files. |
| 137 | |
| 138 | sed -i -E '...' $(git grep -lw Foo '*.cpp' '*.h') |
| 139 | |
| 140 | Remember that you can restrict sed actions to matching (or non-matching) lines. |
| 141 | For example, to skip lines with a line comment, use the following: |
| 142 | |
| 143 | '\,//, ! s/foo/bar/g' |
| 144 | |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 145 | ## Diffs |
andybons | 22afb31 | 2015-08-31 02:24:51 | [diff] [blame] | 146 | |
| 147 | git diff --shortstat |
| 148 | |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 149 | Displays summary statistics, such as: |
andybons | 22afb31 | 2015-08-31 02:24:51 | [diff] [blame] | 150 | |
| 151 | 2104 files changed, 9309 insertions(+), 9309 deletions(-) |