---
type: agent/information
---

{{h1}}git.md

{{h1}}Git Best Practices & Tips

{{h2}}Core Best Practices

{{h3}}1. Commit Messages
- {{b}}Use conventional commits:{{b}} `type(scope): description`
- {{b}}Keep first line under 50 characters{{b}}
- {{b}}Use imperative mood:{{b}} "Add feature" not "Added feature"
- {{b}}Include issue references:{{b}} `fix: resolve login bug (#123)`

{{h3}}2. Branch Management
- {{b}}Use descriptive branch names:{{b}} `feature/user-authentication`, `fix/memory-leak`
- {{b}}Keep branches focused:{{b}} One feature/fix per branch
- {{b}}Delete merged branches:{{b}} Clean up after merging
- {{b}}Use branch protection rules{{b}} on main/master

{{h3}}3. Commit Hygiene
- {{b}}Commit early and often{{b}} with logical chunks
- {{b}}Never commit secrets{{b}} or sensitive data
- {{b}}Use `.gitignore`{{b}} for build artifacts and IDE files
- {{b}}Squash commits{{b}} before merging to keep history clean

{{h3}}4. Collaboration
- {{b}}Pull before push:{{b}} Always sync with remote first
- {{b}}Use pull requests/merge requests{{b}} for code review
- {{b}}Rebase feature branches{{b}} to maintain linear history
- {{b}}Tag releases{{b}} with semantic versioning

{{h2}}Specific Git Usage Instructions
{{b}}IMPORTANT{{b}}
- As all commits are signed, do not do a git commit directly.
  Instead, prepare the commit message, and ask me to do so
- {{b}}NEVER{{b}} `checkout` or `restore` files from git to revert recent changes that you have made. What is in git may not be up to date as other changes that have been made, and these commands also risk overwriting other files as a side-effect, destroying many hours of development work.
- When you come to a logical place where a feature or sub-feature has been completed, ask the user whether they wish to commit.
- Before commit, all the relevant checks must be done in preparation:
  - All tests must be passing
  - All lint and type checking must be passing
  - Code has been formatted according to the required standard

{{h2}}Essential Commands & Tips

{{h3}}Quick Fixes
```bash
# Undo last commit (keep changes)
git reset --soft HEAD~1

# Amend last commit message
git commit --amend -m "New message"

# Unstage files
git reset HEAD <file>

# Discard local changes
git checkout -- <file>
```

{{h3}}Branch Operations
```bash
# Create and switch to new branch
git checkout -b feature/new-feature

# Switch branches
git switch main

# Delete local branch
git branch -d feature-name

# Delete remote branch
git push origin --delete feature-name
```

{{h3}}History & Information
```bash
# View commit history (one line)
git log --oneline

# Show changes in last commit
git show

# Find who changed a line
git blame <file>

# Search commit messages
git log --grep="keyword"
```

{{h3}}Stashing
```bash
# Stash current changes
git stash

# Apply last stash
git stash pop

# List all stashes
git stash list

# Apply specific stash
git stash apply stash@{2}
```

{{h2}}Advanced Tips

{{h3}}5. Rebase vs Merge
- {{b}}Use rebase{{b}} for feature branches to maintain clean history
- {{b}}Use merge{{b}} for integrating completed features
- {{b}}Never rebase shared/public branches{{b}}

{{h3}}6. Interactive Rebase
```bash
# Clean up last 3 commits
git rebase -i HEAD~3

# Squash, reword, or reorder commits
# Use 'squash' to combine commits
```

{{h3}}7. Cherry-picking
```bash
# Apply specific commit to current branch
git cherry-pick <commit-hash>

# Cherry-pick without committing
git cherry-pick -n <commit-hash>
```

{{h3}}8. Aliases for Efficiency
```bash
# Set up useful aliases
git config --global alias.st status
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'
```

{{h2}}FAQ Solutions

{{h3}}"I committed to the wrong branch"
```bash
# Move last commit to correct branch
git checkout correct-branch
git cherry-pick wrong-branch
git checkout wrong-branch
git reset --hard HEAD~1
```

{{h3}}"I need to change the last commit"
```bash
# Add more changes to last commit
git add .
git commit --amend --no-edit
```

{{h3}}"I want to see what changed"
```bash
# Compare working directory to last commit
git diff HEAD

# Compare staged changes
git diff --cached

# Compare two branches
git diff main..feature-branch
```

{{h3}}"I accidentally deleted a file"
```bash
# Restore file from last commit
git checkout HEAD -- <file>

# Find when file was deleted
git log --oneline --follow -- <file>
```

{{h2}}Security & Cleanup

{{h3}}9. Remove Sensitive Data
```bash
# Remove file from entire history (use with caution)
git filter-branch --force --index-filter \
'git rm --cached --ignore-unmatch <file>' \
--prune-empty --tag-name-filter cat -- --all
```

{{h3}}10. Repository Maintenance
```bash
# Clean up unreachable objects
git gc --prune=now

# Verify repository integrity
git fsck

# Show repository size
git count-objects -vH
```

{{h2}}Configuration Essentials

{{h3}}11. Global Setup
```bash
# Set identity
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

# Set default editor
git config --global core.editor "code --wait"

# Enable colour output
git config --global color.ui auto
```

{{h3}}12. Ignore Patterns
{{b}}Common `.gitignore` entries:{{b}}
```
# Dependencies
node_modules/
__pycache__/
*.pyc

# Build outputs
dist/
build/
*.o
*.exe

# IDE files
.vscode/
.idea/
*.swp

# OS files
.DS_Store
Thumbs.db

# Environment files
.env
.env.local
```

Remember: {{b}}Git is a tool for collaboration and history tracking. Keep commits atomic, meaningful, and your history clean.{{b}}
