feat: add git cheatsheet
This commit is contained in:
472
GIT-CHEATSHEET.md
Normal file
472
GIT-CHEATSHEET.md
Normal file
@@ -0,0 +1,472 @@
|
||||
# Git Command Cheatsheet
|
||||
|
||||
A comprehensive reference for all Git commands covered in this workshop.
|
||||
|
||||
---
|
||||
|
||||
## Essentials
|
||||
|
||||
### Repository Initialization
|
||||
|
||||
```bash
|
||||
git init
|
||||
```
|
||||
Initialize a new Git repository in the current directory.
|
||||
|
||||
### Staging & Committing
|
||||
|
||||
```bash
|
||||
git status
|
||||
```
|
||||
Show the current state of your working directory and staging area.
|
||||
|
||||
```bash
|
||||
git add <file>
|
||||
```
|
||||
Add a file to the staging area (prepare it for commit).
|
||||
|
||||
```bash
|
||||
git add .
|
||||
```
|
||||
Add all changed files in the current directory to the staging area.
|
||||
|
||||
```bash
|
||||
git commit -m "message"
|
||||
```
|
||||
Create a new commit with the staged changes and a descriptive message.
|
||||
|
||||
```bash
|
||||
git commit -am "message"
|
||||
```
|
||||
Stage all modified tracked files and commit in one step (doesn't include new files).
|
||||
|
||||
### Viewing History
|
||||
|
||||
```bash
|
||||
git log
|
||||
```
|
||||
Show the commit history for the current branch.
|
||||
|
||||
```bash
|
||||
git log --oneline
|
||||
```
|
||||
Show commit history in compact one-line format.
|
||||
|
||||
```bash
|
||||
git log --oneline --graph --all
|
||||
```
|
||||
Show commit history as a graph with all branches.
|
||||
|
||||
```bash
|
||||
git log --stat
|
||||
```
|
||||
Show commit history with statistics about which files changed.
|
||||
|
||||
```bash
|
||||
git show <commit>
|
||||
```
|
||||
Display detailed information about a specific commit.
|
||||
|
||||
```bash
|
||||
git show <commit>:<file>
|
||||
```
|
||||
View the contents of a file from a specific commit.
|
||||
|
||||
### Comparing Changes
|
||||
|
||||
```bash
|
||||
git diff
|
||||
```
|
||||
Show unstaged changes in your working directory.
|
||||
|
||||
```bash
|
||||
git diff --staged
|
||||
```
|
||||
Show staged changes (what will be committed).
|
||||
|
||||
```bash
|
||||
git diff <commit1> <commit2>
|
||||
```
|
||||
Compare two commits.
|
||||
|
||||
```bash
|
||||
git diff <commit1> <commit2> <file>
|
||||
```
|
||||
Compare changes to a specific file between two commits.
|
||||
|
||||
### Branching
|
||||
|
||||
```bash
|
||||
git branch
|
||||
```
|
||||
List all local branches (current branch marked with *).
|
||||
|
||||
```bash
|
||||
git branch <branch-name>
|
||||
```
|
||||
Create a new branch.
|
||||
|
||||
```bash
|
||||
git branch -d <branch-name>
|
||||
```
|
||||
Delete a branch (only if it's been merged).
|
||||
|
||||
```bash
|
||||
git branch -D <branch-name>
|
||||
```
|
||||
Force delete a branch (even if not merged).
|
||||
|
||||
```bash
|
||||
git checkout <branch-name>
|
||||
```
|
||||
Switch to a different branch.
|
||||
|
||||
```bash
|
||||
git checkout -b <branch-name>
|
||||
```
|
||||
Create a new branch and switch to it in one command.
|
||||
|
||||
```bash
|
||||
git switch <branch-name>
|
||||
```
|
||||
Modern command to switch branches (alternative to checkout).
|
||||
|
||||
```bash
|
||||
git switch -c <branch-name>
|
||||
```
|
||||
Create a new branch and switch to it (alternative to checkout -b).
|
||||
|
||||
### Merging
|
||||
|
||||
```bash
|
||||
git merge <branch-name>
|
||||
```
|
||||
Merge the specified branch into your current branch.
|
||||
|
||||
```bash
|
||||
git merge --abort
|
||||
```
|
||||
Abort a merge in progress and return to the pre-merge state.
|
||||
|
||||
### Merge Conflict Resolution
|
||||
|
||||
```bash
|
||||
git status
|
||||
```
|
||||
During a merge conflict, shows which files have conflicts.
|
||||
|
||||
```bash
|
||||
# After resolving conflicts manually:
|
||||
git add <resolved-file>
|
||||
git commit
|
||||
```
|
||||
Stage resolved files and complete the merge.
|
||||
|
||||
---
|
||||
|
||||
## Advanced
|
||||
|
||||
### Rebasing
|
||||
|
||||
```bash
|
||||
git rebase <branch>
|
||||
```
|
||||
Reapply your commits on top of another branch (creates linear history).
|
||||
|
||||
```bash
|
||||
git rebase -i <commit>
|
||||
```
|
||||
Interactive rebase - edit, reorder, squash, or drop commits.
|
||||
|
||||
```bash
|
||||
git rebase --continue
|
||||
```
|
||||
Continue rebasing after resolving conflicts.
|
||||
|
||||
```bash
|
||||
git rebase --abort
|
||||
```
|
||||
Abort a rebase and return to the original state.
|
||||
|
||||
### Reset (History Manipulation)
|
||||
|
||||
```bash
|
||||
git reset --soft HEAD~<n>
|
||||
```
|
||||
Undo last n commits but keep changes staged.
|
||||
|
||||
```bash
|
||||
git reset --mixed HEAD~<n>
|
||||
```
|
||||
Undo last n commits and unstage changes (default mode).
|
||||
|
||||
```bash
|
||||
git reset --hard HEAD~<n>
|
||||
```
|
||||
Undo last n commits and discard all changes (DESTRUCTIVE).
|
||||
|
||||
```bash
|
||||
git reset --hard <commit>
|
||||
```
|
||||
Reset your branch to a specific commit, discarding all changes.
|
||||
|
||||
### Revert (Safe Undo)
|
||||
|
||||
```bash
|
||||
git revert <commit>
|
||||
```
|
||||
Create a new commit that undoes changes from a specific commit (safe for shared branches).
|
||||
|
||||
```bash
|
||||
git revert --no-commit <commit>
|
||||
```
|
||||
Revert changes but don't create the commit yet (allows editing).
|
||||
|
||||
```bash
|
||||
git revert --abort
|
||||
```
|
||||
Abort a revert in progress.
|
||||
|
||||
### Cherry-Pick
|
||||
|
||||
```bash
|
||||
git cherry-pick <commit>
|
||||
```
|
||||
Apply the changes from a specific commit to your current branch.
|
||||
|
||||
```bash
|
||||
git cherry-pick <commit1> <commit2> <commit3>
|
||||
```
|
||||
Cherry-pick multiple commits.
|
||||
|
||||
```bash
|
||||
git cherry-pick --continue
|
||||
```
|
||||
Continue cherry-picking after resolving conflicts.
|
||||
|
||||
```bash
|
||||
git cherry-pick --abort
|
||||
```
|
||||
Abort a cherry-pick in progress.
|
||||
|
||||
### Stash (Temporary Storage)
|
||||
|
||||
```bash
|
||||
git stash
|
||||
```
|
||||
Save your uncommitted changes temporarily and revert to a clean working directory.
|
||||
|
||||
```bash
|
||||
git stash save "description"
|
||||
```
|
||||
Stash changes with a descriptive message.
|
||||
|
||||
```bash
|
||||
git stash list
|
||||
```
|
||||
Show all stashed changes.
|
||||
|
||||
```bash
|
||||
git stash pop
|
||||
```
|
||||
Apply the most recent stash and remove it from the stash list.
|
||||
|
||||
```bash
|
||||
git stash apply
|
||||
```
|
||||
Apply the most recent stash but keep it in the stash list.
|
||||
|
||||
```bash
|
||||
git stash apply stash@{n}
|
||||
```
|
||||
Apply a specific stash by index.
|
||||
|
||||
```bash
|
||||
git stash drop
|
||||
```
|
||||
Delete the most recent stash.
|
||||
|
||||
```bash
|
||||
git stash drop stash@{n}
|
||||
```
|
||||
Delete a specific stash.
|
||||
|
||||
```bash
|
||||
git stash clear
|
||||
```
|
||||
Delete all stashes.
|
||||
|
||||
### Working with Remotes
|
||||
|
||||
```bash
|
||||
git clone <url>
|
||||
```
|
||||
Create a local copy of a remote repository.
|
||||
|
||||
```bash
|
||||
git remote
|
||||
```
|
||||
List all remote repositories.
|
||||
|
||||
```bash
|
||||
git remote -v
|
||||
```
|
||||
List all remote repositories with their URLs.
|
||||
|
||||
```bash
|
||||
git remote add <name> <url>
|
||||
```
|
||||
Add a new remote repository.
|
||||
|
||||
```bash
|
||||
git remote remove <name>
|
||||
```
|
||||
Remove a remote repository.
|
||||
|
||||
```bash
|
||||
git fetch <remote>
|
||||
```
|
||||
Download changes from remote but don't merge them.
|
||||
|
||||
```bash
|
||||
git pull <remote> <branch>
|
||||
```
|
||||
Fetch and merge changes from remote branch to current branch.
|
||||
|
||||
```bash
|
||||
git pull
|
||||
```
|
||||
Fetch and merge from the tracked remote branch.
|
||||
|
||||
```bash
|
||||
git push <remote> <branch>
|
||||
```
|
||||
Upload your commits to a remote branch.
|
||||
|
||||
```bash
|
||||
git push -u <remote> <branch>
|
||||
```
|
||||
Push and set up tracking relationship (use -u first time pushing a branch).
|
||||
|
||||
```bash
|
||||
git push
|
||||
```
|
||||
Push to the tracked remote branch.
|
||||
|
||||
### Worktrees (Multiple Working Directories)
|
||||
|
||||
```bash
|
||||
git worktree add <path> <branch>
|
||||
```
|
||||
Create a new working directory linked to your repository for a specific branch.
|
||||
|
||||
```bash
|
||||
git worktree add <path> -b <new-branch>
|
||||
```
|
||||
Create a new branch and working directory for it.
|
||||
|
||||
```bash
|
||||
git worktree list
|
||||
```
|
||||
Show all worktrees associated with the repository.
|
||||
|
||||
```bash
|
||||
git worktree remove <path>
|
||||
```
|
||||
Remove a worktree.
|
||||
|
||||
```bash
|
||||
git worktree prune
|
||||
```
|
||||
Clean up stale worktree administrative data.
|
||||
|
||||
### Bisect (Binary Search for Bugs)
|
||||
|
||||
```bash
|
||||
git bisect start
|
||||
```
|
||||
Start a bisect session to find which commit introduced a bug.
|
||||
|
||||
```bash
|
||||
git bisect bad
|
||||
```
|
||||
Mark the current commit as bad (contains the bug).
|
||||
|
||||
```bash
|
||||
git bisect good <commit>
|
||||
```
|
||||
Mark a commit as good (doesn't contain the bug).
|
||||
|
||||
```bash
|
||||
git bisect reset
|
||||
```
|
||||
End the bisect session and return to the original branch.
|
||||
|
||||
```bash
|
||||
git bisect run <script>
|
||||
```
|
||||
Automatically test commits using a script to find the bad commit.
|
||||
|
||||
---
|
||||
|
||||
## Useful Tips
|
||||
|
||||
### Viewing Changes
|
||||
|
||||
- Use `git log --oneline --graph --all` to visualize branch structure
|
||||
- Use `git log -p` to show the actual changes (patch) in each commit
|
||||
- Use `git log --author="name"` to filter commits by author
|
||||
- Use `git log --since="2 weeks ago"` to filter commits by date
|
||||
|
||||
### Undoing Changes
|
||||
|
||||
- **Haven't committed yet?** Use `git checkout -- <file>` or `git restore <file>` to discard changes
|
||||
- **Committed to wrong branch?** Use `git cherry-pick` to copy the commit to the right branch
|
||||
- **Want to change last commit message?** Use `git commit --amend`
|
||||
- **Pushed to remote already?** Use `git revert` (not reset) to safely undo
|
||||
|
||||
### Best Practices
|
||||
|
||||
- Commit often with clear, descriptive messages
|
||||
- Pull before you push to avoid conflicts
|
||||
- Use branches for new features or experiments
|
||||
- Don't rebase or reset commits that have been pushed to shared branches
|
||||
- Use `git stash` when you need to switch contexts quickly
|
||||
- Test your changes before committing
|
||||
|
||||
### Getting Help
|
||||
|
||||
```bash
|
||||
git help <command>
|
||||
```
|
||||
Show detailed help for any Git command.
|
||||
|
||||
```bash
|
||||
git <command> --help
|
||||
```
|
||||
Alternative way to show help.
|
||||
|
||||
```bash
|
||||
git <command> -h
|
||||
```
|
||||
Show brief usage summary.
|
||||
|
||||
---
|
||||
|
||||
## Module Reference
|
||||
|
||||
Each workshop module focuses on specific commands:
|
||||
|
||||
- **Module 01**: init, add, commit, status
|
||||
- **Module 02**: log, show, diff
|
||||
- **Module 03**: branch, checkout/switch
|
||||
- **Module 04**: merge (fast-forward and three-way)
|
||||
- **Module 05**: merge (conflict resolution)
|
||||
- **Module 06**: rebase
|
||||
- **Module 07**: reset (interactive rebase alternative)
|
||||
- **Module 08**: cherry-pick
|
||||
- **Module 09**: reset vs revert
|
||||
- **Module 10**: stash
|
||||
- **Module 11**: clone, remote, push, pull, fetch
|
||||
- **Module 12**: worktree
|
||||
- **Module 13**: bisect
|
||||
@@ -36,6 +36,8 @@ Each module is a self-contained challenge that teaches specific git concepts:
|
||||
5. Run `./verify.ps1` to check if you've solved it correctly
|
||||
6. Move to the next module
|
||||
|
||||
**Quick Reference**: See [GIT-CHEATSHEET.md](GIT-CHEATSHEET.md) for a comprehensive list of all Git commands covered in this workshop.
|
||||
|
||||
## Requirements
|
||||
|
||||
- Git installed and configured
|
||||
@@ -98,6 +100,7 @@ The modules are designed to build on each other:
|
||||
|
||||
- Don't skip modules - each builds on previous concepts
|
||||
- Read the README.md thoroughly before starting each challenge
|
||||
- Keep [GIT-CHEATSHEET.md](GIT-CHEATSHEET.md) open as a quick reference
|
||||
- Experiment freely - you can always run `./reset.ps1` to start over
|
||||
- Use `git log --oneline --graph --all` frequently to visualize repository state
|
||||
- If stuck, check the Key Concepts section in the module's README
|
||||
|
||||
Reference in New Issue
Block a user