Git — Advanced Techniques

If you like this project, consider supporting me on Buy Me a Coffee ☕️


tags:

Description: Advanced Git operations for power users — worktrees, bisect, reflog, submodules, interactive rebase, hooks, and repository maintenance. This cheatsheet covers techniques beyond basic workflow for experienced developers and DevOps engineers. Продвинутые операции Git для опытных пользователей — worktrees, bisect, reflog, подмодули, интерактивный rebase, хуки и обслуживание репозитория.

Prerequisites: Familiarity with Git Basics. Role: Developer / DevOps / Release Engineer


🌳 Worktrees

Create Worktree

git worktree add ../feature feature-branch    # Create worktree for branch / Создать worktree для ветки
git worktree add -b new-feature ../feature    # Create new branch and worktree / Создать новую ветку и worktree
git worktree add --detach ../temp <COMMIT>    # Detached HEAD worktree / Worktree с отсоединённым HEAD

List & Remove

git worktree list                             # List all worktrees / Список всех worktrees
git worktree remove ../feature                # Remove worktree / Удалить worktree
git worktree prune                            # Clean up stale worktrees / Очистить устаревшие worktrees

[!TIP] Worktrees let you work on multiple branches simultaneously without stashing or switching. Ideal for hotfixes while developing features. Worktrees позволяют работать с несколькими ветками одновременно без stash или switch. Идеально для хотфиксов во время разработки.


🔍 Bisect

Basic Bisect

git bisect start                              # Start bisect / Начать bisect
git bisect bad                                # Mark current as bad / Отметить текущий как плохой
git bisect good <COMMIT>                      # Mark commit as good / Отметить коммит как хороший
git bisect reset                              # End bisect / Закончить bisect

Automated Bisect

git bisect start HEAD v1.0                    # Start with range / Начать с диапазоном
git bisect run ./test.sh                      # Automate with script / Автоматизировать со скриптом
git bisect skip                               # Skip current commit / Пропустить текущий коммит

📜 Reflog

View Reflog

git reflog                                    # Show reflog / Показать reflog
git reflog show HEAD                          # Show HEAD reflog / Показать reflog HEAD
git reflog show main                          # Show branch reflog / Показать reflog ветки

Restore from Reflog

git reset --hard HEAD@{1}                     # Restore to previous HEAD / Восстановить к предыдущему HEAD
git reset --hard HEAD@{2.hours.ago}           # Restore to 2 hours ago / Восстановить к 2 часам назад
git reflog expire --expire=now --all          # Clear reflog / Очистить reflog

[!CAUTION] git reflog expire --expire=now --all permanently removes all reflog entries and makes recovery impossible. git reflog expire --expire=now --all безвозвратно удаляет все записи reflog и делает восстановление невозможным.


📦 Submodules

Add Submodule

git submodule add <REPO_URL> path/to/submodule  # Add submodule / Добавить подмодуль
git submodule add -b branch <REPO_URL> path   # Add specific branch / Добавить конкретную ветку

Update Submodules

git submodule update --init --recursive       # Initialize submodules / Инициализировать подмодули
git submodule update --remote                 # Update to latest / Обновить до последних
git submodule foreach git pull origin main    # Pull all submodules / Подтянуть все подмодули

Remove Submodule

git submodule deinit path/to/submodule        # Deinitialize submodule / Деинициализировать подмодуль
git rm path/to/submodule                      # Remove submodule / Удалить подмодуль
rm -rf .git/modules/path/to/submodule         # Clean up git directory / Очистить git директорию

[!NOTE] Consider using git subtree as an alternative to submodules for simpler dependency management. Subtrees embed the code directly in your repo. Рассмотрите git subtree как альтернативу подмодулям для более простого управления зависимостями.


🍒 Cherry Pick & Rebase

Cherry Pick

git cherry-pick <COMMIT>                      # Apply commit / Применить коммит
git cherry-pick <COMMIT1> <COMMIT2>           # Apply multiple / Применить несколько
git cherry-pick <COMMIT1>..<COMMIT2>          # Apply range / Применить диапазон
git cherry-pick --continue                    # Continue after conflict / Продолжить после конфликта
git cherry-pick --abort                       # Abort cherry-pick / Отменить cherry-pick

Interactive Rebase

git rebase -i HEAD~3                          # Rebase last 3 commits / Rebase последних 3 коммитов
git rebase -i main                            # Rebase onto main / Rebase на main
git rebase --continue                         # Continue after resolving / Продолжить после разрешения
git rebase --abort                            # Abort rebase / Отменить rebase
git rebase --skip                             # Skip current commit / Пропустить текущий коммит

Rebase Options

git rebase -i --autosquash HEAD~5             # Auto-squash fixup commits / Автослияние fixup коммитов
git rebase --onto main feature~3 feature      # Advanced rebase / Продвинутый rebase

Interactive Rebase Commands

Command Short Description (EN / RU)
pick p Use commit as-is / Использовать коммит как есть
reword r Edit commit message / Изменить сообщение коммита
edit e Stop for amending / Остановить для изменения
squash s Meld into previous commit / Объединить с предыдущим коммитом
fixup f Like squash, discard message / Как squash, отбросить сообщение
drop d Remove commit / Удалить коммит

[!WARNING] Never rebase commits that have been pushed to a shared remote. This rewrites history and causes conflicts for collaborators. Никогда не делайте rebase коммитов, отправленных в общий удалённый репозиторий. Это перезаписывает историю.


💾 Stash

Basic Stash

git stash                                     # Stash changes / Отложить изменения
git stash push -m "description"               # Stash with message / Отложить с сообщением
git stash -u                                  # Include untracked files / Включить неотслеживаемые файлы
git stash -a                                  # Include all (ignored too) / Включить все

List & Apply

git stash list                                # List stashes / Список stash
git stash show                                # Show latest stash / Показать последний stash
git stash show -p stash@{1}                   # Show specific stash diff / Показать diff конкретного stash
git stash apply                               # Apply latest stash / Применить последний stash
git stash apply stash@{2}                     # Apply specific stash / Применить конкретный stash
git stash pop                                 # Apply and remove stash / Применить и удалить stash

Manage Stashes

git stash drop stash@{1}                      # Remove specific stash / Удалить конкретный stash
git stash clear                               # Remove all stashes / Удалить все stash
git stash branch new-branch stash@{0}         # Create branch from stash / Создать ветку из stash

🪝 Hooks

Git Hooks Location

.git/hooks/

ls .git/hooks/                                # List available hooks / Список доступных хуков
Hook File Trigger (EN / RU)
pre-commit Before commit / Перед коммитом
pre-push Before push / Перед push
commit-msg Validate commit message / Проверка сообщения коммита
post-merge After merge / После слияния
pre-rebase Before rebase / Перед rebase
post-checkout After checkout / После checkout
prepare-commit-msg Before editor opens for commit message / Перед открытием редактора для сообщения

Sample Pre-Commit Hook

#!/bin/bash
# Run tests before commit
npm test || exit 1

Enable Hooks

chmod +x .git/hooks/pre-commit                # Make executable / Сделать исполняемым
git config core.hooksPath custom-hooks/       # Custom hooks path / Пользовательский путь хуков

[!TIP] Use tools like husky (Node.js) or pre-commit (Python) for cross-platform, repository-level hook management. Используйте husky (Node.js) или pre-commit (Python) для кроссплатформенного управления хуками.


🔬 Advanced Techniques

Filter-Branch

git filter-branch --tree-filter 'rm -f passwords.txt' HEAD  # Remove file from history / Удалить файл из истории
git filter-branch --env-filter 'export GIT_AUTHOR_NAME="<NAME>"' HEAD  # Rewrite author / Переписать автора

[!CAUTION] filter-branch rewrites the entire repository history. Use git-filter-repo instead for better performance and safety. filter-branch перезаписывает всю историю репозитория. Используйте git-filter-repo для лучшей производительности.

BFG Repo-Cleaner / BFG

bfg --delete-files passwords.txt              # Remove file / Удалить файл
bfg --strip-blobs-bigger-than 10M             # Remove large files / Удалить большие файлы

Find Large Files

git rev-list --objects --all | git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' | awk '/^blob/ {print substr($0,6)}' | sort -n -k 2 | tail -10

Blame & Log

git blame file.txt                            # Show who changed each line / Показать кто изменил каждую строку
git blame -L 10,20 file.txt                   # Blame specific lines / Обвинить конкретные строки
git log --follow file.txt                     # Follow file renames / Следовать за переименованиями
git log -p -S "function"                      # Search for code changes / Искать изменения кода
git log --graph --oneline --all               # Visual graph / Визуальный график

Diff Advanced

git diff HEAD~3..HEAD                         # Diff range / Diff диапазона
git diff --stat                               # Show statistics / Показать статистику
git diff --word-diff                          # Word-level diff / Diff на уровне слов
git diff main...feature                       # Three-dot diff / Трёхточечный diff

Archive

git archive --format=zip --output=repo.zip HEAD  # Create ZIP archive / Создать ZIP архив
git archive --format=tar HEAD | gzip > repo.tar.gz  # Create tar.gz / Создать tar.gz

Sparse Checkout

git sparse-checkout init                      # Initialize sparse checkout / Инициализировать sparse checkout
git sparse-checkout set dir1 dir2             # Checkout specific directories / Выборка конкретных директорий

Maintenance

git gc                                        # Garbage collection / Сборка мусора
git gc --aggressive                           # Aggressive GC / Агрессивная сборка мусора
git fsck                                      # Check integrity / Проверить целостность
git prune                                     # Prune unreachable objects / Очистить недостижимые объекты

🌟 Real-World Examples

Fix Commit Message

git commit --amend -m "New message"           # Amend last commit message / Изменить последнее сообщение
git rebase -i HEAD~3                          # Reword older commit / Переписать старый коммит

Undo Last Commit

git reset --soft HEAD~1                       # Keep changes staged / Оставить изменения staged
git reset --mixed HEAD~1                      # Keep changes unstaged / Оставить изменения unstaged
git reset --hard HEAD~1                       # Discard changes / Отбросить изменения

Squash Commits

git rebase -i HEAD~3                          # Interactive rebase / Интерактивный rebase
# Change 'pick' to 'squash' for commits to squash

Split Commit

git rebase -i HEAD~3
# Mark commit for 'edit'
git reset HEAD~
git add file1.txt && git commit -m "Part 1"
git add file2.txt && git commit -m "Part 2"
git rebase --continue

Emergency Recovery

git reflog                                    # Find lost commit / Найти потерянный коммит
git reset --hard <COMMIT>                     # Restore / Восстановить

💡 Best Practices


🔧 Useful Aliases

git config --global alias.lg "log --graph --oneline --all"
git config --global alias.st "status -sb"
git config --global alias.co "checkout"
git config --global alias.br "branch"
git config --global alias.unstage "reset HEAD --"
git config --global alias.last "log -1 HEAD"
git config --global alias.amend "commit --amend --no-edit"

Official Documentation

On this page

sysadmin Git Advanced Techniques Cheatsheet 🌳 Worktrees Create Worktree List &amp; Remove 🔍 Bisect Basic Bisect Automated Bisect 📜 Reflog View Reflog Restore from Reflog 📦 Submodules Add Submodule Update Submodules Remove Submodule 🍒 Cherry Pick &amp; Rebase Cherry Pick Interactive Rebase Rebase Options Interactive Rebase Commands 💾 Stash Basic Stash List &amp; Apply Manage Stashes 🪝 Hooks Git Hooks Location Sample Pre-Commit Hook Enable Hooks 🔬 Advanced Techniques Filter-Branch BFG Repo-Cleaner / BFG Find Large Files Blame &amp; Log Diff Advanced Archive Sparse Checkout Maintenance 🌟 Real-World Examples Fix Commit Message Undo Last Commit Squash Commits Split Commit Emergency Recovery 💡 Best Practices 🔧 Useful Aliases Official Documentation