maruel | 9e198a8 | 2016-08-11 15:32:19 | [diff] [blame] | 1 | # git-cl |
| 2 | |
| 3 | The git-cl README describes the git-cl command set. This document describes how |
| 4 | code review and git work together in general, intended for people familiar with |
| 5 | git but unfamiliar with the code review process supported by Rietveld and |
| 6 | Gerrit. |
| 7 | |
| 8 | |
maruel | 9e198a8 | 2016-08-11 15:32:19 | [diff] [blame] | 9 | ## Basic interaction with git |
| 10 | |
| 11 | The fundamental problem you encounter when you try to mix git and code review is |
| 12 | that with git it's nice to commit code locally, while during a code review |
| 13 | you're often requested to change something about your code. There are a few |
| 14 | different ways you can handle this workflow with git: |
| 15 | |
| 16 | 1. Rewriting a single commit. Say the origin commit is O, and you commit your |
| 17 | initial work in a commit A, making your history like O--A. After review |
qyearsley | 274c159 | 2016-09-05 00:16:19 | [diff] [blame] | 18 | comments, you `git commit --amend`, effectively erasing A and making a new |
| 19 | commit A', so history is now O--A'. (Equivalently, you can use |
| 20 | `git reset --soft` or `git rebase -i`.) |
maruel | 9e198a8 | 2016-08-11 15:32:19 | [diff] [blame] | 21 | 2. Writing follow-up commits. Initial work is again in A, and after review |
| 22 | comments, you write a new commit B so your history looks like O--A--B. When |
| 23 | you upload the revised patch, you upload the diff of O..B, not A..B; you |
| 24 | always upload the full diff of what you're proposing to change. |
| 25 | |
| 26 | The Rietveld patch uploader just takes arguments to `git diff`, so either of the |
| 27 | above workflows work fine. If all you want to do is upload a patch, you can use |
| 28 | the upload.py provided by Rietveld with arguments like this: |
| 29 | |
| 30 | upload.py --server server.com <args to "git diff"> |
| 31 | |
| 32 | The first time you upload, it creates a new issue; for follow-ups on the same |
| 33 | issue, you need to provide the issue number: |
| 34 | |
| 35 | upload.py --server server.com --issue 1234 <args to "git diff"> |
| 36 | |
| 37 | |
| 38 | ## git-cl to the rescue |
| 39 | |
| 40 | git-cl simplifies the above in the following ways: |
| 41 | |
| 42 | 1. `git cl config` puts a persistent --server setting in your .git/config. |
| 43 | 2. The first time you upload an issue, the issue number is associated with the |
| 44 | current *branch*. If you upload again, it will upload on the same issue. |
| 45 | (Note that this association is tied to a branch, not a commit, which means |
| 46 | you need a separate branch per review.) |
| 47 | 3. If your branch is _tracking_ (in the `git checkout --track` sense) another |
Josip Sokcevic | 9c0dc30 | 2020-11-20 18:41:25 | [diff] [blame] | 48 | one (like origin/main), calls to `git cl upload` will diff against that |
maruel | 9e198a8 | 2016-08-11 15:32:19 | [diff] [blame] | 49 | branch by default. (You can still pass arguments to `git diff` on the |
| 50 | command line, if necessary.) |
| 51 | |
| 52 | In the common case, this means that calling simply `git cl upload` will always |
| 53 | upload the correct diff to the correct place. |
| 54 | |
| 55 | |
| 56 | ## Patch series |
| 57 | |
| 58 | The above is all you need to know for working on a single patch. |
| 59 | |
| 60 | Things get much more complicated when you have a series of commits that you want |
| 61 | to get reviewed. Say your history looks like O--A--B--C. If you want to upload |
| 62 | that as a single review, everything works just as above. |
| 63 | |
| 64 | But what if you upload each of A, B, and C as separate reviews? What if you |
| 65 | then need to change A? |
| 66 | |
qyearsley | 274c159 | 2016-09-05 00:16:19 | [diff] [blame] | 67 | 1. One option is rewriting history: write a new commit A', then use |
| 68 | `git rebase -i` to insert that diff in as O--A--A'--B--C as well as squash |
| 69 | it. This is sometimes not possible if B and C have touched some lines |
| 70 | affected by A'. |
maruel | 9e198a8 | 2016-08-11 15:32:19 | [diff] [blame] | 71 | 2. Another option, and the one espoused by software like topgit, is for you to |
| 72 | have separate branches for A, B, and C, and after writing A' you merge it |
| 73 | into each of those branches. (topgit automates this merging process.) This |
| 74 | is also what is recommended by git-cl, which likes having different branch |
| 75 | identifiers to hang the issue number off of. Your history ends up looking |
| 76 | like: |
| 77 | |
| 78 | O---A---B---C |
| 79 | \ \ \ |
| 80 | A'--B'--C' |
| 81 | |
| 82 | Which is ugly, but it accurately tracks the real history of your work, can be |
| 83 | thrown away at the end by committing A+A' as a single `squash` commit. |
| 84 | |
| 85 | In practice, this comes up pretty rarely. Suggestions for better workflows are |
| 86 | welcome. |
tandrii | e594e21 | 2016-08-22 21:18:02 | [diff] [blame] | 87 | |
qyearsley | 274c159 | 2016-09-05 00:16:19 | [diff] [blame] | 88 | ## Bash auto completion |
tandrii | e594e21 | 2016-08-22 21:18:02 | [diff] [blame] | 89 | |
| 90 | 1. Ensure that your base git commands are autocompleted |
| 91 | [doc](https://git-scm.com/book/en/v1/Git-Basics-Tips-and-Tricks). |
| 92 | 2. Add this to your .bashrc: |
qyearsley | 274c159 | 2016-09-05 00:16:19 | [diff] [blame] | 93 | |
tandrii | e594e21 | 2016-08-22 21:18:02 | [diff] [blame] | 94 | # The next line enables bash completion for git cl. |
| 95 | if [ -f "$HOME/bin/depot_tools/git_cl_completion.sh" ]; then |
| 96 | . "$HOME/bin/depot_tools/git_cl_completion.sh" |
| 97 | fi |
qyearsley | 274c159 | 2016-09-05 00:16:19 | [diff] [blame] | 98 | |
tandrii | e594e21 | 2016-08-22 21:18:02 | [diff] [blame] | 99 | 3. Profit. |