Git
Table of Contents
- Initial Setup
- Basic Operations (Recording Changes)
- Branches
- Remote Repositories
- Diff / History
- Undoing / Amending Changes
- Stash (Temporary Storage)
- Merge / Rebase
- Tags
1. Initial Setup
# Set user info (first time only)
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
# Set default branch name to main
git config --global init.defaultBranch main
# View current config
git config --listCreate / Clone a Repository
# Create a new repository
git init
# Clone a remote repository
git clone https://github.com/user/repo.git
# Clone with a custom directory name
git clone https://github.com/user/repo.git my-project2. Basic Operations (Recording Changes)
# Check status of working tree
git status
# Stage changes
git add <filename>
git add . # stage all changes
# Commit
git commit -m "commit message"
# Stage and commit at once (tracked files only)
git commit -am "commit message".gitignore
# Files and directories to exclude from tracking
node_modules/
.env
*.log
dist/
.DS_Store3. Branches
# List branches
git branch # local only
git branch -a # including remote
# Create a branch
git branch feature/login
# Switch to a branch
git switch feature/login
# Create and switch in one step
git switch -c feature/login
# Delete a branch
git branch -d feature/login # only if merged
git branch -D feature/login # force delete
# Rename a branch
git branch -m old-name new-name4. Remote Repositories
# View remotes
git remote -v
# Add a remote
git remote add origin https://github.com/user/repo.git
# Push (use -u on first push to set tracking)
git push -u origin main
git push # subsequent pushes
# Pull (fetch + merge)
git pull
# Fetch (download remote changes without merging)
git fetch origin
# Check out a remote branch locally
git switch -c feature/login origin/feature/login
# Delete a remote branch
git push origin --delete feature/login5. Diff / History
# View diffs
git diff # working tree vs staging
git diff --staged # staging vs latest commit
git diff main..feature # diff between branches
# Commit history
git log
git log --oneline # one line per commit
git log --oneline --graph # graph view with branches
git log -5 # last 5 commits
git log --author="Alice" # commits by a specific author
# History of a specific file
git log --oneline -- src/app.ts
# View changes in a specific commit
git show <commit-hash>6. Undoing / Amending Changes
# Unstage a file (keeps file changes)
git restore --staged <filename>
git restore --staged . # unstage all
# Discard changes in working tree (reverts file to latest commit)
git restore <filename>
# Amend the last commit message (unpushed only)
git commit --amend -m "updated message"
# Add more changes to the last commit (unpushed only)
git add <file>
git commit --amend --no-edit
# Undo a commit by creating a new reverting commit (preserves history)
git revert <commit-hash>
# Undo commits by moving HEAD (unpushed only)
git reset --soft HEAD~1 # undo commit only (changes remain staged)
git reset --mixed HEAD~1 # undo commit + unstage (changes remain in working tree)
git reset --hard HEAD~1 # undo commit + discard changes (irreversible)7. Stash (Temporary Storage)
Use stash when you need to switch branches but aren't ready to commit.
# Stash current changes
git stash
# Stash with a description
git stash push -m "WIP: login feature"
# List stashes
git stash list
# Apply and remove the latest stash
git stash pop
# Apply a specific stash (and remove it)
git stash pop stash@{1}
# Apply a stash without removing it
git stash apply stash@{0}
# Delete a stash
git stash drop stash@{0}
git stash clear # delete all stashes8. Merge / Rebase
Merge
# Merge feature into main
git switch main
git merge feature/login
# Always create a merge commit
git merge --no-ff feature/login
# Abort the merge
git merge --abortRebase
# Rebase feature branch onto tip of main
git switch feature/login
git rebase main
# Continue after resolving conflicts
git rebase --continue
# Abort the rebase
git rebase --abort
# Interactive rebase (squash, reword, reorder commits)
git rebase -i HEAD~3 # last 3 commitsInteractive Rebase Commands
| Command | Description |
|---|---|
pick | Use the commit as-is |
reword | Edit the commit message |
squash | Combine with the previous commit |
fixup | Combine (discard this commit's message) |
drop | Remove the commit |
Resolving Conflicts
# 1. Edit the conflicting file(s) manually
# 2. Stage the resolved file(s)
git add <filename>
# 3. For merge: commit; for rebase: continue
git commit
git rebase --continue9. Tags
# List tags
git tag
# Lightweight tag (name only)
git tag v1.0.0
# Annotated tag (includes message, author, date)
git tag -a v1.0.0 -m "Release v1.0.0"
# Tag a specific commit
git tag -a v1.0.0 <commit-hash>
# Push a tag to remote
git push origin v1.0.0
git push origin --tags # push all tags
# Delete a tag
git tag -d v1.0.0 # local
git push origin --delete v1.0.0 # remoteTips
Useful Aliases
git config --global alias.st status
git config --global alias.co switch
git config --global alias.br branch
git config --global alias.lg "log --oneline --graph --all"Switch Back to the Previous Branch
git switch - # switch to the previously checked-out branchApply a Specific Commit to the Current Branch
git cherry-pick <commit-hash>Remove a File from Git Tracking (Without Deleting It)
git rm --cached <filename>See Who Changed a Line and When
git blame <filename>
git blame -L 10,20 <filename> # lines 10 to 20 only