1. Setup & Configuration
Define your Git identity (required before first commit)
git config --global user.name "John Doe"
git config --global user.email "john@example.com"
Example: Set your name/email to attribute commits correctly.
Starting a Project
Clone an existing repository:
git clone https://github.com/username/repo.git
Example: Download a project from GitHub to your local machine.
Initialize a new repository:
mkdir my-project && cd my-project git init
Example: Start version control for a new app.
Everyday Workflow
Check status of changes:
git status
Example: See which files are modified/staged.
Stage changes for commit:
git add file.txt # Stage a single file git add . # Stage all modified files
Example: Prepare changes to a login feature for saving.
Commit staged changes:
git commit -m "Add user login functionality"
Example: Save a snapshot of your progress with a clear message.
Branching & Merging
Create a new branch:
git branch feature/payment git checkout feature/payment # Or shortcut: git checkout -b feature/payment Example: Develop a payment system without disrupting the main code.
Merge branches:
git checkout main git merge feature/payment
Example: Integrate the completed payment feature into the main app.
List all branches:
git branch
Switch branches:
git checkout main
Delete a branch:
git branch -d feature/payment
Remote Repositories
Push changes to GitHub/GitLab:
git push origin main
Example: Upload your latest bug fix to the team’s remote repo.
Pull latest updates:
git pull origin main
Example: Sync your local code with changes made by teammates.
Link a local repo to a remote:
git remote add origin https://github.com/username/repo.git
Undoing Mistakes
Discard unstaged changes:
git restore file.txt
Example: Revert accidental edits toconfig.yml.
Amend the last commit:
git commit --amend -m "Improved login error handling"
Example: Fix a typo in the previous commit message or add missed files.
Reset to a previous commit:
git reset --hard HEAD~1 # WARNING: Destructive!
Example: Completely undo a bad commit.
Stashing
Temporarily save uncommitted work:
git stash git stash pop # Restore the stash
Example: Pause work on a feature to fix an urgent bug.
Collaboration
Push local changes to GitHub/GitLab:
git push origin main
Example: Upload your bug fix so the team can access it.
Link local repo to a remote:
git remote add origin https://github.com/user/repo.git
Fetch updates without merging:
git fetch
Example: Check if teammates pushed new code before merging.
View commit history:
git log --oneline --graph
Example: Track who changed the API endpoint last week.
Advanced (But Useful!)
Cherry-pick a commit:
git cherry-pick abc1234
Example: Apply a hotfix frommainto a legacy branch.
Manage submodules:
git submodule add https://github.com/library/dependency.git
Example: Include a shared UI component library.
Aliases for Speed
Create shortcuts in ~/.gitconfig:
[alias] co = checkout br = branch ci = commit st = status
Example: Type git st instead of git status!
💡 Pro Tips:
Pro Tips:
- Use
git diffto see line-by-line changes before committing. - Always test code after pulling and before pushing.
- Write clear commit messages (e.g., "Fix navbar overflow bug" not "Update code").
Final Project:
- Create a GitHub account.
- Make a repo for a mock project (e.g., travel blog).
- Practice: Add files, commit, branch, merge, and push!
