├── .gitignore ├── README.md └── git_cheat_sheet.md /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | /vendor/ 3 | /build/ 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Git Cheat Sheet 2 | =============== 3 | 4 | This is a comprehensive cheat sheet for daily work with `Git`. 5 | 6 | ![Cheat Sheet Type](https://img.shields.io/badge/cheat%20sheet-git-orange.svg) 7 | ![Cheat Sheet Type](https://img.shields.io/badge/license-cc--by--sa--3.0-blue.svg) 8 | ![GitHub tag](https://img.shields.io/github/tag/pixelbrackets/git_cheat_sheet.svg) 9 | 10 | [![Preview](https://pixelbrackets.github.io/git_cheat_sheet/card.png)](https://pixelbrackets.github.io/git_cheat_sheet/) 11 | 12 | Usage 13 | ----- 14 | 15 | A webview of the cheat sheet is available at 16 | 17 | * https://pixelbrackets.github.io/git_cheat_sheet/ 18 | 19 | A PDF to print out is available at 20 | 21 | * https://pixelbrackets.github.io/git_cheat_sheet/git_cheat_sheet.pdf 22 | 23 | The cheat sheet is [written in Markdown](https://github.com/pixelbrackets/git_cheat_sheet/blob/master/git_cheat_sheet.md) 24 | and converted to HTML & PDF on every tagged release. 25 | 26 | Source 27 | ------ 28 | 29 | https://github.com/pixelbrackets/git_cheat_sheet 30 | 31 | License 32 | ------- 33 | 34 | Creative Commons Attribution ShareAlike 3.0 (CC-BY-SA 3.0) 35 | 36 | The Creative Commons License can be found at https://creativecommons.org/licenses/by-sa/3.0/ 37 | 38 | Author 39 | ------ 40 | 41 | Dan Untenzu ( / [@pixelbrackets](https://github.com/pixelbrackets)) 42 | 43 | Contribution 44 | ------------ 45 | 46 | This documentation is Open Source, so please use, patch, extend or fork it. 47 | 48 | I welcome every [Pull Request](https://github.com/pixelbrackets/git_cheat_sheet/pulls). 49 | -------------------------------------------------------------------------------- /git_cheat_sheet.md: -------------------------------------------------------------------------------- 1 | # Git Cheat Sheet 2 | 3 | * Legend: `<>` required, `[]` optional 4 | 5 | ## Common Terminology 6 | 7 | * commit ≙ object with annotated changes in relation other commits 8 | * branch ≙ collection of commits 9 | * stage ≙ files earmarked for the next commit 10 | * HEAD ≙ reference to the top commit in the current branch 11 | * remote = bookmark for a repository origin (a repository may have several remotes) 12 | 13 | ## Create & Clone 14 | 15 | * Clone an existing repository 16 | 17 | git clone [folder] 18 | 19 | _Default protocoll is SSH, eg. »git@example.com:repo.git«. HTTPS would be »https://example.com/repo.git«, another local repo »/home/user/repo.git«_ 20 | 21 | _Creates a subfolder with the repo, if the folder name is not given, then the repo name is used (»foo.git« = »./foo« subfolder)_ 22 | 23 | * Create a new local repository 24 | 25 | git init 26 | 27 | _If a folder name is given, a subfolder is created, otherwise the current folder is used_ 28 | 29 | * Send existing local repository to remote 30 | 31 | git remote add origin && git push 32 | 33 | * Special: Create an empty repository on a remote server 34 | 35 | _Connect with the remote server first_ 36 | 37 | mkdir .git && cd .git && git init --bare 38 | 39 | _The remote repository has to be »bare« (does not contain a working filetree, but a special .git subdirectory only) in order to accept a push_ 40 | 41 | ## Show changes 42 | 43 | * Show working status - show current branch name and changed or new files 44 | 45 | git status 46 | 47 | _Hint: Set a short alias for often used commands, like `git st` for `git status` → see »Configuration«_ 48 | 49 | * Difference between HEAD and files not yet staged 50 | 51 | git diff 52 | 53 | _Note: This ignores new files = files which were not added to the repository yet and therefore arent »tracked«_ 54 | 55 | * Difference between HEAD and staged files 56 | 57 | git diff --cached 58 | 59 | * Difference between HEAD and all files (staged and not staged) 60 | 61 | git diff HEAD 62 | 63 | * Difference between branches, two commits, etc 64 | 65 | git diff 66 | 67 | _»+« line does exist in »bar« but not in »foo«, »-« reverse_ 68 | 69 | * Difference to another branch and show names of changed files only 70 | 71 | git diff --name-status 72 | 73 | * Show all commits of current branch which are not merged into another branch 74 | 75 | git log .. --oneline 76 | 77 | _The reference may be a branch or a tag, note the two dots at the end_ 78 | 79 | * Show branches in which one commit exists 80 | 81 | git branch --contains 82 | 83 | ## Show history 84 | 85 | * Show all commits of current branch 86 | 87 | git log 88 | 89 | * Show all commits of current branch and names of each changed file 90 | 91 | git whatchanged 92 | 93 | * Show commits and each difference for a specific file 94 | 95 | git log -p 96 | 97 | * Examination: Show who changed what and when in a file 98 | 99 | git blame 100 | 101 | _Left side shows the last commit ID for the content on the right side_ 102 | 103 | * Show a single commit and its differences 104 | 105 | git show 106 | 107 | * Show all commits with a certain word in the commit message 108 | 109 | git log --grep= 110 | 111 | ## Commit 112 | 113 | * Stage all (even untracked) files 114 | 115 | git add -A 116 | 117 | * Stage a tracked and modified file 118 | 119 | git add 120 | 121 | * Add hand-picked changes in a file to the next commit (≙ partial commit) 122 | 123 | git add -p 124 | 125 | `y` _Yes, add this part to the next commit_ 126 | `n` _No, skip this part_ 127 | `d` _Don’t add this and all remaining parts of the file_ 128 | `s` _Try to split the current part into smaller ones_ 129 | `e` _Manually edit the part_ 130 | 131 | * Stage all changes in tracked files and start a commit 132 | 133 | git commit -a 134 | 135 | * Commit all previously staged changes 136 | 137 | git commit 138 | git commit -m "" 139 | 140 | ## Branches 141 | 142 | * List local branches 143 | 144 | git branch 145 | 146 | _`*` marks the current branch_ 147 | 148 | * List remote branches 149 | 150 | git branch -r 151 | 152 | _use `-a` to show local and remote branches at once_ 153 | 154 | * Switch to a different branch 155 | 156 | git checkout 157 | git checkout -t / 158 | 159 | _`-t` checkout a new branch based on remote branch and save their connection_ 160 | 161 | * Create a new branch based on HEAD 162 | 163 | git branch 164 | 165 | _use `git checkout -b ` to create a branch and switch right into it_ 166 | 167 | * Create a new branch based on a remote branch 168 | 169 | git branch --track / 170 | 171 | _use `--no-track` to create a new branch based on a remote branch, but don't save a connection between both_ 172 | 173 | * Connect a remote branch with a local branch 174 | 175 | git branch --track / 176 | 177 | * Show merged branches 178 | 179 | git branch -a --merged 180 | 181 | _`--no-merged` will show branches not merged yet_ 182 | 183 | * Delete a local branch 184 | 185 | git branch -d 186 | 187 | _`-d` will only delete the branch if it is merged with its remote branch (if set), `-D` will force the deletion_ 188 | 189 | * Delete a remote branch 190 | 191 | git push : 192 | 193 | ## Tags 194 | 195 | Use tags to save a specific version (the commit relations up to this point) of a project. Merging older commits into the branch afterwards hence wont affect the tag. 196 | 197 | * Show all tags 198 | 199 | git tag -n 200 | 201 | _`-l` will show tag names only, `-n` will add a number of lines from the annotation (default is one)_ 202 | 203 | * Mark the current commit with a tag 204 | 205 | git tag -m "" 206 | 207 | _Hint: Use semantic version numbers as tags_ 208 | 209 | ## Update 210 | 211 | * Download all changes from , but don't merge to HEAD yet 212 | 213 | git fetch 214 | 215 | _A manual merge is required now_ 216 | 217 | * Download changes and directly merge to HEAD 218 | 219 | git pull [ ] 220 | 221 | _If the connection between remote & local branch is saved, then `git pull` is sufficient_ 222 | 223 | * List all currently configured remote repositories 224 | 225 | git remote -v 226 | 227 | * Show information about a remote, eg. which branches exist in this remote 228 | 229 | git remote show 230 | 231 | * Remove stale remote branch trackings (outdated connections) 232 | 233 | git remote prune 234 | 235 | _Remove connections to branches deleted on the remote by now - does not delete the local branch_ 236 | 237 | * Add a new remote repository 238 | 239 | git remote add 240 | 241 | ## Publish 242 | 243 | * Push local branch or tag to remote 244 | 245 | git push [ ] 246 | 247 | _use »-u« to push the branch and automatically save the connection between local & remote_ 248 | 249 | * Push all local branches to remote 250 | 251 | git push --all 252 | 253 | * Push all tags to remote 254 | 255 | git push --tags 256 | 257 | ## Merge 258 | 259 | * Merge a branch into your current HEAD 260 | 261 | git merge 262 | 263 | * Manually solve conflicts and mark file as resolved 264 | 265 | git add && git commit -m 'Manual Merge' 266 | 267 | * Use a tool to solve merge conflicts 268 | 269 | git mergetool 270 | 271 | _will use tool set in »merge.tool«, use »-t « to start a custom tool_ 272 | 273 | * Use a merge strategy 274 | 275 | git merge -s recursive -X 276 | 277 | _»recursive« is the default merge strategy when pulling or merging one branch, so this param may be redundant_ 278 | _»ours« merge commits but try to ignore all conflicting changes from the other branch_ 279 | _»theirs« merge commits but try to ignore conflicts introduced by the own branch_ 280 | _»patience« will cause GIT run rather time-consuming intelligent merge routines to avoid merge conflicts and errors in the first place_ 281 | 282 | * Cancel merge 283 | 284 | git merge --abort 285 | 286 | ## Rebase 287 | 288 | Use rebase with care! It will rewrite the history and therefore requires additional efforts when working with a team! Dont rebase unless every project member knows about the required workflow! 289 | 290 | * Rewrite commits from HEAD until given commit 291 | 292 | git rebase -i 293 | 294 | _Opens an editable rebase command list - reorder the commands to change commit order, remove a line to delete the commit, change the preceded keyword to change the command_ 295 | 296 | `p|pick` _keep commit_ 297 | `r|reword` _use commit, but edit the commit message_ 298 | `e|edit` _use commit, but halt the rebase sequence to change the commit (use `git commit --amend -a`)_ 299 | `s|squash` _use commit, but meld into previous commit_ 300 | 301 | * Rebase your current HEAD onto 302 | 303 | git rebase 304 | 305 | _Merges all commits of given branch and applies new commits of the local branch on top (creates new commit IDs for these)_ 306 | 307 | * Abort a rebase 308 | 309 | git rebase --abort 310 | 311 | * Continue a rebase after resolving conflicts 312 | 313 | git rebase --continue 314 | 315 | ## Stash 316 | 317 | Use stash to save all current changes to a clipboard and retrieve them later. 318 | 319 | * Stash all changes away 320 | 321 | git stash save [comment] 322 | 323 | * Show all available stashes 324 | 325 | git stash list 326 | 327 | _»stash@{0}« is the rather unreadable name of the stash state, where 0 is the latest_ 328 | 329 | * Retrieve a state form the stash list 330 | 331 | git stash apply 332 | 333 | _default is »stash@{0}«, use `git stash pop ` to apply changes and remove the state from stash list_ 334 | 335 | * Remove a state from the stash list 336 | 337 | git stash drop 338 | 339 | * Remove all the stashed states 340 | 341 | git stash clear 342 | 343 | ## Revert 344 | 345 | Git is merciful and lets you undo allmost all changes with ease. 346 | 347 | * Clear stage (≙ unadd files) 348 | 349 | git reset HEAD -- 350 | 351 | * Discard all changes 352 | 353 | git checkout -- [file] 354 | 355 | * Change the last commit 356 | 357 | git commit --amend -a 358 | 359 | _Replaces the last commit (new ID), so it should only be used if the modified branch was not pushed yet_ 360 | 361 | * Special: Change author of the last commit 362 | 363 | git commit --amend --author "John Doe " 364 | 365 | * Remove the last commit but keep all files and changes 366 | 367 | git reset HEAD~1 368 | 369 | _Removes the last commit from the local history_ 370 | 371 | * Revert a commit (≙ apply inversion) 372 | 373 | git revert 374 | 375 | _Inverts changes of the given commit, applies them to the working directory and starts a new commit_ 376 | 377 | * Undo a local merge 378 | 379 | git reset --hard 380 | 381 | _Use only if the branch wasn't pushed yet, otherwise rebase or revert_ 382 | 383 | * Remove a file 384 | 385 | git rm --cached 386 | 387 | _Removes the file from the git repository index but keeps it on the file system_ 388 | 389 | ## Configuration 390 | 391 | * Get configuration option 392 | 393 | git config
. 394 | 395 | * Set configuration option 396 | 397 | git config --local
. 398 | 399 | _»local« will write to ».git/config« in current repository, »global« to »~/.gitconfig» and »system« to your systems »/etc/gitconfig«_ 400 | 401 | * Set username and e-mail 402 | 403 | git config --local user.name "" && git config --local user.email 404 | 405 | * Ignore mode changes (chmod) 406 | 407 | git config --local core.filemode false 408 | 409 | * Set alias »st« for »status« 410 | 411 | git config --global alias.st status 412 | 413 | ## Commit Message Format 414 | 415 | [BUGFIX] Short summary 416 | 417 | Optional explanatory text. Separated by new line. Wrapped to 74 chars. Written in imperative present tense ("Fix bug", not "Fixed bug"). 418 | 419 | Help others to understand what you did (Motivation for the change? Difference to previous version?), but keep it simple. 420 | 421 | Mandatory title prefix: [BUGFIX], [FEATURE] (also small additions) or [TASK] (none of the above, e.g. code cleanup). Additionall flags: [!!] (breaking change), [DB] (alter database definition), [CONF] (configuration change), [SECURITY] (fix a security issue). 422 | 423 | Bug tracker refs added at bottom (see http://is.gd/commit_refs). 424 | 425 | Resolve #42 426 | Ref #4 #8 #15 #16 427 | 428 | _shortened, detailed example at http://is.gd/commitformat_ 429 | 430 | ## Best practices 431 | 432 | * Commit related changes 433 | * Each commit should adress one logical unit. Two different bugs should result into two commits. 434 | * Commit early & often 435 | * Keep your commits small and comprehensible, split large features into logical chunks. 436 | * Test code before committing 437 | * Make sure the code works, don't guess. Or let tools test your commit automatically. Revert faulty commits if necessary. 438 | * Don't commit half-done work 439 | * Commit only complete, logical changes, not half-done chunks. »Stash« changes if applicable. 440 | * Don't commit hot files 441 | * Don't commit configuration files (commit a config template instead), personal data, temporary files (GIT is no backup system) or things that can be regenerated form other commited things. 442 | * Write good commit messages 443 | * Help others to understand what you did (Motivation for the change? Whats the difference to the previous version?) 444 | * Useless commit messages may be forwarded to whatthecommit.com 445 | * Write in imperative present tense («change», not «changed» or «changes») 446 | * A commit is a set of instructions for how to go from a previous state to a new state, so you should describe was the commit does and not what it did to your repository. 447 | * Don't panic 448 | * GIT lets you undo, fix or remove a bunch of actions 449 | * Don't change published history 450 | * GIT allows you to rewrite public history, but it is problematic for everyone and thus it is just not best practice to do so. 451 | * Use branches 452 | * Branching is cheap. Use separate branches for each bugfix, feature & idea. Make branching a part of your local workflow. 453 | * Merge regularly 454 | * Don't merge a huge feature into the master, instead merge the master regularly with your branch. 455 | * Use conventions 456 | * As with every development process: use conventions. For naming of branches and tags, how to write commit messages, when to commit into what branch, etc. 457 | 458 | ## Sources 459 | 460 | * [Git Cheat Sheet by Ying Guo](https://github.com/yguo89/RTOS/wiki/Git-Cheat-Sheet) 461 | * [Git Cheat Sheet by Git Tower](http://www.git-tower.com/files/cheatsheet/Git_Cheat_Sheet_grey.pdf) 462 | * http://gitready.com/ 463 | * http://git-scm.com/documentation 464 | * http://wiki.typo3.org/CommitMessage_Format_(Git) 465 | 466 | ## About 467 | 468 | * Supervisor: Dan Untenzu [@pixelbrackets](https://twitter.com/pixelbrackets) 469 | * License: [CC-BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/de/) 470 | * Download & Contribution: [pixelbrackets.de/git-cheat-sheet](https://pixelbrackets.de/git-cheat-sheet) 471 | --------------------------------------------------------------------------------