$npx -y skills add LobsterTrap/lola --skill git-cheatsheetUse this skill when working with git commands, version control, or repository management
| 1 | # Git Cheatsheet |
| 2 | |
| 3 | ## Getting Started |
| 4 | |
| 5 | ### Start a new repo: |
| 6 | |
| 7 | `git init` |
| 8 | |
| 9 | ### Clone an existing repo: |
| 10 | |
| 11 | `git clone <url>` |
| 12 | |
| 13 | ## Prepare to Commit |
| 14 | |
| 15 | ### Add untracked file or unstaged changes: |
| 16 | |
| 17 | `git add <file>` |
| 18 | |
| 19 | ### Add all untracked files and unstaged changes: |
| 20 | |
| 21 | `git add .` |
| 22 | |
| 23 | ### Choose which parts of a file to stage: |
| 24 | |
| 25 | `git add -p` |
| 26 | |
| 27 | ### Move file: |
| 28 | |
| 29 | `git mv <old> <new>` |
| 30 | |
| 31 | ### Delete file: |
| 32 | |
| 33 | `git rm <file>` |
| 34 | |
| 35 | ### Tell Git to forget about a file without deleting it: |
| 36 | |
| 37 | `git rm --cached <file>` |
| 38 | |
| 39 | ### Unstage one file: |
| 40 | |
| 41 | `git reset <file>` |
| 42 | |
| 43 | ### Unstage everything: |
| 44 | |
| 45 | `git reset` |
| 46 | |
| 47 | ### Check what you added: |
| 48 | |
| 49 | `git status` |
| 50 | |
| 51 | ## Make Commits |
| 52 | |
| 53 | ### Make a commit (and open text editor to write message): |
| 54 | |
| 55 | `git commit` |
| 56 | |
| 57 | ### Make a commit: |
| 58 | |
| 59 | `git commit -m 'message'` |
| 60 | |
| 61 | ### Commit all unstaged changes: |
| 62 | |
| 63 | `git commit -am 'message'` |
| 64 | |
| 65 | ## Move Between Branches |
| 66 | |
| 67 | ### Switch branches: |
| 68 | |
| 69 | `git switch <name>` OR `git checkout <name>` |
| 70 | |
| 71 | ### Create a branch: |
| 72 | |
| 73 | `git switch -c <name>` OR `git checkout -b <name>` |
| 74 | |
| 75 | ### List branches: |
| 76 | |
| 77 | `git branch` |
| 78 | |
| 79 | ### List branches by most recently committed to: |
| 80 | |
| 81 | `git branch --sort=-committerdate` |
| 82 | |
| 83 | ### Delete a branch: |
| 84 | |
| 85 | `git branch -d <name>` |
| 86 | |
| 87 | ### Force delete a branch: |
| 88 | |
| 89 | `git branch -D <name>` |
| 90 | |
| 91 | ## Diff Staged/Unstaged Changes |
| 92 | |
| 93 | ### Diff all staged and unstaged changes: |
| 94 | |
| 95 | `git diff HEAD` |
| 96 | |
| 97 | ### Diff just staged changes: |
| 98 | |
| 99 | `git diff --staged` |
| 100 | |
| 101 | ### Diff just unstaged changes: |
| 102 | |
| 103 | `git diff` |
| 104 | |
| 105 | ## Diff Commits |
| 106 | |
| 107 | ### Show diff between a commit and its parent: |
| 108 | |
| 109 | `git show <commit>` |
| 110 | |
| 111 | ### Diff two commits: |
| 112 | |
| 113 | `git diff <commit> <commit>` |
| 114 | |
| 115 | ### Diff one file since a commit: |
| 116 | |
| 117 | `git diff <commit> <file>` |
| 118 | |
| 119 | ### Show a summary of a diff: |
| 120 | |
| 121 | `git diff <commit> --stat` `git show <commit> --stat` |
| 122 | |
| 123 | ## Ways to refer to a commit |
| 124 | |
| 125 | Every time we say `<commit>`, you can use any of these: |
| 126 | |
| 127 | a branch |
| 128 | |
| 129 | main |
| 130 | |
| 131 | a tag |
| 132 | |
| 133 | v0.1 |
| 134 | |
| 135 | a commit ID |
| 136 | |
| 137 | 3e887ab |
| 138 | |
| 139 | a remote branch |
| 140 | |
| 141 | origin/main |
| 142 | |
| 143 | current commit |
| 144 | |
| 145 | HEAD |
| 146 | |
| 147 | 3 commits ago |
| 148 | |
| 149 | HEAD^^^ or HEAD~3 |
| 150 | |
| 151 | ## Discard Your Changes |
| 152 | |
| 153 | ### Delete unstaged changes to one file: |
| 154 | |
| 155 | `git restore <file>` OR `git checkout <file>` |
| 156 | |
| 157 | ### Delete all staged and unstaged changes to one file: |
| 158 | |
| 159 | `git restore --staged --worktree <file>` OR `git checkout HEAD <file>` |
| 160 | |
| 161 | ### Delete all staged and unstaged changes: |
| 162 | |
| 163 | `git reset --hard` |
| 164 | |
| 165 | ### Delete untracked files: |
| 166 | |
| 167 | `git clean` |
| 168 | |
| 169 | ### 'Stash' all staged and unstaged changes: |
| 170 | |
| 171 | `git stash` |
| 172 | |
| 173 | ## Edit History |
| 174 | |
| 175 | ### "Undo" the most recent commit (keep your working directory the same): |
| 176 | |
| 177 | `git reset HEAD^` |
| 178 | |
| 179 | ### Squash the last 5 commits into one: |
| 180 | |
| 181 | `git rebase -i HEAD~6` |
| 182 | |
| 183 | Then change "pick" to "fixup" for any commit you want to combine with the previous one |
| 184 | |
| 185 | ### Undo a failed rebase: |
| 186 | |
| 187 | `git reflog BRANCHNAME` |
| 188 | |
| 189 | Then manually find the right commit ID in the reflog, then run: |
| 190 | |
| 191 | `git reset --hard <commit>` |
| 192 | |
| 193 | ### Change a commit message (or add a file you forgot): |
| 194 | |
| 195 | `git commit --amend` |
| 196 | |
| 197 | ## Code Archaeology |
| 198 | |
| 199 | ### Look at a branch's history: |
| 200 | |
| 201 | `git log main` `git log --graph main` `git log --oneline` |
| 202 | |
| 203 | ### Show every commit that modified a file: |
| 204 | |
| 205 | `git log <file>` |
| 206 | |
| 207 | ### Show every commit that modified a file, including before it was renamed: |
| 208 | |
| 209 | `git log --follow <file>` |
| 210 | |
| 211 | ### Find every commit that added or removed some text: |
| 212 | |
| 213 | `git log -G banana` |
| 214 | |
| 215 | ### Show who last changed each line of a file: |
| 216 | |
| 217 | `git blame <file>` |
| 218 | |
| 219 | ## Combine Diverged Branches |
| 220 | |
| 221 | ### Combine with rebase: |
| 222 | |
| 223 | `git switch banana` `git rebase main`   |
| 224 | |
| 225 | ### Combine with merge: |
| 226 | |
| 227 | `git switch main` `git merge banana`   |
| 228 | |
| 229 | ### Combine with squash merge: |
| 230 | |
| 231 | `git switch main` `git merge --squash banana` `git commit`   |
| 232 | |
| 233 | ### Bring a branch up to date with another branch (aka "fast-forward merge"): |
| 234 | |
| 235 | `git switch main` `git merge banana`   |
| 236 | |
| 237 | ### Copy one commit onto the current branch: |
| 238 | |
| 239 | `git cherry-pick <commit>`   |
| 240 | |
| 241 | ## Restore an Old File |
| 242 | |
| 243 | ### Get the version of a file from another commit: |
| 244 | |
| 245 | `git checkout <commit> <file>` OR `git restore <file> --source <commit>` |
| 246 | |
| 247 | ## Add a Remote |
| 248 | |
| 249 | `git remote add <name> <url>` |
| 250 | |
| 251 | ## Push Your Changes |
| 252 | |
| 253 | ### Push the main branch to the remote origin: |
| 254 | |
| 255 | `git push origin main` |
| 256 | |
| 257 | ### Push the current branch to its remote "tracking branch": |
| 258 | |
| 259 | `git push` |
| 260 | |
| 261 | ### Push a branch that you've never pushed before: |
| 262 | |
| 263 | `git push -u origin <name>` |
| 264 | |
| 265 | ### Force push: |
| 266 | |
| 267 | `git push --force-with-lease` |
| 268 | |
| 269 | ### Push tags: |
| 270 | |
| 271 | `git push --tags` |
| 272 | |
| 273 | ## Pull Changes |
| 274 | |
| 275 | ### Fetch changes (but don't change any of your local branches): |
| 276 | |
| 277 | `git fetch origin main` |
| 278 | |
| 279 | ### Fetch changes and then rebase your current branch: |
| 280 | |
| 281 | `git pull --rebase` |
| 282 | |
| 283 | ### Fetch changes and then merge them into your current branch: |
| 284 | |
| 285 | `git pull origin main` OR `git pull` |
| 286 | |
| 287 | ## Configure Git |
| 288 | |
| 289 | ### Set a config option: |
| 290 | |
| 291 | `git config user.name 'Your Name'` |
| 292 | |
| 293 | ### Set option globally: |
| 294 | |
| 295 | `git config --global ...` |
| 296 | |
| 297 | ### Add an alias: |