├── LICENSE ├── README.md └── images ├── basic.png ├── checkout.png ├── feature-branch.png ├── gitflow.png ├── gitflow_1.png ├── gitflow_3.png ├── hotfix.png ├── reset.png └── triangularflow.png /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 katam 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 1 Basics 2 | 3 | ## 1.1 creating repository 4 | 5 | - go to target project root directory 6 | 7 | ```bash 8 | git init 9 | ``` 10 | 11 | - create some file eg. some-file.md : 12 | 13 | ```bash 14 | git add some-file.md 15 | git commit -m "first commit" 16 | git remote add origin https://github.com/YOUR_USER_NAME/YOUR_REPO_NAME.git 17 | git push -u origin master 18 | ``` 19 | 20 | ## 1.2 Clone 21 | ```bash 22 | git clone https://github.com/YOUR_USER_NAME/YOUR_REPO_NAME.git 23 | ``` 24 | 25 | * If you dont need project files but only git history data you may use --mirror or --bare flags. This will only bring .git folder with repository data without any files or folders that exists in working tree : 26 | ```bash 27 | git clone --mirror https://github.com/YOUR_USER_NAME/YOUR_REPO_NAME.git 28 | ``` 29 | 30 | ## 1.3 configs 31 | 32 | - local and global configs reside : 33 | 34 | ```yml 35 | global: ~/.gitconfig 36 | local: .git/config 37 | ``` 38 | 39 | - common configs you do in company networks 40 | 41 | ```bash 42 | git config --global http.sslVerify false 43 | git config --global http.proxy http://proxyUsername:proxyPassword@proxy.server.com:port 44 | ``` 45 | 46 | - passing config on command 47 | 48 | ```bash 49 | git -c http.proxy=someproxy clone https://github.com/user/repo.git 50 | git clone -c core.autocrlf=false -c core.filemode=false https://github.com/user/repo.git 51 | ``` 52 | 53 | - unset config 54 | 55 | ```bash 56 | git config --unset core.editor 57 | ``` 58 | 59 | - edit config file manually 60 | 61 | ```bash 62 | git config --edit 63 | ``` 64 | 65 | - list configs 66 | 67 | ```bash 68 | git config --list # list all configs 69 | git config --global --list # lists only global configs 70 | ``` 71 | 72 | * Default local config file will look like this : 73 | ```yml 74 | repositoryformatversion = 0 # about git version compatilibity 75 | filemode = false # permission changes considered as change by git 76 | bare = false # working tree is not included 77 | logallrefupdates = true # enable reflog 78 | ignorecase = true # ignore file name changes for case 79 | ``` 80 | 81 | 82 | ## 1.4 Adding ssh key 83 | 84 | * Create ssh key: https://help.github.com/en/github/authenticating-to-github/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent 85 | 86 | * Adding key : https://help.github.com/en/enterprise/2.15/user/articles/adding-a-new-ssh-key-to-your-github-account 87 | 88 | * If you dont want to add ssh key and want to git remember password 89 | ```bash 90 | git config --global credential.helper store # (saved into : ~/.git-credentials); password can be read for this file!!! 91 | ``` 92 | 93 | ## 1.5 Basic Commands : 94 | 95 | ### 1.5.1 Stage and Commit 96 | 97 | * Stage 98 | ```bash 99 | git status 100 | git add path/to/file.md # stage file 101 | git reset path/to/file.md # undo 102 | git commit -m "Add file" 103 | ``` 104 | 105 | 106 | ### 1.5.2 Stash 107 | 108 | ```bash 109 | git stash # save the staged tracked files 110 | git stash list # list stashed changes, last one is on top 111 | git stash push -m "with message" # stash with message 112 | git stash push -m "single file" path/to/file.txt # stash single file 113 | git apply # applies the first but does not pop it from list 114 | git pop # applies and deletes it from stash stack 115 | ``` 116 | **Untracked files :** 117 | 118 | You can start to track them 119 | ```bash 120 | git add # add untracked file 121 | ``` 122 | 123 | Or stash them with -a or -u flag 124 | ```bash 125 | git stash -a 126 | git stash -u 127 | ``` 128 | 129 | ### 1.5.3 Patch 130 | * Create patch from stash list 131 | ```bash 132 | git stash show -p stash@{0} > mychanges.patch 133 | ``` 134 | 135 | * Create patch file from commit (this will have commit author data as well) : 136 | ```bash 137 | git format-patch -1 138 | ``` 139 | 140 | * Apply Patch : 141 | Check which files will be patched : 142 | ```bash 143 | git apply --stat mychanges.patch 144 | ``` 145 | 146 | * Check if patch file can be applicable : 147 | 148 | ```bash 149 | git apply --check mychanges.patch 150 | ``` 151 | 152 | * Just apply : 153 | ```bash 154 | git apply --3way mychanges.patch 155 | ``` 156 | Note: You better use this option if you create your patch from diff file where no commit info is included. 157 | 158 | Apply as a commit (with author etc.) : 159 | 160 | ```bash 161 | git am --3way < mychanges.patch 162 | ``` 163 | 164 | # 2 Workflows 165 | 166 | ## 2.1 Basic 167 | * Just commit and push if you are working alone in repo 168 | ```bash 169 | git add FEATURE.md 170 | git commit -m "Add FEATURE.md" 171 | git push 172 | ``` 173 | 174 | ## 2.2 Feature (Topic) Branch 175 | 176 | feature branch 180 | 181 | ```bash 182 | git checkout -b feature-branch 183 | git add FEATURE.md 184 | git commit -m "Add FEATURE.md" 185 | git push origin feature-branch 186 | ``` 187 | 188 | * Beware we haven't merge to master yet. You may open a pull request (aka merge request) to master on your browser (Github, Gitlab, Bitbutcket or whatever UI you use). You may also merge it by yourself if you are authorised to do so. Follow command below to merge with master : 189 | 190 | ```bash 191 | # if you dont merge with pull request you may use commands below : 192 | git checkout master 193 | git pull # be updated to remote 194 | git merge feature-branch 195 | git push 196 | ``` 197 | Read more : https://www.atlassian.com/git/tutorials/comparing-workflows/feature-branch-workflow 198 | 199 | ### Solving Conflict 200 | * After creating merge request; lets say we have a conflict 201 | 202 | * option 1 : merge 203 | ```bash 204 | git checkout feature-branch 205 | git merge origin/master # solve the conflict 206 | git commit 207 | git push 208 | ``` 209 | 210 | * option 2 : rebase 211 | ```bash 212 | git checkout feature-branch 213 | git rebase origin/master # solve conflicts 214 | git push -f # now history has changed on this branch, so we need to use force flag (-f) 215 | ``` 216 | ### Tradeoffs of Rebase 217 | * Pros. of Rebase : 218 | 1. It keeps your feature branch cleaner as you don't need to put few merge commits 219 | * Cons of Rebase : 220 | 1. Rewriting history. Do not use it for infinite development branches;meaning do not rebase at master. 221 | 2. Difficult to apply with branch having so much commits. Eg. rebasing development branch with 100 commits to master 30 commits behind. It does not make sense; just merge it. 222 | 223 | Read more : https://www.atlassian.com/git/tutorials/merging-vs-rebasing#the-golden-rule-of-rebasing 224 | 225 | ## 2.3 GitFlow WorkFlow 226 | 227 | gitflow branch 231 | 232 | Model proposed by Vİncent Driessen 2010 : https://nvie.com/posts/a-successful-git-branching-model/ 233 | 234 | 235 | There are 5 kinds of branches : master, develop, release, feature and hotfix. 236 | 237 | **1. Master Branch** (Immortal) 238 | * It is supposed to be always stable. 239 | * **Feature branches** never directly merges to master. 240 | * Only merges with **Release branch** and **Hotfix branch**. 241 | * It has Tags (ideally for all commits) for version numbers. 242 | 243 | **2. Develop Branch** (Immortal) 244 | * It is immortal like master branch, meaning this branch won't be deleted after merge. 245 | * It is NOT supposed to be stable. 246 | * **Feature branches** always directly merges to **Develop branch**. 247 | * **Hotfix branches** also applied to **Develop branch**. 248 | 249 | feature branch 253 | 254 | 255 | **3. Release Branch** 256 | * It is last step before merging to master 257 | * It is forked from **develop branch** 258 | * Some bug fixes can be applied here but NOT feature. 259 | * It merges to **master** and **develop branch** at same time. 260 | 261 | 262 | 263 | feature branch 267 | 268 | **4 Hotfix Branch** 269 | * Quick and critical fixes that need to be in production immadiately. 270 | * Merges to **master** and **release branch**. 271 | 272 | ```bash 273 | # Create hotfix branch 274 | git checkout -b hotfix-1.2.1 master 275 | ``` 276 | 277 | ```bash 278 | # run a script that edits version number on files if needed 279 | ./bump-version.sh 1.2.1 280 | 281 | # commit version bump 282 | git commit -a -m "Bumped version number to 1.2.1" 283 | 284 | # Do your fix, commit and push 285 | git add fixed-file.md 286 | git commit -m "Fixed severe production problem" 287 | git push origin hotfix-1.2.1 288 | 289 | ``` 290 | 291 | **5 Feature Branch** 292 | * Forked and Merged only to **develop branch** 293 | ```bash 294 | # branch from develop 295 | git checkout develop 296 | git checkout -b myfeature 297 | 298 | # ready to develop your feature 299 | git add my-feature.md 300 | git commit -m "feat: add my feature" 301 | 302 | # push your changes to remote and open pull request for review 303 | git push origin myfeature 304 | ``` 305 | 306 | If you don't apply pull request then you can merge with commands : 307 | 308 | ```bash 309 | git checkout develop 310 | 311 | # merge to develop 312 | # --no-ff option will create merge commit in all cases. Because for example if there is no conflict; merge commit won't be created in fast forward merge. 313 | git merge --no-ff myfeature 314 | 315 | # delete feature branch 316 | git branch -d myfeature 317 | git push origin develop 318 | ``` 319 | 320 | ## 2.4 Triangular Workflow 321 | 322 | triangular flow 326 | 327 | 328 | * It is used in open source project 329 | * take fork from a canonical repository 330 | 331 | ```bash 332 | git clone https://github.com/YOUR-USERNAME/atom 333 | ``` 334 | 335 | * set upstream (or name it whatever you like) 336 | ```bash 337 | git remote add upstream https://github.com/atom/atom 338 | git fetch upstream 339 | ``` 340 | 341 | 342 | * Checkout your feature branch 343 | ```bash 344 | git fetch upstream 345 | git checkout -b feature-1 upstream/master 346 | ``` 347 | 348 | ```bash 349 | # push to your fork 350 | git add change.md 351 | git commit -m "feat: add my changes" 352 | git push origin branch-name 353 | ``` 354 | 355 | ```bash 356 | git config remote.pushdefault origin 357 | git config push.default current 358 | 359 | # now no need push with origin branch-name 360 | git push 361 | ``` 362 | 363 | To fix or change remote 364 | ```bash 365 | git remote set-url origin https://hostname/USERNAME/REPOSITORY.git 366 | ``` 367 | 368 | ### During Conflict 369 | ```bash 370 | # merge 371 | git merge upstream master 372 | 373 | # or rebase 374 | git rebase upstream master 375 | ``` 376 | 377 | Check where you push 378 | ```bash 379 | git push --dry-run 380 | ``` 381 | 382 | 383 | ## 3 Cheat sheet 384 | 385 | ### 3.1 Reset vs Checkout 386 | * Reset : moves the HEAD does not leave the branch 387 | ```bash 388 | git reset # soft reset 389 | git reset --hard # clean working tree 390 | ``` 391 | * Move N commit into history 392 | ```bash 393 | git reset HEAD~4 --hard 394 | ``` 395 | * Checkout : moves the HEAD and leave the branch; so eventually you fall into detached HEAD state. 396 | ```bash 397 | git checkout path/to/file.md 398 | ``` 399 | 400 | ### 3.2 Cleaning 401 | * Clean untracked files (you files will be removed, use it carefully) 402 | ```bash 403 | git clean -f -d 404 | ``` 405 | 406 | * Have clean remote master 407 | ```bash 408 | git fetch upstream 409 | git reset --hard upstream/master 410 | git clean -df 411 | ``` 412 | 413 | * Safe cleaning with stash 414 | ```bash 415 | git add --all 416 | git stash push -m "I am cleaning my repo" 417 | ``` 418 | If you want to take it back 419 | ```bash 420 | git stash list # check your cleaning 421 | git stash apply 422 | ``` 423 | 424 | ### 3.3 Rewrite History 425 | ### 3.3.1 Amending 426 | * Change last commit : 427 | ```bash 428 | git commit --amend 429 | # usage -f flag to change on remote repo 430 | git push -f origin your-branch 431 | ``` 432 | 433 | # Commit Naming 434 | * **feat:** New feature, MINOR in semantic versioning 435 | * **feat!:** Add exclamation mark when breaking change, MAJOR in semantic versioning 436 | * **fix:** Bug fix, **PATCH** in semantic versioning 437 | * **refactor:** refactoring production code 438 | * **perf:** A code change that improves performance 439 | * **ci:** Changes to our CI configuration files and scripts (example scopes: Travis, Circle, BrowserStack, SauceLabs) 440 | * **docs:** changes in documentation 441 | * **style:** formatting, missing semi colons, etc; no code change 442 | * **test:** adding missing tests, refactoring tests; no production code change 443 | * **chore:** updating grunt tasks etc; no production code change 444 | 445 | 446 | **Resources :** 447 | * https://www.conventionalcommits.org/ 448 | * https://seesparkbox.com/foundry/semantic_commit_messages 449 | * http://karma-runner.github.io/1.0/dev/git-commit-msg.html 450 | * https://github.com/angular/angular/blob/22b96b9/CONTRIBUTING.md#-commit-message-guidelines 451 | 452 | # TODO 453 | ### 3.3.2 Revert Commit 454 | ### 3.3.3 Interactive rebase 455 | * cherry pick 456 | * reflog usage 457 | 458 | # Resources 459 | * https://github.com/tiimgreen/github-cheat-sheet 460 | * https://www.atlassian.com/git/tutorials 461 | -------------------------------------------------------------------------------- /images/basic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kerematam/git-guide/1ca3911446d008f6db133d72a379d07264a6a9b5/images/basic.png -------------------------------------------------------------------------------- /images/checkout.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kerematam/git-guide/1ca3911446d008f6db133d72a379d07264a6a9b5/images/checkout.png -------------------------------------------------------------------------------- /images/feature-branch.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kerematam/git-guide/1ca3911446d008f6db133d72a379d07264a6a9b5/images/feature-branch.png -------------------------------------------------------------------------------- /images/gitflow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kerematam/git-guide/1ca3911446d008f6db133d72a379d07264a6a9b5/images/gitflow.png -------------------------------------------------------------------------------- /images/gitflow_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kerematam/git-guide/1ca3911446d008f6db133d72a379d07264a6a9b5/images/gitflow_1.png -------------------------------------------------------------------------------- /images/gitflow_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kerematam/git-guide/1ca3911446d008f6db133d72a379d07264a6a9b5/images/gitflow_3.png -------------------------------------------------------------------------------- /images/hotfix.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kerematam/git-guide/1ca3911446d008f6db133d72a379d07264a6a9b5/images/hotfix.png -------------------------------------------------------------------------------- /images/reset.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kerematam/git-guide/1ca3911446d008f6db133d72a379d07264a6a9b5/images/reset.png -------------------------------------------------------------------------------- /images/triangularflow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kerematam/git-guide/1ca3911446d008f6db133d72a379d07264a6a9b5/images/triangularflow.png --------------------------------------------------------------------------------