blob: e89815d881ba98da2c3796fe6a45dbdc6b9c6e61 [file] [log] [blame] [view]
andybons22afb312015-08-31 02:24:511# Git Cookbook
2
andybons3322f762015-08-24 21:37:093A collection of git recipes to do common git tasks.
4
andybons22afb312015-08-31 02:24:515See also [Git Tips](git_tips.md).
andybons3322f762015-08-24 21:37:096
andybons22afb312015-08-31 02:24:517[TOC]
andybons3322f762015-08-24 21:37:098
9## Introduction
10
andybons22afb312015-08-31 02:24:5111This is designed to be a cookbook for common command sequences/tasks relating to
qyearsleyc0dc6f42016-12-02 22:13:3912git, git-cl, and how they work with Chromium development. It might be a little
andybons22afb312015-08-31 02:24:5113light on explanations.
andybons3322f762015-08-24 21:37:0914
andybons22afb312015-08-31 02:24:5115If you are new to git, or do not have much experience with a distributed version
16control system, you should also check out
17[The Git Community Book](http://book.git-scm.com/) for an overview of basic git
18concepts and general git usage. Knowing what git means by branches, commits,
19reverts, and resets (as opposed to what SVN means by them) will help make the
20following much more understandable.
andybons3322f762015-08-24 21:37:0921
Aaron Gablebbc51dc2017-05-17 18:45:5422## Chromium-specific Git Extensions
23
24Chromium ships a large number of git extensions in depot_tools. Some (like
25`git cl`) are required for the Chromium development workflow, while others
26(like `git map-branches`) are simple utilities to make your life easier.
27Please take a look at the full
28[depot_tools tutorial](https://commondatastorage.googleapis.com/chrome-infra-docs/flat/depot_tools/docs/html/depot_tools_tutorial.html),
29and at the extensive
30[man pages](https://commondatastorage.googleapis.com/chrome-infra-docs/flat/depot_tools/docs/html/depot_tools.html)
31for all the extensions.
32
andybons3322f762015-08-24 21:37:0933## Excluding file(s) from git-cl, while preserving them for later use
34
andybons22afb312015-08-31 02:24:5135Since git-cl assumes that the diff between your current branch and its tracking
Quinten Yearsley893fd752017-07-13 15:34:4636branch is what should be used for the CL, the goal is to remove the unwanted
37files from the current branch, and preserve them in another branch.
andybons3322f762015-08-24 21:37:0938
Quinten Yearsley893fd752017-07-13 15:34:4639### Method #1: Reset your current branch, and selectively commit files
andybons3322f762015-08-24 21:37:0940
andybons22afb312015-08-31 02:24:51411. `git log` See the list of your commits. Find the hash of the last commit
42 before your changes.
431. `git reset --soft abcdef` where abcdef is the hash found in the step above.
441. `git commit <files_for_this_cl> -m "files to upload"` commit the files you
45 want included in the CL here.
Quinten Yearsley893fd752017-07-13 15:34:46461. `git new-branch new_branch_name` Create a new branch for the
andybons22afb312015-08-31 02:24:5147 files that you want to exclude.
481. `git commit -a -m "preserved files"` Commit the rest of the files.
andybons3322f762015-08-24 21:37:0949
50### Method #2: Create a new branch, reset, then commit files to preserve
andybons3322f762015-08-24 21:37:0951
andybons22afb312015-08-31 02:24:5152This method creates a new branch from your current one to preserve your changes.
53The commits on the new branch are undone, and then only the files you want to
54preserve are recommitted.
andybons3322f762015-08-24 21:37:0955
andybons22afb312015-08-31 02:24:51561. `git checkout -b new_branch_name` This preserves your old files.
571. `git log` See the list of your commits. Find the hash of the last commit
58 before your changes.
591. `git reset --soft abcdef` Where abcdef is the hash found in the step above.
601. `git commit <files_to_preserve> -m "preserved files"` Commit the found files
61 into the `new_branch_name`.
62
63Then revert your files however you'd like in your old branch. The files listed
64in step 4 will be saved in `new_branch_name`
andybons3322f762015-08-24 21:37:0965
Quinten Yearsley893fd752017-07-13 15:34:4666### Method #3: Cherry-pick changes into review branches
andybons3322f762015-08-24 21:37:0967
andybons22afb312015-08-31 02:24:5168If you are systematic in creating separate local commits for independent
69changes, you can make a number of different changes in the same client and then
70cherry-pick each one into a separate review branch.
andybons3322f762015-08-24 21:37:0971
andybons22afb312015-08-31 02:24:51721. Make and commit a set of independent changes.
731. `git log` # see the hashes for each of your commits.
Thiago Perrottad65660fc2022-07-19 15:42:23741. repeat checkout, cherry-pick, upload steps for each change 1..n
Quinten Yearsley893fd752017-07-13 15:34:4675 1. `git new-branch review-changeN` Create a new review branch
andybons22afb312015-08-31 02:24:5176 tracking origin
77 1. `git cherry-pick <hash of change N>`
78 1. `git cl upload`
andybons3322f762015-08-24 21:37:0979
andybons22afb312015-08-31 02:24:5180If a change needs updating due to review comments, you can go back to your main
81working branch, update the commit, and re-cherry-pick it into the review branch.
82
831. `git checkout <working branch>`
841. Make changes.
851. If the commit you want to update is the most recent one:
86 1. `git commit --amend <files>`
871. If not:
88 1. `git commit <files>`
89 1. `git rebase -i origin` # use interactive rebase to squash the new
90 commit into the old one.
911. `git log` # observe new hash for the change
921. `git checkout review-changeN`
931. `git reset --hard` # remove the previous version of the change
Thiago Perrottad65660fc2022-07-19 15:42:23941. `git cherry-pick <new hash of change N>`
andybons22afb312015-08-31 02:24:51951. `git cl upload`
andybons3322f762015-08-24 21:37:0996
97## Sharing code between multiple machines
andybons22afb312015-08-31 02:24:5198
Quinten Yearsley893fd752017-07-13 15:34:4699Assume Windows computer named vista, and a Linux one named penguin.
100Prerequisite: both machines have git clones of the main git tree.
andybons22afb312015-08-31 02:24:51101
102```shell
andybons3322f762015-08-24 21:37:09103vista$ git remote add linux ssh://penguin/path/to/git/repo
104vista$ git fetch linux
105vista$ git branch -a # should show "linux/branchname"
106vista$ git checkout -b foobar linux/foobar
107vista$ hack hack hack; git commit -a
108vista$ git push linux # push branch back to linux
109penguin$ git reset --hard # update with new stuff in branch
110```
111
andybons22afb312015-08-31 02:24:51112Note that, by default, `gclient sync` will update all remotes. If your other
113machine (i.e., `penguin` in the above example) is not always available,
114`gclient sync` will timeout and fail trying to reach it. To fix this, you may
115exclude your machine from being fetched by default:
andybons3322f762015-08-24 21:37:09116
andybons22afb312015-08-31 02:24:51117 vista$ git config --bool remote.linux.skipDefaultUpdate true
andybons3322f762015-08-24 21:37:09118
Quinten Yearsley893fd752017-07-13 15:34:46119## Reverting commits
andybons3322f762015-08-24 21:37:09120
Quinten Yearsley893fd752017-07-13 15:34:46121The command `git revert X` patches in the inverse of a particular commit.
122Using this command is one way of making a revert:
andybons22afb312015-08-31 02:24:51123
124```shell
125git checkout origin # start with trunk
andybons22afb312015-08-31 02:24:51126git revert abcdef
Quinten Yearsley893fd752017-07-13 15:34:46127git cl upload
andybons3322f762015-08-24 21:37:09128```
129
andybons3322f762015-08-24 21:37:09130## Retrieving, or diffing against an old file revision
andybons22afb312015-08-31 02:24:51131
132Git works in terms of commits, not files. Thus, working with the history of a
133single file requires modified version of the show and diff commands.
134
135```shell
136# Find the commit you want in the file's commit log.
137git log path/to/file
138# This prints out the file contents at commit 123abc.
139git show 123abc:path/to/file
140# Diff the current version against path/to/file against the version at
141# path/to/file
142git diff 123abc -- path/to/file
andybons3322f762015-08-24 21:37:09143```
144
andybons22afb312015-08-31 02:24:51145When invoking `git show` or `git diff`, the `path/to/file` is **not relative the
146the current directory**. It must be the full path from the directory where the
147.git directory lives. This is different from invoking `git log` which
148understands relative paths.
andybons3322f762015-08-24 21:37:09149
andybons3322f762015-08-24 21:37:09150## Reusing a Git mirror
151
andybons22afb312015-08-31 02:24:51152If you have a nearby copy of a Git repo, you can quickly bootstrap your copy
153from that one then adjust it to point it at the real upstream one.
andybons3322f762015-08-24 21:37:09154
andybons22afb312015-08-31 02:24:511551. Clone a nearby copy of the code you want: `git clone coworker-machine:/path/to/repo`
1561. Change the URL your copy fetches from to point at the real git repo:
Thiago Perrottad65660fc2022-07-19 15:42:23157 `git remote set-url origin https://chromium.googlesource.com/chromium/src.git`
andybons22afb312015-08-31 02:24:511581. Update your copy: `git fetch`
1591. Delete any extra branches that you picked up in the initial clone:
160 `git prune origin`