Git is more than just commit, push, and pull. Let's explore advanced techniques that can improve your version control workflow.
# Initialize git flow
git flow init
# Start a new feature
git flow feature start my-feature
# Finish a feature
git flow feature finish my-feature
# Start a hotfix
git flow hotfix start bug-fix
# List all branches with latest commits
git branch -v
# Delete merged branches
git branch --merged | grep -v "\*" | xargs -n 1 git branch -d
# Show branch hierarchy
git log --graph --oneline --all
# Start interactive rebase
git rebase -i HEAD~3
# Commands available in rebase:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# Start bisect
git bisect start
# Mark current version as bad
git bisect bad
# Mark last known good version
git bisect good v1.0
# Git will checkout commits for you to test
# Mark each as good or bad until found
git bisect good # or
git bisect bad
# End bisect
git bisect reset
# Cherry-pick a specific commit
git cherry-pick abc123
# Cherry-pick without committing
git cherry-pick -n abc123
# Cherry-pick a range of commits
git cherry-pick abc123..def456
#!/bin/sh
# .git/hooks/pre-commit
# Run tests before commit
npm test
# Check code formatting
npm run lint
# Exit with non-zero status if any commands fail
#!/bin/sh
# .git/hooks/commit-msg
# Enforce conventional commits
if ! grep -qE '^(feat|fix|docs|style|refactor|test|chore): .+' "$1"; then
echo "Commit message must follow conventional commits format"
exit 1
fi
# Set up aliases
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
# Custom log format
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
# Create a new worktree
git worktree add ../feature-branch feature
# List worktrees
git worktree list
# Remove a worktree
git worktree remove ../feature-branch
# Recursive strategy with patience algorithm
git merge --strategy-option=patience feature
# Octopus merge for multiple branches
git merge branch1 branch2 branch3
# Rebase merge
git merge --rebase feature
# Clone with limited history
git clone --depth 1 repository-url
# Sparse checkout
git sparse-checkout set folder1/ folder2/
# Update sparse checkout
git sparse-checkout add folder3/
Commit Messages
# Good commit message format
feat: add new user authentication system
- Implement JWT tokens
- Add password hashing
- Create user sessions
Branch Naming
feature/user-auth
bugfix/login-error
hotfix/security-patch
release/v1.2.0
Git Attributes
# Auto detect text files and perform LF normalization
* text=auto
# Declare files that will always have CRLF line endings
*.bat text eol=crlf
# Denote binary files
*.png binary
*.jpg binary
# Fix detached HEAD
git checkout main
# Recover lost commits
git reflog
git checkout HEAD@{1}
# Undo last commit
git reset --soft HEAD^
# Clean untracked files
git clean -fd
Mastering these advanced Git techniques can significantly improve your development workflow and make you more efficient at version control.