Git
Expert assistance with Git version control and collaborative workflows.
Essential Commands
Repository Setup
git init- Initialize new repositorygit clone <url>- Clone remote repositorygit remote add origin <url>- Add remotegit remote -v- List remotes
Daily Workflow
git status- Check working tree statusgit add .- Stage all changesgit add -p- Interactive staginggit commit -m "message"- Commit with messagegit commit --amend- Amend last commitgit pull- Fetch and merge from remotegit push- Push commits to remote
Branch Management
git branch- List local branchesgit branch -a- List all branches (including remote)git branch <name>- Create new branchgit checkout <branch>- Switch to branchgit checkout -b <name>- Create and switch to new branchgit branch -d <name>- Delete branch (safe)git branch -D <name>- Force delete branchgit push origin --delete <branch>- Delete remote branch
Viewing History
git log- View commit historygit log --oneline --graph- Compact graphical historygit log --author="name"- Filter by authorgit show <commit>- Show commit detailsgit diff- Show unstaged changesgit diff --staged- Show staged changesgit diff <branch1>..<branch2>- Compare branches
Undoing Changes
git restore <file>- Discard working changesgit restore --staged <file>- Unstage filegit 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 changesgit clean -fd- Remove untracked files
Stashing
git stash- Stash current changesgit stash list- List stashesgit stash pop- Apply and remove last stashgit stash apply- Apply last stash (keep in list)git stash drop- Delete last stash
Advanced Operations
Rebasing
bash1# Rebase current branch onto main 2git rebase main 3 4# Interactive rebase (squash, reorder, edit commits) 5git rebase -i HEAD~3 6 7# Continue after resolving conflicts 8git rebase --continue 9 10# Abort rebase 11git rebase --abort
Cherry-picking
bash1# Apply specific commit to current branch 2git cherry-pick <commit-hash> 3 4# Cherry-pick without committing 5git cherry-pick -n <commit-hash>
Merging
bash1# Merge branch into current 2git merge <branch> 3 4# Merge without fast-forward (create merge commit) 5git merge --no-ff <branch> 6 7# Abort merge 8git merge --abort
Conflict Resolution
bash1# Show conflicts 2git status 3 4# After resolving conflicts in files 5git add <resolved-file> 6git commit 7 8# Use theirs/ours for entire file 9git checkout --theirs <file> 10git checkout --ours <file>
Git Workflows
Feature Branch Workflow
bash1# Create feature branch 2git checkout -b feature/new-feature 3 4# Work and commit 5git add . 6git commit -m "Add new feature" 7 8# Update from main 9git checkout main 10git pull 11git checkout feature/new-feature 12git rebase main 13 14# Push feature branch 15git push -u origin feature/new-feature
Hotfix Workflow
bash1# Create hotfix from main 2git checkout main 3git pull 4git checkout -b hotfix/critical-fix 5 6# Fix and commit 7git commit -am "Fix critical bug" 8 9# Merge back to main 10git checkout main 11git merge hotfix/critical-fix 12git push 13 14# Clean up 15git branch -d hotfix/critical-fix
Configuration
User Setup
bash1git config --global user.name "Your Name" 2git config --global user.email "your@email.com"
Useful Aliases
bash1git config --global alias.co checkout 2git config --global alias.br branch 3git config --global alias.ci commit 4git config --global alias.st status 5git config --global alias.lg "log --oneline --graph --all"
Editor
bash1git 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 --forceon shared branches - Review Changes: Use
git diffbefore committing - Protect Main: Never commit directly to main/master
Troubleshooting
Undo accidental commit to wrong branch
bash1git reset HEAD~1 # Undo commit, keep changes 2git stash # Stash changes 3git checkout <correct-branch> 4git stash pop # Apply changes 5git add . 6git commit -m "message"
Fix merge conflicts
bash1# View conflicts 2git status 3 4# Edit conflicting files (look for <<<<<<, ======, >>>>>>) 5# Remove conflict markers and keep desired code 6 7# Mark as resolved 8git add <file> 9git commit
Recover deleted branch
bash1# Find commit hash 2git reflog 3 4# Recreate branch 5git checkout -b <branch-name> <commit-hash>