Git: Comprehensive Guide
Table of Contents
Section titled “Table of Contents”- Configuration
- Aliases and Shortcuts
- Merge Conflicts
- Merge Strategies
- Repository Management
- Best Practices
- Common Commands Reference
Configuration
Section titled “Configuration”Global Configuration Setup
Section titled “Global Configuration Setup”git config --global user.name "CJ Steiner"git config --global user.email "csrhinoserver@gmail.com"git config --global core.editor vigit config --global core.pager "less -FRXS"git config --global color.ui truegit config --global svc.rmdir truegit config --global merge.tool vimdiffgit config --global merge.summary truegit config --global push.default simplegit config --global format.pretty fullergit config --global pull.rebase trueAliases and Shortcuts
Section titled “Aliases and Shortcuts”Navigation Aliases
Section titled “Navigation Aliases”alias ..='cd ../'alias ...='cd ../../'alias ....='cd ../../../'Git Aliases
Section titled “Git Aliases”alias gd='git diff'alias gds='git diff --staged'alias gs='git status'alias ga='git add'alias gr='git restore'alias grs='git restore --staged'alias gc='git commit'alias gcm='git commit -m'alias gca='git commit --amend --no-edit --date=now'alias gb='git branch'alias gbdates="git for-each-ref --sort=committerdate refs/heads/ --format='%(HEAD) %(color:yellow)%(refname:short)%(color:reset) - %(contents-subject) - %(authorname) (%(color:green)%(committerdate:short)%(color:reset))'"alias gbresethard='git reset --hard @{u}'Merge Conflicts
Section titled “Merge Conflicts”Understanding Merge Conflicts
Section titled “Understanding Merge Conflicts”A merge conflict occurs when Git cannot automatically resolve differences between two commits. This happens when:
- Both branches modify the same lines in a file
- One branch deletes a file that another branch modifies
- Two branches add different files with the same name
Common Conflict Scenarios
Section titled “Common Conflict Scenarios”Code Changes in Same Location
Section titled “Code Changes in Same Location”# Branch A modifies line 10-15# Branch B modifies line 10-15# Result: Conflict when merginggit merge feature-branch# Conflict in README.mdFile Deletion vs Modification
Section titled “File Deletion vs Modification”# Branch A: deletes config.json# Branch B: modifies config.json# Result: Delete/modify conflictUnderstanding Conflict Markers
Section titled “Understanding Conflict Markers”When a conflict occurs, Git marks the conflicting sections:
<<<<<<< HEADThis is the version from the current branch (HEAD)=======This is the version from the branch being merged>>>>>>> feature-branchSections marked:
<<<<<<< HEAD— Current branch version=======— Separator>>>>>>> branch-name— Incoming branch version
Resolving Conflicts Manually
Section titled “Resolving Conflicts Manually”Step 1: Identify Conflicted Files
Section titled “Step 1: Identify Conflicted Files”git status# Shows "Unmerged paths" with conflicted filesStep 2: Edit Conflicted Files
Section titled “Step 2: Edit Conflicted Files”Open conflicted files and manually decide which changes to keep. Remove conflict markers and edit the code.
# Example: resolving in config.json<<<<<<< HEAD{ "environment": "production"}======={ "environment": "staging"}>>>>>>> update-config
# Resolved version:{ "environment": "production-staging"}Step 3: Mark as Resolved
Section titled “Step 3: Mark as Resolved”git add <resolved-file>Step 4: Complete the Merge
Section titled “Step 4: Complete the Merge”git commit -m "Resolve merge conflict in config.json"# Or if already staged:git merge --continueUsing Merge Tools
Section titled “Using Merge Tools”Configure a Merge Tool
Section titled “Configure a Merge Tool”# Use vimdiffgit config --global merge.tool vimdiff
# Use VSCodegit config --global merge.tool vscodegit config --global mergetool.vscode.cmd 'code --wait $MERGED'
# Use meld (visual tool)git config --global merge.tool meldLaunch Merge Tool
Section titled “Launch Merge Tool”git mergetoolThe tool opens each conflicted file for visual resolution. After resolving:
git add .git commitMerge Strategies
Section titled “Merge Strategies”Recursive (Default)
Section titled “Recursive (Default)”git merge feature-branchWorks best for most scenarios. Takes multiple common ancestors into account.
Ours Strategy
Section titled “Ours Strategy”Always prefer current branch changes:
git merge -s ours feature-branchUse with caution — discards changes from the merged branch.
Theirs Strategy
Section titled “Theirs Strategy”Take changes from the branch being merged (requires creating a temporary commit):
git merge -X theirs feature-branchResolve Strategy
Section titled “Resolve Strategy”Simpler algorithm, useful for 2-way merges:
git merge -s resolve feature-branchConflict Resolution Preferences
Section titled “Conflict Resolution Preferences”Prefer current branch’s changes:
Section titled “Prefer current branch’s changes:”git merge -X ours feature-branchPrefer incoming branch’s changes:
Section titled “Prefer incoming branch’s changes:”git merge -X theirs feature-branchUndoing a Merge
Section titled “Undoing a Merge”If Not Yet Committed
Section titled “If Not Yet Committed”Abort the merge entirely:
git merge --abortAfter Merging (Undo Last Commit)
Section titled “After Merging (Undo Last Commit)”# Reset to before mergegit reset --hard HEAD~1Creating a Revert Commit
Section titled “Creating a Revert Commit”For merged commits already pushed to shared branches:
git revert -m 1 <merge-commit-hash>The -m 1 flag specifies merging with mainline.
Working with Rebases
Section titled “Working with Rebases”Conflicts can also occur during rebasing. The process is similar:
git rebase main# Conflicts occur# ... resolve conflicts ...git add <resolved-files>git rebase --continue# Or abort:git rebase --abortRepository Management
Section titled “Repository Management”Create a Git Bundle
Section titled “Create a Git Bundle”Create a portable repository snapshot:
git bundle create repo.bundle --allgit clone repo.bundle /tmp/repo/Sync Local with Upstream
Section titled “Sync Local with Upstream”git fetch upstream# if no upstream git remote add upstream <url>git merge upstream/mastergit push origin masterAlternative using rebase:
git fetch upstreamgit rebase upstream/mastergit push origin masterFix Broken Git Repository
Section titled “Fix Broken Git Repository”git gcgit remote prune origingit fetch --prune # clean up refsBest Practices
Section titled “Best Practices”Keep Commits Small and Focused
Section titled “Keep Commits Small and Focused”Smaller, focused commits reduce conflict surface area.
# Good: Specific commitgit commit -m "Fix authentication bug in login handler"
# Avoid: Large commit touching many areasgit commit -m "Various fixes and updates"Sync Frequently with Main Branch
Section titled “Sync Frequently with Main Branch”git fetch origingit rebase origin/master# orgit merge origin/masterCommunicate with Team
Section titled “Communicate with Team”- Discuss major changes before starting work
- Review related PRs before starting similar work
- Notify team when working on shared files
Use Feature Branches Strategically
Section titled “Use Feature Branches Strategically”Keep feature branches short-lived (days, not weeks).
# Create focused feature branchgit checkout -b fix/auth-bug# ... make changes ...# Merge quickly to avoid divergencegit push origin fix/auth-bug# Create PR and mergeOrganize Code to Minimize Conflicts
Section titled “Organize Code to Minimize Conflicts”- Separate concerns into different files
- Avoid modifying shared utility files unnecessarily
- Use configuration files for shared settings
Common Commands Reference
Section titled “Common Commands Reference”| Command | Purpose |
|---|---|
git status | Show working tree status |
git diff | View differences |
git diff --staged | View staged changes |
git add <file> | Stage changes |
git commit -m "msg" | Create commit |
git push | Push to remote |
git pull | Fetch and merge |
git fetch | Download remote changes |
git merge <branch> | Merge branch |
git rebase <branch> | Rebase current branch |
git branch | List branches |
git checkout -b <branch> | Create and switch branch |
git log | View commit history |
git reset --hard HEAD~1 | Undo last commit |
git checkout --ours <file> | Take current branch version in conflict |
git checkout --theirs <file> | Take incoming branch version in conflict |
git mergetool | Open visual merge tool |
git merge --abort | Cancel merge in progress |
git revert -m 1 <hash> | Revert merge commit |
git bundle create | Create repository bundle |
git gc | Garbage collection |
git remote prune origin | Clean up stale remote refs |