| # Git Skills |
|
|
| ## Setup & Config |
|
|
| ```bash |
| # Configure user |
| git config --global user.name "Your Name" |
| git config --global user.email "your@email.com" |
| |
| # Configure editor |
| git config --global core.editor vim |
| |
| # List config |
| git config --list |
| ``` |
|
|
| ## Basic Commands |
|
|
| ### Status & Logs |
| ```bash |
| git status # Show working tree status |
| git status -s # Short format |
| git log # Show commit history |
| git log --oneline # Compact log |
| git log -n 5 # Last 5 commits |
| git log --graph # Visual branch graph |
| ``` |
|
|
| ### Add & Commit |
| ```bash |
| git add file.txt # Stage single file |
| git add . # Stage all changes |
| git add -p # Interactive staging |
| git commit -m "message" # Commit with message |
| git commit -am "message" # Add + commit (tracked files) |
| git commit --amend # Modify last commit |
| ``` |
|
|
| ### Branching |
| ```bash |
| git branch # List branches |
| git branch feature/login # Create branch |
| git checkout feature/login # Switch to branch |
| git switch -c feature/login # Create and switch (modern) |
| git branch -d feature/login # Delete branch |
| git branch -m new-name # Rename current branch |
| ``` |
|
|
| ### Merging |
| ```bash |
| git checkout main |
| git merge feature/login # Merge feature into main |
| git merge --no-ff feature # No fast-forward merge |
| git merge --squash feature # Squash commits |
| ``` |
|
|
| ### Rebasing |
| ```bash |
| git rebase main # Rebase onto main |
| git rebase -i HEAD~3 # Interactive rebase |
| git rebase --continue |
| git rebase --abort |
| ``` |
|
|
| ## Remote Operations |
|
|
| ### Push & Pull |
| ```bash |
| git push origin main # Push to remote |
| git push -u origin feature # Push with upstream |
| git push --force # Force push (use carefully) |
| git pull # Pull and merge |
| git pull --rebase # Pull with rebase |
| git fetch # Fetch without merge |
| ``` |
|
|
| ### Remote Management |
| ```bash |
| git remote -v # List remotes |
| git remote add origin url # Add remote |
| git remote remove origin # Remove remote |
| git remote rename origin upstream |
| ``` |
|
|
| ## Advanced Operations |
|
|
| ### Stashing |
| ```bash |
| git stash # Stash changes |
| git stash pop # Apply and remove stash |
| git stash apply # Apply without removing |
| git stash list # List stashes |
| git stash drop # Delete stash |
| git stash -u # Include untracked files |
| ``` |
|
|
| ### Resetting |
| ```bash |
| git reset HEAD~1 # Reset to commit before last |
| git reset --soft HEAD~1 # Keep changes staged |
| git reset --hard HEAD~1 # Discard all changes |
| ``` |
|
|
| ### Cherry Picking |
| ```bash |
| git cherry-pick abc123 # Cherry-pick single commit |
| git cherry-pick abc123 def456 # Multiple commits |
| ``` |
|
|
| ### Tagging |
| ```bash |
| git tag v1.0.0 # Create tag |
| git tag -a v1.0.0 -m "Release 1.0" # Annotated tag |
| git push origin v1.0.0 # Push tag |
| git tag -d v1.0.0 # Delete local tag |
| ``` |
|
|
| ## Workflow Examples |
|
|
| ### Feature Branch Workflow |
| ```bash |
| git checkout -b feature/new-feature |
| # Make changes |
| git add . |
| git commit -m "Add new feature" |
| git push -u origin feature/new-feature |
| # Create PR on GitHub/GitLab |
| # After PR merged |
| git checkout main |
| git pull origin main |
| git branch -d feature/new-feature |
| ``` |
|
|
| ### Hotfix Workflow |
| ```bash |
| git checkout main |
| git pull |
| git checkout -b hotfix/bug-fix |
| # Fix the bug |
| git commit -am "Fix critical bug" |
| git checkout main |
| git merge --no-ff hotfix/bug-fix |
| git push origin main |
| git branch -d hotfix/bug-fix |
| ``` |
|
|
| ## Finding & Fixing Issues |
|
|
| ### Blame & Show |
| ```bash |
| git blame file.txt # Show who changed what |
| git show abc123 # Show commit details |
| git show abc123:file.txt # Show file at commit |
| ``` |
|
|
| ### Bisect (Find Bug) |
| ```bash |
| git bisect start |
| git bisect bad HEAD |
| git bisect good abc123 |
| # Test... |
| git bisect good # or git bisect bad |
| git bisect reset |
| ``` |
|
|
| ### Diff |
| ```bash |
| git diff # Unstaged changes |
| git diff --staged # Staged changes |
| git diff HEAD~1 # vs previous commit |
| git diff main...feature # Changes in feature branch |
| ``` |
|
|