Git

Track code changes, collaborate with teams, and maintain project history with distributed version control

TL;DR

One-liner: Git tracks every change to your code, lets you undo mistakes, and makes collaboration possible.

Core Value:

  • History - every change is recorded
  • Undo - go back to any previous state
  • Branches - work on features without breaking main code
  • Collaboration - merge work from multiple people

Quick Start

Install

macOS:

brew install git

Windows: Download Git for Windows

Linux:

sudo apt install git  # Debian/Ubuntu

Configure

git config --global user.name "Your Name"
git config --global user.email "[email protected]"

First Repo

mkdir myproject && cd myproject
git init
echo "# My Project" > README.md
git add .
git commit -m "Initial commit"

Cheatsheet

CommandDescription
git initCreate new repo
git clone URLClone a repo
git statusCheck current state
git add FILEStage changes
git add .Stage all changes
git commit -m "msg"Commit changes
git pushPush to remote
git pullPull from remote
git branchList branches
git branch NAMECreate branch
git checkout NAMESwitch branch
git checkout -b NAMECreate and switch
git merge NAMEMerge branch
git log --onelineView history
git diffView unstaged changes
git stashSave changes temporarily
git stash popRestore stashed changes

Gotchas

Forgot to add files before commit

git add forgotten-file
git commit --amend --no-edit

Wrong commit message

git commit --amend -m "Correct message"

Undo last commit (keep changes)

git reset --soft HEAD~1

Undo last commit (discard changes)

git reset --hard HEAD~1

Merge conflicts

Edit the conflicting files (look for <<<<<<<), then:

git add .
git commit -m "Resolve conflicts"

Accidentally committed to wrong branch

# Move commit to correct branch
git checkout correct-branch
git cherry-pick COMMIT_HASH
git checkout wrong-branch
git reset --hard HEAD~1

Next Steps