| name | Git |
| description | Expert guidance for Git version control operations including commits, branches, merging, rebasing, conflict resolution, and Git workflows. Use this when working with Git repositories, version control, or collaborative development. |
Git
Expert assistance with Git version control and collaborative workflows.
Essential Commands
Repository Setup
git init - Initialize new repository
git clone <url> - Clone remote repository
git remote add origin <url> - Add remote
git remote -v - List remotes
Daily Workflow
git status - Check working tree status
git add . - Stage all changes
git add -p - Interactive staging
git commit -m "message" - Commit with message
git commit --amend - Amend last commit
git pull - Fetch and merge from remote
git push - Push commits to remote
Branch Management
git branch - List local branches
git branch -a - List all branches (including remote)
git branch <name> - Create new branch
git checkout <branch> - Switch to branch
git checkout -b <name> - Create and switch to new branch
git branch -d <name> - Delete branch (safe)
git branch -D <name> - Force delete branch
git push origin --delete <branch> - Delete remote branch
Viewing History
git log - View commit history
git log --oneline --graph - Compact graphical history
git log --author="name" - Filter by author
git show <commit> - Show commit details
git diff - Show unstaged changes
git diff --staged - Show staged changes
git diff <branch1>..<branch2> - Compare branches
Undoing Changes
git restore <file> - Discard working changes
git restore --staged <file> - Unstage file
git reset HEAD~1 - Undo last commit (keep changes)
git reset --hard HEAD~1 - Undo last commit (discard changes)
git revert <commit> - Create new commit that undoes changes
git clean -fd - Remove untracked files
Stashing
git stash - Stash current changes
git stash list - List stashes
git stash pop - Apply and remove last stash
git stash apply - Apply last stash (keep in list)
git stash drop - Delete last stash
Advanced Operations
Rebasing
git rebase main
git rebase -i HEAD~3
git rebase --continue
git rebase --abort
Cherry-picking
git cherry-pick <commit-hash>
git cherry-pick -n <commit-hash>
Merging
git merge <branch>
git merge --no-ff <branch>
git merge --abort
Conflict Resolution
git status
git add <resolved-file>
git commit
git checkout --theirs <file>
git checkout --ours <file>
Git Workflows
Feature Branch Workflow
git checkout -b feature/new-feature
git add .
git commit -m "Add new feature"
git checkout main
git pull
git checkout feature/new-feature
git rebase main
git push -u origin feature/new-feature
Hotfix Workflow
git checkout main
git pull
git checkout -b hotfix/critical-fix
git commit -am "Fix critical bug"
git checkout main
git merge hotfix/critical-fix
git push
git branch -d hotfix/critical-fix
Configuration
User Setup
git config --global user.name "Your Name"
git config --global user.email "your@email.com"
Useful Aliases
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.lg "log --oneline --graph --all"
Editor
git config --global core.editor "vim"
Best Practices
- Commit Messages: Use clear, descriptive messages following conventional commits
- Small Commits: Make atomic commits that do one thing
- Pull Before Push: Always pull latest changes before pushing
- Branch Naming: Use descriptive names like
feature/, bugfix/, hotfix/
- Never Force Push: Avoid
git push --force on shared branches
- Review Changes: Use
git diff before committing
- Protect Main: Never commit directly to main/master
Troubleshooting
Undo accidental commit to wrong branch
git reset HEAD~1
git stash
git checkout <correct-branch>
git stash pop
git add .
git commit -m "message"
Fix merge conflicts
git status
git add <file>
git commit
Recover deleted branch
git reflog
git checkout -b <branch-name> <commit-hash>