Spaces:
Sleeping
Sleeping
| Git Basics | |
| Git is a distributed version control system that tracks changes in source code over time. | |
| Setup: | |
| git config --global user.name "Your Name" | |
| git config --global user.email "you@example.com" | |
| Core Workflow: | |
| git init β initialize a new repository | |
| git clone <url> β copy a remote repository locally | |
| git status β show working tree status (staged, unstaged, untracked) | |
| git add <file> β stage a file for commit | |
| git add . β stage all changes | |
| git commit -m "msg" β save staged changes with a message | |
| git push origin main β push commits to remote | |
| git pull β fetch and merge from remote | |
| Branching: | |
| git branch β list all local branches | |
| git branch feature-x β create a new branch | |
| git checkout feature-x β switch to branch (older syntax) | |
| git switch feature-x β switch to branch (newer syntax) | |
| git checkout -b feature-x β create and switch in one step | |
| git merge feature-x β merge branch into current branch | |
| git branch -d feature-x β delete a branch (after merge) | |
| Remote: | |
| git remote -v β show remote URLs | |
| git remote add origin <url> β add a remote | |
| git fetch origin β download remote changes (don't merge) | |
| git pull origin main β fetch + merge remote main into current branch | |
| Undoing Changes: | |
| git restore <file> β discard unstaged changes in a file | |
| git restore --staged <file> β unstage a file | |
| git revert <commit> β create a new commit that undoes a commit (safe) | |
| git reset --soft HEAD~1 β undo last commit, keep changes staged | |
| git reset --hard HEAD~1 β undo last commit AND discard changes (destructive) | |
| Viewing History: | |
| git log β full commit history | |
| git log --oneline β compact one-line per commit | |
| git diff β show unstaged changes | |
| git diff --staged β show staged changes | |
| git show <commit> β show changes in a specific commit | |
| Stashing: | |
| git stash β temporarily save uncommitted changes | |
| git stash pop β restore last stash and remove it | |
| git stash list β show all stashes | |
| Rebasing vs Merging: | |
| - Merge: creates a merge commit, preserves history of both branches | |
| - Rebase: replays commits on top of another branch, creates linear history | |
| git rebase main β rebase current branch onto main | |
| - Rule: never rebase commits that have been pushed to a shared remote | |
| .gitignore: | |
| - Lists files/patterns Git should not track | |
| - Common entries: node_modules/, __pycache__/, .env, *.pyc, dist/, build/ | |
| - Check gitignore.io for language-specific templates | |