1. What is Git?
Git is a version control system. It tracks changes to your files over time, so you can:
- Go back to older versions
- Work on features without breaking the main code
- Collaborate with other people without overwriting each other's work
2. Setup (Do This Once)
# Tell Git who you are
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
# Check your settings
git config --list3. Starting a Project
# Create a new Git repository in the current folder
git init
# Copy (clone) an existing repository from GitHub/GitLab etc.
git clone https://github.com/user/repo.git💡
initstarts fresh.clonecopies a project that already exists somewhere else.
4. The Basic Workflow
This is the core loop you'll repeat constantly:
git status # See what changed
git add file.txt # Stage a specific file
git add . # Stage everything
git commit -m "Describe what you changed" # Save a snapshotQuick visual
| Stage | Command | Meaning |
|---|---|---|
| Working Directory | (you edit files) | Your actual files on disk |
| Staging Area | git add | Files marked "ready to save" |
| Repository | git commit | Permanent snapshot saved |
5. Checking History & Changes
git log # Full commit history
git log --oneline # Compact one-line-per-commit view
git log --graph --all # Visual branch history
git diff # See unstaged changes
git diff --staged # See staged changes (before commit)
git show <commit-hash> # See details of one specific commit6. Branching
Branches let you work on something new without touching the main code.
git branch # List all branches
git branch new-feature # Create a new branch
git checkout new-feature # Switch to that branch
# Shortcut: create + switch in one step
git checkout -b new-feature
# Newer Git versions (recommended)
git switch new-feature
git switch -c new-feature # create + switchgit branch -d branch-name # Delete a branch (safe)
git branch -D branch-name # Force delete a branch7. Merging & Combining Work
git checkout main # Go to the branch you want to merge INTO
git merge new-feature # Bring changes from new-feature into mainIf there's a conflict, Git will mark the conflicting lines in the file like this:
<<<<<<< HEAD
your current code
=======
incoming code
>>>>>>> new-feature
Edit the file to keep what you want, then:
git add file.txt
git commit -m "Resolve merge conflict"8. Working with Remotes (GitHub, GitLab, etc.)
git remote -v # Show connected remote repositories
git remote add origin <url> # Connect a remote for the first time
git push origin main # Upload your commits
git push -u origin main # Upload + remember this as default
git pull origin main # Download + merge latest changes
git fetch # Download changes WITHOUT merging them💡
pull=fetch+merge. Usefetchwhen you want to review changes before merging.
9. Undoing Things
| Situation | Command |
|---|---|
| Unstage a file (keep changes) | git restore --staged file.txt |
| Discard changes in a file | git restore file.txt |
| Undo last commit, keep changes staged | git reset --soft HEAD~1 |
| Undo last commit, unstage changes | git reset HEAD~1 |
| Undo last commit, delete changes | git reset --hard HEAD~1 ⚠️ |
| Create a new commit that undoes an old one | git revert <commit-hash> |
⚠️
--hardpermanently deletes changes. Use with caution.
10. Stashing (Save Work Temporarily)
Useful when you need to switch branches but aren't ready to commit.
git stash # Save current changes, clean working directory
git stash list # See all stashes
git stash pop # Reapply the most recent stash and remove it
git stash apply # Reapply without removing from stash list
git stash drop # Delete a stash11. Tags (Marking Releases)
git tag v1.0.0 # Create a lightweight tag
git tag -a v1.0.0 -m "Release 1.0" # Create an annotated tag (recommended)
git push origin v1.0.0 # Push a single tag
git push origin --tags # Push all tags12. Inspecting Files
git blame file.txt # See who last changed each line
git log -p file.txt # See full change history of one file13. Common Real-World Scenarios
"I committed to the wrong branch"
git reset --soft HEAD~1 # Undo the commit, keep changes
git switch correct-branch
git add .
git commit -m "message""I want to update my branch with the latest main"
git switch my-branch
git merge main
# or, for a cleaner history:
git rebase main"I accidentally deleted a file"
git checkout -- file.txt"I want to see what changed before pulling"
git fetch
git diff main origin/main14. Quick Reference Cheat Sheet
git init # Start a repo
git clone <url> # Copy a repo
git status # Check current state
git add . # Stage all changes
git commit -m "msg" # Save a snapshot
git push # Upload to remote
git pull # Download + merge from remote
git branch # List branches
git switch -c <name> # Create + switch branch
git merge <branch> # Combine branches
git log --oneline # View history
git stash # Temporarily save changes
git reset --hard HEAD~1 # ⚠️ Undo + delete last commit15. Golden Rules
- Commit often, with clear messages.
- Pull before you push to avoid conflicts.
- Never
--forcepush to a shared branch unless you're sure. - Use branches for new features — don't work directly on
main. - When in doubt,
git statustells you exactly where you stand.