android_webview
apps
ash
base
build
build_overrides
buildtools
cc
chrome
chromecast
chromeos
cloud_print
codelabs
components
content
courgette
crypto
dbus
device
docs
accessibility
autofill
design
enterprise
fuchsia
gpu
images
infra
intl
ios
linux
login
mac
media
memory
memory-infra
patterns
privacy
privacy_budget
process
security
speed
speed_metrics
standards
testing
ui
updater
webapps
workflow
DIR_METADATA
OWNERS
README.md
accessibility.md
ad_tagging.md
adding_to_third_party.md
android_accessing_cpp_enums_in_java.md
android_accessing_cpp_features_in_java.md
android_accessing_cpp_switches_in_java.md
android_build_instructions.md
android_cast_build_instructions.md
android_debugging_instructions.md
android_dynamic_feature_modules.md
android_emulator.md
android_logging.md
android_native_libraries.md
android_studio.md
angle_in_chromium.md
api_keys.md
asan.md
atom.md
bitmap_pipeline.md
branch_sheriff.md
building_old_revisions.md
callback.md
ccache_mac.md
chrome_os_logging.md
chrome_settings.md
chrome_untrusted.md
chromedriver_status.md
chromeos_build_instructions.md
chromeos_glossary.md
chromium_browser_vs_google_chrome.md
cipd_and_3pp.md
cl_respect.md
clang.md
clang_code_coverage_wrapper.md
clang_format.md
clang_sheriffing.md
clang_static_analyzer.md
clang_tidy.md
clang_tool_refactoring.md
clangd.md
clion.md
closure_compilation.md
cocoa_tips_and_tricks.md
code_review_owners.md
code_reviews.md
commit_checklist.md
component_build.md
configuration.md
contributing.md
cq_quick_run.md
cr_respect.md
cr_user_manual.md
cross_platform_ui.md
cygwin_dll_remapping_failure.md
dbus_mojo_connection_service.md
debugging_with_crash_keys.md
deterministic_builds.md
disassemble_code.md
documentation_best_practices.md
documentation_guidelines.md
early-hints.md
eclipse.md
emacs.md
erc_irc.md
flag_expiry.md
flag_ownership.md
gdbinit.md
get_the_code.md
git_cookbook.md
git_tips.md
google_chrome_branded_builds.md
google_play_services.md
graphical_debugging_aid_chromium_views.md
gwp_asan.md
how_cc_works.md
how_to_add_your_feature_flag.md
how_to_extend_web_test_framework.md
idn.md
initialize_blink_features.md
inlined_stack_traces.md
installation_at_vmware.md
ios_build_instructions.md
ios_infra.md
ios_voiceover.md
kiosk_mode.md
lacros.md
life_of_a_frame.md
lldbinit.md
mac_arm64.md
mac_build_instructions.md
mac_lld.md
mojo_and_services.md
mojo_ipc_conversion.md
mojo_testing.md
native_relocations.md
navigation-request-navigation-state.gv
navigation-request-navigation-state.png
navigation.md
navigation_concepts.md
network_traffic_annotations.md
new_port_policy.md
no_sources_assignment_filter.md
optimizing_web_uis.md
origin_trials_integration.md
ozone_overview.md
parsing_test_results.md
pgo.md
piranha_plant.md
profiling.md
profiling_content_shell_on_android.md
proxy_auto_config.md
python3_migration.md
qtcreator.md
release_branch_guidance.md
render-frame-host-lifecycle-state.gv
render-frame-host-lifecycle-state.png
render_document.md
seccomp_sandbox_crash_dumping.md
servicification.md
session_history.md
sheriff.md
shutdown.md
special_case_urls.md
static_initializers.md
sublime_ide.md
system_hardening_features.md
tab_helpers.md
threading_and_tasks.md
threading_and_tasks_faq.md
threading_and_tasks_testing.md
toolchain_support.md
tour_of_luci_ui.md
tpm_quick_ref.md
translation_screenshots.md
trusted_types_on_webui.md
updating_clang.md
updating_clang_format_binaries.md
use_counter_wiki.md
useful_urls.md
user_data_dir.md
user_data_storage.md
user_handle_mapping.md
vanilla_msysgit_workflow.md
vscode.md
vscode_python.md
webui_explainer.md
webui_in_chrome.md
webui_in_components.md
webview_policies.md
win_cross.md
win_order_files.md
windows_build_instructions.md
windows_native_window_occlusion_tracking.md
windows_pwa_integration.md
windows_shortcut_and_taskbar_handling.md
windows_split_dll.md
windows_virtual_desktop_handling.md
wmax_tokens.md
working_remotely_with_android.md
writing_clang_plugins.md
extensions
fuchsia
gin
google_apis
google_update
gpu
headless
infra
ios
ipc
media
mojo
native_client_sdk
net
pdf
ppapi
printing
remoting
rlz
sandbox
services
skia
sql
storage
styleguide
testing
third_party
tools
ui
url
weblayer
.clang-format
.clang-tidy
.eslintrc.js
.git-blame-ignore-revs
.gitattributes
.gitignore
.gn
.mailmap
.rustfmt.toml
.vpython
.vpython3
.yapfignore
AUTHORS
BUILD.gn
CODE_OF_CONDUCT.md
DEPS
DIR_METADATA
ENG_REVIEW_OWNERS
LICENSE
LICENSE.chromium_os
OWNERS
PRESUBMIT.py
PRESUBMIT_test.py
PRESUBMIT_test_mocks.py
README.md
WATCHLISTS
codereview.settings

Change-Id: I4cdaa39a0832ad368d282967cc7375756a06238c Reviewed-on: https://chromium-review.googlesource.com/569065 Reviewed-by: Robbie Iannucci <iannucci@chromium.org> Commit-Queue: Quinten Yearsley <qyearsley@chromium.org> Cr-Commit-Position: refs/heads/master@{#486387}
161 lines
6.4 KiB
Markdown
161 lines
6.4 KiB
Markdown
# Git Cookbook
|
|
|
|
A collection of git recipes to do common git tasks.
|
|
|
|
See also [Git Tips](git_tips.md).
|
|
|
|
[TOC]
|
|
|
|
## Introduction
|
|
|
|
This is designed to be a cookbook for common command sequences/tasks relating to
|
|
git, git-cl, and how they work with Chromium development. It might be a little
|
|
light on explanations.
|
|
|
|
If you are new to git, or do not have much experience with a distributed version
|
|
control system, you should also check out
|
|
[The Git Community Book](http://book.git-scm.com/) for an overview of basic git
|
|
concepts and general git usage. Knowing what git means by branches, commits,
|
|
reverts, and resets (as opposed to what SVN means by them) will help make the
|
|
following much more understandable.
|
|
|
|
## Chromium-specific Git Extensions
|
|
|
|
Chromium ships a large number of git extensions in depot_tools. Some (like
|
|
`git cl`) are required for the Chromium development workflow, while others
|
|
(like `git map-branches`) are simple utilities to make your life easier.
|
|
Please take a look at the full
|
|
[depot_tools tutorial](https://commondatastorage.googleapis.com/chrome-infra-docs/flat/depot_tools/docs/html/depot_tools_tutorial.html),
|
|
and at the extensive
|
|
[man pages](https://commondatastorage.googleapis.com/chrome-infra-docs/flat/depot_tools/docs/html/depot_tools.html)
|
|
for all the extensions.
|
|
|
|
## Excluding file(s) from git-cl, while preserving them for later use
|
|
|
|
Since git-cl assumes that the diff between your current branch and its tracking
|
|
branch is what should be used for the CL, the goal is to remove the unwanted
|
|
files from the current branch, and preserve them in another branch.
|
|
|
|
### Method #1: Reset your current branch, and selectively commit files
|
|
|
|
1. `git log` See the list of your commits. Find the hash of the last commit
|
|
before your changes.
|
|
1. `git reset --soft abcdef` where abcdef is the hash found in the step above.
|
|
1. `git commit <files_for_this_cl> -m "files to upload"` commit the files you
|
|
want included in the CL here.
|
|
1. `git new-branch new_branch_name` Create a new branch for the
|
|
files that you want to exclude.
|
|
1. `git commit -a -m "preserved files"` Commit the rest of the files.
|
|
|
|
### Method #2: Create a new branch, reset, then commit files to preserve
|
|
|
|
This method creates a new branch from your current one to preserve your changes.
|
|
The commits on the new branch are undone, and then only the files you want to
|
|
preserve are recommitted.
|
|
|
|
1. `git checkout -b new_branch_name` This preserves your old files.
|
|
1. `git log` See the list of your commits. Find the hash of the last commit
|
|
before your changes.
|
|
1. `git reset --soft abcdef` Where abcdef is the hash found in the step above.
|
|
1. `git commit <files_to_preserve> -m "preserved files"` Commit the found files
|
|
into the `new_branch_name`.
|
|
|
|
Then revert your files however you'd like in your old branch. The files listed
|
|
in step 4 will be saved in `new_branch_name`
|
|
|
|
### Method #3: Cherry-pick changes into review branches
|
|
|
|
If you are systematic in creating separate local commits for independent
|
|
changes, you can make a number of different changes in the same client and then
|
|
cherry-pick each one into a separate review branch.
|
|
|
|
1. Make and commit a set of independent changes.
|
|
1. `git log` # see the hashes for each of your commits.
|
|
1. repeat checkout, cherry-pick, upload steps for each change1..n
|
|
1. `git new-branch review-changeN` Create a new review branch
|
|
tracking origin
|
|
1. `git cherry-pick <hash of change N>`
|
|
1. `git cl upload`
|
|
|
|
If a change needs updating due to review comments, you can go back to your main
|
|
working branch, update the commit, and re-cherry-pick it into the review branch.
|
|
|
|
1. `git checkout <working branch>`
|
|
1. Make changes.
|
|
1. If the commit you want to update is the most recent one:
|
|
1. `git commit --amend <files>`
|
|
1. If not:
|
|
1. `git commit <files>`
|
|
1. `git rebase -i origin` # use interactive rebase to squash the new
|
|
commit into the old one.
|
|
1. `git log` # observe new hash for the change
|
|
1. `git checkout review-changeN`
|
|
1. `git reset --hard` # remove the previous version of the change
|
|
1. `cherry-pick <new hash of change N>`
|
|
1. `git cl upload`
|
|
|
|
## Sharing code between multiple machines
|
|
|
|
Assume Windows computer named vista, and a Linux one named penguin.
|
|
Prerequisite: both machines have git clones of the main git tree.
|
|
|
|
```shell
|
|
vista$ git remote add linux ssh://penguin/path/to/git/repo
|
|
vista$ git fetch linux
|
|
vista$ git branch -a # should show "linux/branchname"
|
|
vista$ git checkout -b foobar linux/foobar
|
|
vista$ hack hack hack; git commit -a
|
|
vista$ git push linux # push branch back to linux
|
|
penguin$ git reset --hard # update with new stuff in branch
|
|
```
|
|
|
|
Note that, by default, `gclient sync` will update all remotes. If your other
|
|
machine (i.e., `penguin` in the above example) is not always available,
|
|
`gclient sync` will timeout and fail trying to reach it. To fix this, you may
|
|
exclude your machine from being fetched by default:
|
|
|
|
vista$ git config --bool remote.linux.skipDefaultUpdate true
|
|
|
|
## Reverting commits
|
|
|
|
The command `git revert X` patches in the inverse of a particular commit.
|
|
Using this command is one way of making a revert:
|
|
|
|
```shell
|
|
git checkout origin # start with trunk
|
|
git revert abcdef
|
|
git cl upload
|
|
```
|
|
|
|
## Retrieving, or diffing against an old file revision
|
|
|
|
Git works in terms of commits, not files. Thus, working with the history of a
|
|
single file requires modified version of the show and diff commands.
|
|
|
|
```shell
|
|
# Find the commit you want in the file's commit log.
|
|
git log path/to/file
|
|
# This prints out the file contents at commit 123abc.
|
|
git show 123abc:path/to/file
|
|
# Diff the current version against path/to/file against the version at
|
|
# path/to/file
|
|
git diff 123abc -- path/to/file
|
|
```
|
|
|
|
When invoking `git show` or `git diff`, the `path/to/file` is **not relative the
|
|
the current directory**. It must be the full path from the directory where the
|
|
.git directory lives. This is different from invoking `git log` which
|
|
understands relative paths.
|
|
|
|
## Reusing a Git mirror
|
|
|
|
If you have a nearby copy of a Git repo, you can quickly bootstrap your copy
|
|
from that one then adjust it to point it at the real upstream one.
|
|
|
|
1. Clone a nearby copy of the code you want: `git clone coworker-machine:/path/to/repo`
|
|
1. Change the URL your copy fetches from to point at the real git repo:
|
|
`git set-url origin https://chromium.googlesource.com/chromium/src.git`
|
|
1. Update your copy: `git fetch`
|
|
1. Delete any extra branches that you picked up in the initial clone:
|
|
`git prune origin`
|