git-advanced

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Git Advanced Operations Skill

Git 高级操作技能

This skill provides comprehensive guidance on advanced git operations, sophisticated rebase strategies, commit surgery techniques, and complex history manipulation for experienced git users.
本技能为有经验的Git用户提供高级Git操作、复杂rebase策略、提交调整技巧、复杂历史操作相关的全面指导。

When to Use

适用场景

Activate this skill when:
  • Performing complex interactive rebases
  • Rewriting commit history
  • Splitting or combining commits
  • Advanced merge strategies
  • Cherry-picking across branches
  • Commit message editing in history
  • Author information changes
  • Complex conflict resolution
出现以下需求时启用本技能:
  • 执行复杂的交互式rebase
  • 重写提交历史
  • 拆分或合并提交
  • 使用高级合并策略
  • 跨分支执行cherry-pick
  • 编辑历史中的提交信息
  • 修改作者信息
  • 解决复杂冲突

Interactive Rebase Strategies

交互式Rebase策略

Basic Interactive Rebase

基础交互式Rebase

bash
undefined
bash
undefined

Rebase last 5 commits

Rebase last 5 commits

git rebase -i HEAD~5
git rebase -i HEAD~5

Rebase from specific commit

Rebase from specific commit

git rebase -i abc123^
git rebase -i abc123^

Rebase entire branch

Rebase entire branch

git rebase -i main
undefined
git rebase -i main
undefined

Rebase Commands

Rebase命令

bash
undefined
bash
undefined

Interactive rebase editor commands:

Interactive rebase editor commands:

p, pick = use commit

p, pick = use commit

r, reword = use commit, but edit commit message

r, reword = use commit, but edit commit message

e, edit = use commit, but stop for amending

e, edit = use commit, but stop for amending

s, squash = use commit, but meld into previous commit

s, squash = use commit, but meld into previous commit

f, fixup = like squash, but discard commit message

f, fixup = like squash, but discard commit message

x, exec = run command (the rest of the line) using shell

x, exec = run command (the rest of the line) using shell

d, drop = remove commit

d, drop = remove commit

undefined
undefined

Squashing Commits

压缩提交

bash
undefined
bash
undefined

Example: Squash last 3 commits

Example: Squash last 3 commits

git rebase -i HEAD~3
git rebase -i HEAD~3

In editor:

In editor:

pick abc123 feat: add user authentication squash def456 fix: resolve login bug squash ghi789 style: format code
pick abc123 feat: add user authentication squash def456 fix: resolve login bug squash ghi789 style: format code

Squash all commits in feature branch

Squash all commits in feature branch

git rebase -i main
git rebase -i main

Mark all except first as 'squash'

Mark all except first as 'squash'

undefined
undefined

Fixup Workflow

Fixup工作流

bash
undefined
bash
undefined

Create fixup commit automatically

Create fixup commit automatically

git commit --fixup=abc123
git commit --fixup=abc123

Autosquash during rebase

Autosquash during rebase

git rebase -i --autosquash main
git rebase -i --autosquash main

Set autosquash as default

Set autosquash as default

git config --global rebase.autosquash true
git config --global rebase.autosquash true

Example workflow:

Example workflow:

git log --oneline -5
git log --oneline -5

abc123 feat: add authentication

abc123 feat: add authentication

def456 feat: add authorization

def456 feat: add authorization

git commit --fixup=abc123 git rebase -i --autosquash HEAD~3
undefined
git commit --fixup=abc123 git rebase -i --autosquash HEAD~3
undefined

Reordering Commits

调整提交顺序

bash
undefined
bash
undefined

Interactive rebase

Interactive rebase

git rebase -i HEAD~5
git rebase -i HEAD~5

In editor, change order:

In editor, change order:

pick def456 feat: add database migration pick abc123 feat: add user model pick ghi789 feat: add API endpoints
pick def456 feat: add database migration pick abc123 feat: add user model pick ghi789 feat: add API endpoints

Reorder by moving lines:

Reorder by moving lines:

pick abc123 feat: add user model pick def456 feat: add database migration pick ghi789 feat: add API endpoints
undefined
pick abc123 feat: add user model pick def456 feat: add database migration pick ghi789 feat: add API endpoints
undefined

Splitting Commits

拆分提交

bash
undefined
bash
undefined

Start interactive rebase

Start interactive rebase

git rebase -i HEAD~3
git rebase -i HEAD~3

Mark commit to split with 'edit'

Mark commit to split with 'edit'

edit abc123 feat: add user and role features
edit abc123 feat: add user and role features

When rebase stops:

When rebase stops:

git reset HEAD^
git reset HEAD^

Stage and commit parts separately

Stage and commit parts separately

git add user.go git commit -m "feat: add user management"
git add role.go git commit -m "feat: add role management"
git add user.go git commit -m "feat: add user management"
git add role.go git commit -m "feat: add role management"

Continue rebase

Continue rebase

git rebase --continue
undefined
git rebase --continue
undefined

Editing Old Commits

编辑历史提交

bash
undefined
bash
undefined

Start interactive rebase

Start interactive rebase

git rebase -i HEAD~5
git rebase -i HEAD~5

Mark commit with 'edit'

Mark commit with 'edit'

edit abc123 feat: add authentication
edit abc123 feat: add authentication

When rebase stops, make changes

When rebase stops, make changes

git add modified-file.go git commit --amend --no-edit
git add modified-file.go git commit --amend --no-edit

Or change commit message

Or change commit message

git commit --amend
git commit --amend

Continue rebase

Continue rebase

git rebase --continue
undefined
git rebase --continue
undefined

Commit Surgery

提交调整

Amending Commits

修改提交

bash
undefined
bash
undefined

Amend last commit (add changes)

Amend last commit (add changes)

git add forgotten-file.go git commit --amend --no-edit
git add forgotten-file.go git commit --amend --no-edit

Amend commit message

Amend commit message

git commit --amend -m "fix: correct typo in feature"
git commit --amend -m "fix: correct typo in feature"

Amend author information

Amend author information

git commit --amend --author="John Doe john@example.com"
git commit --amend --author="John Doe john@example.com"

Amend date

Amend date

git commit --amend --date="2024-03-15 10:30:00"
undefined
git commit --amend --date="2024-03-15 10:30:00"
undefined

Changing Commit Messages

修改提交信息

bash
undefined
bash
undefined

Change last commit message

Change last commit message

git commit --amend
git commit --amend

Change older commit messages

Change older commit messages

git rebase -i HEAD~5
git rebase -i HEAD~5

Mark commits with 'reword'

Mark commits with 'reword'

Change commit message without opening editor

Change commit message without opening editor

git commit --amend -m "new message" --no-edit
undefined
git commit --amend -m "new message" --no-edit
undefined

Changing Multiple Authors

批量修改提交作者

bash
undefined
bash
undefined

Filter-branch (legacy method, use filter-repo instead)

Filter-branch (legacy method, use filter-repo instead)

git filter-branch --env-filter ' if [ "$GIT_COMMITTER_EMAIL" = "old@example.com" ]; then export GIT_COMMITTER_NAME="New Name" export GIT_COMMITTER_EMAIL="new@example.com" fi if [ "$GIT_AUTHOR_EMAIL" = "old@example.com" ]; then export GIT_AUTHOR_NAME="New Name" export GIT_AUTHOR_EMAIL="new@example.com" fi ' --tag-name-filter cat -- --branches --tags
git filter-branch --env-filter ' if [ "$GIT_COMMITTER_EMAIL" = "old@example.com" ]; then export GIT_COMMITTER_NAME="New Name" export GIT_COMMITTER_EMAIL="new@example.com" fi if [ "$GIT_AUTHOR_EMAIL" = "old@example.com" ]; then export GIT_AUTHOR_NAME="New Name" export GIT_AUTHOR_EMAIL="new@example.com" fi ' --tag-name-filter cat -- --branches --tags

Modern method with git-filter-repo

Modern method with git-filter-repo

git filter-repo --email-callback ' return email.replace(b"old@example.com", b"new@example.com") '
undefined
git filter-repo --email-callback ' return email.replace(b"old@example.com", b"new@example.com") '
undefined

Removing Files from History

从历史中删除文件

bash
undefined
bash
undefined

Remove file from all history

Remove file from all history

git filter-branch --tree-filter 'rm -f passwords.txt' HEAD
git filter-branch --tree-filter 'rm -f passwords.txt' HEAD

Better performance with index-filter

Better performance with index-filter

git filter-branch --index-filter 'git rm --cached --ignore-unmatch passwords.txt' HEAD
git filter-branch --index-filter 'git rm --cached --ignore-unmatch passwords.txt' HEAD

Modern method with git-filter-repo (recommended)

Modern method with git-filter-repo (recommended)

git filter-repo --path passwords.txt --invert-paths
git filter-repo --path passwords.txt --invert-paths

Remove large files

Remove large files

git filter-repo --strip-blobs-bigger-than 10M
undefined
git filter-repo --strip-blobs-bigger-than 10M
undefined

BFG Repo-Cleaner

BFG Repo-Cleaner工具

bash
undefined
bash
undefined

Install BFG

Install BFG

brew install bfg (macOS)

brew install bfg (macOS)

apt-get install bfg (Ubuntu)

apt-get install bfg (Ubuntu)

Remove files by name

Remove files by name

bfg --delete-files passwords.txt
bfg --delete-files passwords.txt

Remove large files

Remove large files

bfg --strip-blobs-bigger-than 50M
bfg --strip-blobs-bigger-than 50M

Replace passwords in history

Replace passwords in history

bfg --replace-text passwords.txt
bfg --replace-text passwords.txt

After BFG cleanup

After BFG cleanup

git reflog expire --expire=now --all git gc --prune=now --aggressive
undefined
git reflog expire --expire=now --all git gc --prune=now --aggressive
undefined

Advanced Cherry-Picking

高级Cherry-Pick

Basic Cherry-Pick

基础Cherry-Pick

bash
undefined
bash
undefined

Cherry-pick single commit

Cherry-pick single commit

git cherry-pick abc123
git cherry-pick abc123

Cherry-pick multiple commits

Cherry-pick multiple commits

git cherry-pick abc123 def456 ghi789
git cherry-pick abc123 def456 ghi789

Cherry-pick range of commits

Cherry-pick range of commits

git cherry-pick abc123..ghi789
git cherry-pick abc123..ghi789

Cherry-pick without committing (stage only)

Cherry-pick without committing (stage only)

git cherry-pick -n abc123
undefined
git cherry-pick -n abc123
undefined

Cherry-Pick with Conflicts

带冲突的Cherry-Pick

bash
undefined
bash
undefined

When conflicts occur

When conflicts occur

git cherry-pick abc123
git cherry-pick abc123

CONFLICT: resolve conflicts

CONFLICT: resolve conflicts

After resolving conflicts

After resolving conflicts

git add resolved-file.go git cherry-pick --continue
git add resolved-file.go git cherry-pick --continue

Or abort cherry-pick

Or abort cherry-pick

git cherry-pick --abort
git cherry-pick --abort

Skip current commit

Skip current commit

git cherry-pick --skip
undefined
git cherry-pick --skip
undefined

Cherry-Pick Options

Cherry-Pick可选参数

bash
undefined
bash
undefined

Edit commit message during cherry-pick

Edit commit message during cherry-pick

git cherry-pick -e abc123
git cherry-pick -e abc123

Sign-off cherry-picked commit

Sign-off cherry-picked commit

git cherry-pick -s abc123
git cherry-pick -s abc123

Keep original author date

Keep original author date

git cherry-pick --ff abc123
git cherry-pick --ff abc123

Apply changes without commit attribution

Apply changes without commit attribution

git cherry-pick -n abc123 git commit --author="New Author new@example.com"
undefined
git cherry-pick -n abc123 git commit --author="New Author new@example.com"
undefined

Mainline Selection for Merge Commits

合并提交的主线选择

bash
undefined
bash
undefined

Cherry-pick merge commit (specify parent)

Cherry-pick merge commit (specify parent)

git cherry-pick -m 1 abc123
git cherry-pick -m 1 abc123

-m 1 = use first parent (main branch)

-m 1 = use first parent (main branch)

-m 2 = use second parent (merged branch)

-m 2 = use second parent (merged branch)

Example workflow:

Example workflow:

git log --graph --oneline
git log --graph --oneline

* abc123 Merge pull request #123

* abc123 Merge pull request #123

|\

|\

| * def456 feat: feature commit

| * def456 feat: feature commit

* | ghi789 fix: main branch commit

* | ghi789 fix: main branch commit

To cherry-pick the merge keeping main branch changes:

To cherry-pick the merge keeping main branch changes:

git cherry-pick -m 1 abc123
undefined
git cherry-pick -m 1 abc123
undefined

Advanced Merging

高级合并

Merge Strategies

合并策略

bash
undefined
bash
undefined

Recursive merge (default)

Recursive merge (default)

git merge -s recursive branch-name
git merge -s recursive branch-name

Ours (keep our changes on conflict)

Ours (keep our changes on conflict)

git merge -s ours branch-name
git merge -s ours branch-name

Theirs (keep their changes on conflict)

Theirs (keep their changes on conflict)

git merge -s theirs branch-name
git merge -s theirs branch-name

Octopus (merge 3+ branches)

Octopus (merge 3+ branches)

git merge -s octopus branch1 branch2 branch3
git merge -s octopus branch1 branch2 branch3

Subtree merge

Subtree merge

git merge -s subtree branch-name
undefined
git merge -s subtree branch-name
undefined

Merge Strategy Options

合并策略参数

bash
undefined
bash
undefined

Ours (resolve conflicts with our version)

Ours (resolve conflicts with our version)

git merge -X ours branch-name
git merge -X ours branch-name

Theirs (resolve conflicts with their version)

Theirs (resolve conflicts with their version)

git merge -X theirs branch-name
git merge -X theirs branch-name

Ignore whitespace

Ignore whitespace

git merge -X ignore-space-change branch-name git merge -X ignore-all-space branch-name
git merge -X ignore-space-change branch-name git merge -X ignore-all-space branch-name

Patience algorithm (better conflict detection)

Patience algorithm (better conflict detection)

git merge -X patience branch-name
git merge -X patience branch-name

Renormalize line endings

Renormalize line endings

git merge -X renormalize branch-name
undefined
git merge -X renormalize branch-name
undefined

Three-Way Merge

三方合并

bash
undefined
bash
undefined

Standard three-way merge

Standard three-way merge

git merge feature-branch
git merge feature-branch

With custom merge message

With custom merge message

git merge feature-branch -m "Merge feature: add authentication"
git merge feature-branch -m "Merge feature: add authentication"

No fast-forward (always create merge commit)

No fast-forward (always create merge commit)

git merge --no-ff feature-branch
git merge --no-ff feature-branch

Fast-forward only (fail if merge commit needed)

Fast-forward only (fail if merge commit needed)

git merge --ff-only feature-branch
git merge --ff-only feature-branch

Squash merge (combine all commits)

Squash merge (combine all commits)

git merge --squash feature-branch git commit -m "feat: add complete authentication system"
undefined
git merge --squash feature-branch git commit -m "feat: add complete authentication system"
undefined

Advanced Conflict Resolution

高级冲突解决

Understanding Conflict Markers

理解冲突标记

<<<<<<< HEAD (Current Change)
int result = add(a, b);
=======
int sum = calculate(a, b);
>>>>>>> feature-branch (Incoming Change)
<<<<<<< HEAD (Current Change)
int result = add(a, b);
=======
int sum = calculate(a, b);
>>>>>>> feature-branch (Incoming Change)

Conflict Resolution Tools

冲突解决工具

bash
undefined
bash
undefined

Use mergetool

Use mergetool

git mergetool
git mergetool

Specify merge tool

Specify merge tool

git mergetool --tool=vimdiff git mergetool --tool=meld git mergetool --tool=kdiff3
git mergetool --tool=vimdiff git mergetool --tool=meld git mergetool --tool=kdiff3

Configure default merge tool

Configure default merge tool

git config --global merge.tool vimdiff git config --global mergetool.vimdiff.cmd 'vimdiff "$LOCAL" "$MERGED" "$REMOTE"'
git config --global merge.tool vimdiff git config --global mergetool.vimdiff.cmd 'vimdiff "$LOCAL" "$MERGED" "$REMOTE"'

Check out specific version

Check out specific version

git checkout --ours file.go # Keep our version git checkout --theirs file.go # Keep their version git checkout --merge file.go # Recreate conflict markers
undefined
git checkout --ours file.go # Keep our version git checkout --theirs file.go # Keep their version git checkout --merge file.go # Recreate conflict markers
undefined

Rerere (Reuse Recorded Resolution)

Rerere(复用已记录的冲突解决方案)

bash
undefined
bash
undefined

Enable rerere

Enable rerere

git config --global rerere.enabled true
git config --global rerere.enabled true

Rerere will automatically resolve previously seen conflicts

Rerere will automatically resolve previously seen conflicts

git merge feature-branch
git merge feature-branch

Conflict occurs and is resolved

Conflict occurs and is resolved

git add file.go git commit
git add file.go git commit

Later, same conflict:

Later, same conflict:

git merge another-branch
git merge another-branch

Rerere automatically applies previous resolution

Rerere automatically applies previous resolution

View rerere cache

View rerere cache

git rerere status git rerere diff
git rerere status git rerere diff

Clear rerere cache

Clear rerere cache

git rerere forget file.go git rerere clear
undefined
git rerere forget file.go git rerere clear
undefined

Interactive Staging

交互式暂存

Partial File Staging

部分文件暂存

bash
undefined
bash
undefined

Interactive staging

Interactive staging

git add -p file.go
git add -p file.go

Patch commands:

Patch commands:

y - stage this hunk

y - stage this hunk

n - do not stage this hunk

n - do not stage this hunk

q - quit (do not stage this and remaining hunks)

q - quit (do not stage this and remaining hunks)

a - stage this and all remaining hunks

a - stage this and all remaining hunks

d - do not stage this and all remaining hunks

d - do not stage this and all remaining hunks

s - split the current hunk into smaller hunks

s - split the current hunk into smaller hunks

e - manually edit the current hunk

e - manually edit the current hunk

undefined
undefined

Interactive Add

交互式添加

bash
undefined
bash
undefined

Interactive mode

Interactive mode

git add -i
git add -i

Commands:

Commands:

1: status - show paths with changes

1: status - show paths with changes

2: update - stage paths

2: update - stage paths

3: revert - unstage paths

3: revert - unstage paths

4: add untracked - stage untracked files

4: add untracked - stage untracked files

5: patch - partial staging

5: patch - partial staging

6: diff - show staged changes

6: diff - show staged changes

7: quit - exit

7: quit - exit

undefined
undefined

Partial Commits

部分提交

bash
undefined
bash
undefined

Stage part of file interactively

Stage part of file interactively

git add -p file.go
git add -p file.go

Create commit with partial changes

Create commit with partial changes

git commit -m "feat: add validation logic"
git commit -m "feat: add validation logic"

Stage remaining changes

Stage remaining changes

git add file.go git commit -m "feat: add error handling"
undefined
git add file.go git commit -m "feat: add error handling"
undefined

Advanced Reset Operations

高级Reset操作

Reset Modes

Reset模式

bash
undefined
bash
undefined

Soft reset (keep changes staged)

Soft reset (keep changes staged)

git reset --soft HEAD~1
git reset --soft HEAD~1

Mixed reset (keep changes unstaged, default)

Mixed reset (keep changes unstaged, default)

git reset --mixed HEAD1 git reset HEAD1
git reset --mixed HEAD1 git reset HEAD1

Hard reset (discard all changes)

Hard reset (discard all changes)

git reset --hard HEAD~1
git reset --hard HEAD~1

Reset to specific commit

Reset to specific commit

git reset --hard abc123
undefined
git reset --hard abc123
undefined

Reset vs Revert

Reset vs Revert

bash
undefined
bash
undefined

Reset (rewrites history, use for local changes)

Reset (rewrites history, use for local changes)

git reset --hard HEAD~3
git reset --hard HEAD~3

Revert (creates new commit, safe for shared history)

Revert (creates new commit, safe for shared history)

git revert HEAD git revert HEAD~3 git revert abc123..def456
undefined
git revert HEAD git revert HEAD~3 git revert abc123..def456
undefined

Advanced Branch Operations

高级分支操作

Branch from Specific Commit

从指定提交创建分支

bash
undefined
bash
undefined

Create branch from commit

Create branch from commit

git branch new-branch abc123 git checkout new-branch
git branch new-branch abc123 git checkout new-branch

Or in one command

Or in one command

git checkout -b new-branch abc123
git checkout -b new-branch abc123

Create branch from remote commit

Create branch from remote commit

git checkout -b local-branch origin/remote-branch
undefined
git checkout -b local-branch origin/remote-branch
undefined

Orphan Branches

孤立分支

bash
undefined
bash
undefined

Create orphan branch (no parent)

Create orphan branch (no parent)

git checkout --orphan new-root git rm -rf .
git checkout --orphan new-root git rm -rf .

Useful for gh-pages, documentation, etc.

Useful for gh-pages, documentation, etc.

echo "# Documentation" > README.md git add README.md git commit -m "docs: initialize documentation"
undefined
echo "# Documentation" > README.md git add README.md git commit -m "docs: initialize documentation"
undefined

Branch Tracking

分支跟踪

bash
undefined
bash
undefined

Set upstream branch

Set upstream branch

git branch -u origin/main
git branch -u origin/main

Push and set upstream

Push and set upstream

git push -u origin feature-branch
git push -u origin feature-branch

Change upstream

Change upstream

git branch -u origin/develop
git branch -u origin/develop

View tracking information

View tracking information

git branch -vv
undefined
git branch -vv
undefined

Advanced Stash Operations

高级Stash操作

Stash Specific Files

暂存指定文件

bash
undefined
bash
undefined

Stash specific files

Stash specific files

git stash push -m "WIP: feature work" file1.go file2.go
git stash push -m "WIP: feature work" file1.go file2.go

Stash with pathspec

Stash with pathspec

git stash push -p
git stash push -p

Stash untracked files

Stash untracked files

git stash -u
git stash -u

Stash including ignored files

Stash including ignored files

git stash -a
undefined
git stash -a
undefined

Stash to Branch

从Stash创建分支

bash
undefined
bash
undefined

Create branch from stash

Create branch from stash

git stash branch new-branch stash@{0}
git stash branch new-branch stash@{0}

Creates new branch and applies stash

Creates new branch and applies stash

git stash branch feature-work
undefined
git stash branch feature-work
undefined

Partial Stash Application

部分应用Stash

bash
undefined
bash
undefined

Apply specific files from stash

Apply specific files from stash

git checkout stash@{0} -- file.go
git checkout stash@{0} -- file.go

Apply stash without dropping

Apply stash without dropping

git stash apply stash@{0}
git stash apply stash@{0}

Apply and drop

Apply and drop

git stash pop stash@{0}
undefined
git stash pop stash@{0}
undefined

Submodule Management

子模块管理

Advanced Submodule Operations

高级子模块操作

bash
undefined
bash
undefined

Update submodule to specific commit

Update submodule to specific commit

cd submodule-dir git checkout abc123 cd .. git add submodule-dir git commit -m "chore: update submodule to version 1.2.3"
cd submodule-dir git checkout abc123 cd .. git add submodule-dir git commit -m "chore: update submodule to version 1.2.3"

Update all submodules to latest

Update all submodules to latest

git submodule update --remote --merge
git submodule update --remote --merge

Update specific submodule

Update specific submodule

git submodule update --remote --merge path/to/submodule
git submodule update --remote --merge path/to/submodule

Run command in all submodules

Run command in all submodules

git submodule foreach 'git checkout main' git submodule foreach 'git pull'
git submodule foreach 'git checkout main' git submodule foreach 'git pull'

Clone with submodules at specific depth

Clone with submodules at specific depth

git clone --recurse-submodules --depth 1 repo-url
undefined
git clone --recurse-submodules --depth 1 repo-url
undefined

Submodule to Subtree Migration

子模块迁移为子树

bash
undefined
bash
undefined

Remove submodule

Remove submodule

git submodule deinit path/to/submodule git rm path/to/submodule rm -rf .git/modules/path/to/submodule
git submodule deinit path/to/submodule git rm path/to/submodule rm -rf .git/modules/path/to/submodule

Add as subtree

Add as subtree

git subtree add --prefix=path/to/submodule
https://github.com/user/repo.git main --squash
git subtree add --prefix=path/to/submodule
https://github.com/user/repo.git main --squash

Update subtree

Update subtree

git subtree pull --prefix=path/to/submodule
https://github.com/user/repo.git main --squash
undefined
git subtree pull --prefix=path/to/submodule
https://github.com/user/repo.git main --squash
undefined

Advanced Log and History

高级日志与历史查询

Custom Log Formatting

自定义日志格式

bash
undefined
bash
undefined

One-line format with custom fields

One-line format with custom fields

git log --pretty=format:"%h - %an, %ar : %s"
git log --pretty=format:"%h - %an, %ar : %s"

Full custom format

Full custom format

git log --pretty=format:"%C(yellow)%h%C(reset) %C(blue)%ad%C(reset) %C(green)%an%C(reset) %s" --date=short
git log --pretty=format:"%C(yellow)%h%C(reset) %C(blue)%ad%C(reset) %C(green)%an%C(reset) %s" --date=short

Aliases for common formats

Aliases for common formats

git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative"
undefined
git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative"
undefined

Finding Lost Commits

查找丢失的提交

bash
undefined
bash
undefined

Find commits not in any branch

Find commits not in any branch

git log --all --oneline --no-walk --decorate $(git fsck --no-reflog | grep commit | cut -d' ' -f3)
git log --all --oneline --no-walk --decorate $(git fsck --no-reflog | grep commit | cut -d' ' -f3)

Find dangling commits

Find dangling commits

git fsck --lost-found
git fsck --lost-found

Search commit messages

Search commit messages

git log --all --grep="search term"
git log --all --grep="search term"

Find commits by author

Find commits by author

git log --author="John Doe"
git log --author="John Doe"

Find commits modifying specific code

Find commits modifying specific code

git log -S "function_name" git log -G "regex_pattern"
undefined
git log -S "function_name" git log -G "regex_pattern"
undefined

Bisect Automation

Bisect自动化

bash
undefined
bash
undefined

Start bisect

Start bisect

git bisect start git bisect bad HEAD git bisect good abc123
git bisect start git bisect bad HEAD git bisect good abc123

Automate bisect with script

Automate bisect with script

git bisect run ./test.sh
git bisect run ./test.sh

test.sh example:

test.sh example:

#!/bin/bash make && make test exit $?
#!/bin/bash make && make test exit $?

Bisect will automatically find first bad commit

Bisect will automatically find first bad commit

undefined
undefined

Best Practices

最佳实践

  1. Backup Before History Rewriting: Create backup branch before complex operations
  2. Never Rewrite Published History: Only rewrite local commits
  3. Communicate Rebases: Inform team when force-pushing
  4. Use Descriptive Commit Messages: Even during interactive rebase
  5. Test After Rebase: Ensure code still works after history changes
  6. Prefer Rebase for Local Branches: Keep history linear
  7. Use Merge for Shared Branches: Preserve complete history
  8. Sign Important Commits: Use GPG signing for releases
  9. Document Complex Operations: Leave comments for future reference
  10. Know When to Stop: Sometimes merge commits are clearer than rebased history
  1. 重写历史前备份: 执行复杂操作前先创建备份分支
  2. 永远不要重写已发布的历史: 仅对本地提交执行重写操作
  3. 变基后同步信息: 强制推送前告知团队成员
  4. 使用清晰的提交信息: 即便是交互式rebase过程中也要遵守
  5. 变基后测试: 确保历史修改后代码仍能正常运行
  6. 本地分支优先使用rebase: 保持历史线性清晰
  7. 共享分支优先使用merge: 保留完整的历史记录
  8. 重要提交签名: 发布版本使用GPG签名
  9. 复杂操作留记录: 为后续维护留下注释说明
  10. 知道何时停止: 有时候合并提交比变基后的历史更易理解

Resources

资源

Additional guides and examples are available in the
assets/
directory:
  • examples/
    - Complex rebase and merge scenarios
  • scripts/
    - Automation scripts for common operations
  • workflows/
    - Advanced workflow patterns
See
references/
directory for:
  • Git internals documentation
  • Advanced rebasing strategies
  • Filter-branch alternatives
  • Conflict resolution techniques
assets/
目录下提供了更多指南和示例:
  • examples/
    - 复杂rebase和合并场景示例
  • scripts/
    - 常见操作的自动化脚本
  • workflows/
    - 高级工作流模式
references/
目录可查看:
  • Git内部原理文档
  • 高级rebase策略
  • filter-branch替代方案
  • 冲突解决技巧