android_webview
apps
ash
base
build
build_overrides
buildtools
cc
chrome
chromecast
chromeos
codelabs
components
content
courgette
crypto
dbus
device
docs
accessibility
autofill
design
enterprise
experiments
fuchsia
gpu
images
infra
intl
ios
lacros
linux
login
mac
media
memory
memory-infra
patterns
privacy
privacy_budget
process
security
speed
speed_metrics
standards
telemetry_extension
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_isolated_splits.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
dangling_ptr.md
dangling_ptr_guide.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_guarding_guidelines.md
flag_ownership.md
frame_trees.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
history_manipulation_intervention.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
modifying_session_history_serialization.md
mojo_and_services.md
mojo_ipc_conversion.md
mojo_testing.md
native_relocations.md
navbar.md
navigation-request-navigation-state.gv
navigation-request-navigation-state.png
navigation.md
navigation_concepts.md
network_traffic_annotations.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
process_model_and_site_isolation.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
rust.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_build_configuration.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_web
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
.vpython3
.yapfignore
ATL_OWNERS
AUTHORS
BUILD.gn
CODE_OF_CONDUCT.md
DEPS
DIR_METADATA
LICENSE
LICENSE.chromium_os
OWNERS
PRESUBMIT.py
PRESUBMIT_test.py
PRESUBMIT_test_mocks.py
README.md
WATCHLISTS
codereview.settings

Bug: 1231155 Change-Id: I0634229c0739c97ac0d7530dd58c33771958149a Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3042058 Reviewed-by: Toby Huang <tobyhuang@chromium.org> Commit-Queue: Andrew Williams <awillia@google.com> Cr-Commit-Position: refs/heads/master@{#903745}
142 lines
5.0 KiB
Markdown
142 lines
5.0 KiB
Markdown
# Git Tips
|
|
|
|
When using Git, there are a few tips that are particularly useful when working
|
|
on the Chromium codebase, especially due to its size.
|
|
|
|
[TOC]
|
|
|
|
## Remember the basic git convention:
|
|
|
|
git COMMAND [FLAGS] [ARGUMENTS]
|
|
|
|
Various git commands have underlying executable with a hyphenated name, such as
|
|
`git-grep`, but these can also be called via the `git` wrapper script as
|
|
`git grep` (and `man` should work either way too).
|
|
|
|
## Git references
|
|
|
|
The following resources can provide background on how Git works:
|
|
|
|
* [Git-SVN Crash Course](http://git-scm.com/course/svn.html) -- this crash
|
|
course is useful for Subversion users switching to Git.
|
|
* [Think Like (a) Git](http://think-like-a-git.net/) -- does a great job of
|
|
explaining the main purpose of Git operations.
|
|
* [Git User's Manual](http://schacon.github.com/git/user-manual.html) -- a
|
|
great resource to learn more about ho to use Git properly.
|
|
* [A Visual Git Reference](http://marklodato.github.com/visual-git-guide/index-en.html)
|
|
-- a resource that explains various Git operations for visual learners.
|
|
* [Git Cheat Sheet](http://cheat.errtheblog.com/s/git) -- now that you
|
|
understand Git, here's a cheat sheet to quickly remind you of all the
|
|
commands you need.
|
|
|
|
## Configuring the output of "git log"
|
|
|
|
By default, the date that "git log" displays is the "author date." In Chromium,
|
|
this generally corresponds to the date that the committed patch was last
|
|
uploaded. In most cases, however, the date that is of interest is the date that
|
|
the patch was committed in the tree. To configure "git log" to instead display
|
|
the latter date for your Chromium checkout, execute the following command:
|
|
|
|
```shell
|
|
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'
|
|
```
|
|
|
|
If you want to change *all* your repos (e.g., because you have multiple Chromium
|
|
checkouts and don't care about having the default for other repos), add
|
|
"--global" after "config" in the above command.
|
|
|
|
## Committing changes
|
|
|
|
For a simple workflow (always commit all changed files, don't keep local
|
|
revisions), the following script handles check; you may wish to call it `gci`
|
|
(git commit) or similar.
|
|
|
|
Amending a single revision is generally easier for various reasons, notably for
|
|
rebasing and for checking that CLs have been committed. However, if you don't
|
|
use local revisions (a local branch with multiple revisions), you should make
|
|
sure to upload revisions periodically to code review if you ever need to go to
|
|
an old version of a CL.
|
|
|
|
```bash
|
|
#!/bin/bash
|
|
# Commit all, amending if not initial commit.
|
|
if git status | grep -q "Your branch is ahead of 'origin/main' by 1 commit."
|
|
then
|
|
git commit --all --amend
|
|
else
|
|
git commit --all # initial, not amendment
|
|
fi
|
|
```
|
|
|
|
## Listing and changing branches
|
|
|
|
```shell
|
|
git branch # list branches
|
|
git checkout - # change to last branch
|
|
```
|
|
|
|
To quickly list the 5 most recent branches, add the following to `.gitconfig`
|
|
in the `[alias]` section:
|
|
|
|
```shell
|
|
last5 = "!git for-each-ref --sort=committerdate refs/heads/ \
|
|
--format='%(committerdate:short) %(refname:short)' | tail -5 | cut -c 12-"
|
|
```
|
|
|
|
A nicely color-coded list, sorted in descending order by date, can be made by
|
|
the following bash function:
|
|
|
|
```bash
|
|
git-list-branches-by-date() {
|
|
local current_branch=$(git rev-parse --symbolic-full-name --abbrev-ref HEAD)
|
|
local normal_text=$(echo -ne '\E[0m')
|
|
local yellow_text=$(echo -ne '\E[0;33m')
|
|
local yellow_bg=$(echo -ne '\E[7;33m')
|
|
git for-each-ref --sort=-committerdate \
|
|
--format=$' %(refname:short) \
|
|
\t%(committerdate:short)\t%(authorname)\t%(objectname:short)' \
|
|
refs/heads \
|
|
| column -t -s $'\t' -n \
|
|
| sed -E "s:^ (${current_branch}) :* ${yellow_bg}\1${normal_text} :" \
|
|
| sed -E "s:^ ([^ ]+): ${yellow_text}\1${normal_text}:"
|
|
}
|
|
```
|
|
|
|
## Searching
|
|
|
|
Use `git-grep` instead of `grep` and `git-ls-files` instead of `find`, as these
|
|
search only files in the index or _tracked_ files in the work tree, rather than
|
|
all files in the work tree.
|
|
|
|
Note that `git-ls-files` is rather simpler than `find`, so you'll often need to
|
|
use `xargs` instead of `-exec` if you want to process matching files.
|
|
|
|
## Global changes
|
|
|
|
To make global changes across the source tree, it's often easiest to use `sed`
|
|
with `git-ls-files`, using `-i` for in-place changing (this is generally safe,
|
|
as we don't use symlinks much, but there are few places that do). Remember that
|
|
you don't need to use `xargs`, since sed can take multiple input files. E.g., to
|
|
strip trailing whitespace from C++ and header files:
|
|
|
|
sed -i -E 's/\s+$//' $(git ls-files '*.cpp' '*.h')
|
|
|
|
|
|
You may also find `git-grep` useful for limiting the scope of your changes,
|
|
using `-l` for listing files.
|
|
|
|
sed -i -E '...' $(git grep -lw Foo '*.cpp' '*.h')
|
|
|
|
Remember that you can restrict sed actions to matching (or non-matching) lines.
|
|
For example, to skip lines with a line comment, use the following:
|
|
|
|
'\,//, ! s/foo/bar/g'
|
|
|
|
## Diffs
|
|
|
|
git diff --shortstat
|
|
|
|
Displays summary statistics, such as:
|
|
|
|
2104 files changed, 9309 insertions(+), 9309 deletions(-)
|