|
| 1 | +# 02 Git In-Depth |
| 2 | + |
| 3 | +Author: Fasermaler |
| 4 | + |
| 5 | +This sub-guide will cover Git concepts in greater depth. Additionally, it will also cover the use of GitKraken and Git Commands. |
| 6 | + |
| 7 | +## Pre-requisites |
| 8 | + |
| 9 | +- Basic bash knowledge |
| 10 | +- Interest in learning how to *git gud* |
| 11 | + |
| 12 | +## Introduction |
| 13 | + |
| 14 | +While GitHub itself is very user friendly, the true power of Git lies in being able to work on a local version of a Git Repository. To do this would require greater knowledge of Git concepts and as well as the use of the Git Command Line or a GUI like GitKraken. |
| 15 | + |
| 16 | +In this guide, we will cover Git concepts in greater depth and how to conduct them in command line or via GitKraken. The sections will cover the concept, followed by command line before GitKraken to allow the reader to greater appreciate the simplicity of Git Kraken. |
| 17 | + |
| 18 | +## Repository |
| 19 | + |
| 20 | +What is a repository? A repository (also known as a *repo*) is a basically a Git workspace. This is where working code can be found, *cloned* (downloaded) from and changes can be made by *committing* them. Ideally a Git repository contains the full working version of the code - such that at any point of time, the *origin* or *master* branch can be run as the latest stable version of the code. |
| 21 | + |
| 22 | +Relative to your workstation, there is a single *local* repository in the HDD. Every other repository (on the internet, on another person's computer) is referred to as a *remote* repository. |
| 23 | + |
| 24 | +### Initialize a Repository (CLI) |
| 25 | + |
| 26 | +The first way to create a new repository is to simply initialize a local repository. Simply navigate to the directory you want and call the following command: |
| 27 | + |
| 28 | +```bash |
| 29 | +$ git init |
| 30 | +``` |
| 31 | + |
| 32 | +This will tell git to create a hidden `.git` folder that stores all information about the git repository. In-depth information on how the `.git` folder will be covered later. |
| 33 | + |
| 34 | +### Clone a Remote Repository (CLI) |
| 35 | + |
| 36 | +The other method is to clone a remote repository: |
| 37 | + |
| 38 | +```shell |
| 39 | +$ git clone <addresss> |
| 40 | +``` |
| 41 | + |
| 42 | +The address can be a github URL of the **main workspace directory** or you can SSH to another machine to get someone else's git repository. |
| 43 | + |
| 44 | +Examples of addresses: |
| 45 | + |
| 46 | +```shell |
| 47 | +$ git clone https://github.com/Fasermaler/coding-notes # clone my coding notes repo! |
| 48 | + |
| 49 | +$ git clone ssh://somemachine/path/to/git/foo.git # clone a repo over ssh |
| 50 | +``` |
| 51 | + |
| 52 | +### Recurse Submodules |
| 53 | + |
| 54 | +Initialize submodules upon initialization: |
| 55 | + |
| 56 | +```shell |
| 57 | +$ git clone --recurse-submodules <address> |
| 58 | +``` |
| 59 | + |
| 60 | +### Set Target Directory |
| 61 | + |
| 62 | +```shell |
| 63 | +$ git clone <address> <directory> # clones the repo into a new directory |
| 64 | +``` |
| 65 | + |
| 66 | +#### Set Branch |
| 67 | + |
| 68 | +Set the branch of the cloned repository: |
| 69 | + |
| 70 | +```shell |
| 71 | +$ git clone -b <branch-name> <address> |
| 72 | +``` |
| 73 | + |
| 74 | +### Set Origin |
| 75 | + |
| 76 | +Set an origin other than *origin*: |
| 77 | + |
| 78 | +```shell |
| 79 | +$ git clone -o <origin-name> <address> |
| 80 | +``` |
| 81 | + |
| 82 | +### GitKraken |
| 83 | + |
| 84 | +In GitKraken, managing repositories is simple: |
| 85 | + |
| 86 | +1. Click on the folder icon in the top left hand corner |
| 87 | + |
| 88 | +  |
| 89 | + |
| 90 | +2. In the *repository management* window, you can |
| 91 | + |
| 92 | + - Open any local repository |
| 93 | + - Clone a remote repository via URL or the various online repository hosts |
| 94 | + - Initialize a local or remote repository (you then have the option to immediately clone it into your local HDD) |
| 95 | + |
| 96 | +3. When cloning a repository with submodules, GitKraken will ask if you would like to clone the submodules as well. If you choose *no*, you can add the submodules later in the left hand panel. |
| 97 | + |
| 98 | +## Commit |
| 99 | + |
| 100 | +A commit is the act of committing a change a local copy of the repository. Remember that no matter what, a branch will exist within a repository (in most cases the *origin* branch is the default). |
| 101 | + |
| 102 | +When you commit a change, you are simply telling Git that the changes you have made are worthy of creating a snapshot of the current iteration of the files you have changed. It does not affect the remote repository that everyone accesses. To do that you will have to *push* the changes. |
| 103 | + |
| 104 | +### Check Status (CLI) |
| 105 | + |
| 106 | +Check the current status of the repository. This will let you know if the repository is out of date, if there are existing commits and etc. |
| 107 | + |
| 108 | +```shell |
| 109 | +$ git status |
| 110 | +``` |
| 111 | + |
| 112 | +Sample output of a clean repository: |
| 113 | + |
| 114 | +```shell |
| 115 | +On branch master |
| 116 | +Your branch is up-to-date with 'origin/master'. |
| 117 | +nothing to commit, working directory clean |
| 118 | +``` |
| 119 | + |
| 120 | +### Stage Files (CLI) |
| 121 | + |
| 122 | +Before files can be committed, they have to be *staged*. Staging is a process where the files required in the commit can be assembled and curated before the commit action actually takes place. This allows the developer to select which files actually get committed instead of having all new files be committed. |
| 123 | + |
| 124 | +```shell |
| 125 | +$ git add <file-name> # Add one file to the staging area |
| 126 | + |
| 127 | +$ git add -A # Add all changes to the staging area |
| 128 | + |
| 129 | +$ git rm -r <file-name> # Remove a file permanently from the repo |
| 130 | +``` |
| 131 | + |
| 132 | +### Check Staged Files (CLI) |
| 133 | + |
| 134 | +These commands allow you to check what files have been staged if you lose track of them: |
| 135 | + |
| 136 | +```shell |
| 137 | +$ git diff # Gets the difference between staged files and workspace |
| 138 | + |
| 139 | +$ git diff --name-only # Show only names of cached files |
| 140 | + |
| 141 | +$ git diff --cached # Show changes between the index and your last commit |
| 142 | + |
| 143 | +$ git diff --name-only --cached # Show names of cached files changed between index and last commit - nifty eh? |
| 144 | +``` |
| 145 | + |
| 146 | +### Committing Staged Files (CLI) |
| 147 | + |
| 148 | +Once everything is in order, proceed to commit the staged files: |
| 149 | + |
| 150 | +```shell |
| 151 | +$ git commit -m "<commit message>" |
| 152 | +``` |
| 153 | + |
| 154 | +More guidelines on how to write commit messages will be provided later. |
| 155 | + |
| 156 | +### GitKraken |
| 157 | + |
| 158 | +In GitKraken, active changes are tracked at the top of the repository tree and labeled as `// WIP`: |
| 159 | + |
| 160 | + |
| 161 | + |
| 162 | +Files are color coded for convenience: |
| 163 | + |
| 164 | +- Orange: Modified files |
| 165 | +- Green: Newly added files |
| 166 | +- Red: Removed Files |
| 167 | +- Blue: Renamed files |
| 168 | + |
| 169 | +On the right panel, select the files to be staged and write the commit message. |
| 170 | + |
| 171 | + |
| 172 | + |
| 173 | +If there are too many files, swapping to *Tree* mode (located at the top of the panel) would use a folder tree structure. Right click a folder to see more options such as staging all files within the folder. |
| 174 | + |
| 175 | +When the commit is completed, the `// WIP` will be changed to the new commit (unless some files are not committed, then they will form the new `// WIP` commit). |
| 176 | + |
| 177 | + |
| 178 | + |
| 179 | +The repository tree will reflect that the local *master* branch is ahead of the remote *master* branch. |
| 180 | + |
| 181 | +Select *Push* in the toolbar on the top to push the commit. You should also *Pull* the latest changes before pushing the commit. |
| 182 | + |
| 183 | +## Pull |
| 184 | + |
| 185 | +Pull is exactly what it sounds like, pulling the latest changes for a repository. This means that Git will pull the latest changes from a remote repository and update your local repository. |
| 186 | + |
| 187 | +In most cases there will only be one remote repository you are working with (usually on GitHub) and one working repository on your local HDD. But this might not be the case in larger projects. It is possible to pull changes from another team member or another fork. |
| 188 | + |
| 189 | +### Command Line |
| 190 | + |
| 191 | +```shell |
| 192 | +$ git pull # Get changes for the current branch |
| 193 | + |
| 194 | +$ git pull <origin> <branch> # Get changes from a specific branch from origin |
| 195 | +``` |
| 196 | + |
| 197 | +The difference between `git pull` and having the extra options is that `git pull` will simply pull changes from the current active *remote* repository. While each extra option will specify which origin and branch to fetch changes from. |
| 198 | + |
| 199 | +For instance, if the current repository and branch you are working on is *local/feature1*, then calling `git pull` will (usually) pull changes from *origin/feature1*. But if you specific `git pull origin release1`, the changes will be pulled from the *origin* release1 branch instead. |
| 200 | + |
| 201 | +### GitKraken |
| 202 | + |
| 203 | +Pulling in GitKraken is as simple as pressing the *Pull* button on the toolbar. Though other options exist: |
| 204 | + |
| 205 | + |
| 206 | + |
| 207 | +Fast-forwarding will be covered later in the Git workflow section. For now, the default option is fine. |
| 208 | + |
| 209 | +## Pull Request |
| 210 | + |
| 211 | +A pull request is a request for another remote repository to pull changes from from your own local repository. In essence, it is a request for another repository to accept changes you have made to the code. In open-source software development, it is used when you do not have access to the actual repository. |
| 212 | + |
| 213 | +In internal production, pull-requests might be used to ensure that all changes are screened by the repository maintainer before anything gets changed in the production code. This can minimize the chances of people breaking the code or worse, destroying the production repository. |
| 214 | + |
| 215 | +*Take note that pull and pull request are actually quite different from each other even though they sound similar* |
| 216 | + |
| 217 | +Take note that to do a pull request from command line requires an extension called `hub` which will not be covered in this guide. |
| 218 | + |
| 219 | +In most cases you can issue a pull request directly via the GitHub UI. This will be covered in a later section. |
| 220 | + |
| 221 | +## Push |
| 222 | + |
| 223 | +After a *commit* a push is used to push changes to a remote repository. |
| 224 | + |
| 225 | +If there are conflicts - which means that the file has been modified while you were in the process of editing it, Git will fail to push. You can adopt to *Force Push* if you'd like to override the changes. More on dealing with conflicts later. |
| 226 | + |
| 227 | +*Note: Always pull before pushing to get the latest version.* |
| 228 | + |
| 229 | +**Warning: Force Push is a destructive action, it's use is not recommended. I have left it here for posterity.** |
| 230 | + |
| 231 | +### Command Line |
| 232 | + |
| 233 | +##### Push to Remembered Branch |
| 234 | + |
| 235 | +```shell |
| 236 | +$ git push # Push changes to the remembered repository |
| 237 | +``` |
| 238 | + |
| 239 | +##### Push to Specific Branch |
| 240 | + |
| 241 | +```shell |
| 242 | +$ git push origin <branch-name> # Push changes to specific branch |
| 243 | + |
| 244 | +$ git push -u origin <branch-name> # Push changes to specific branch and remember it |
| 245 | +``` |
| 246 | + |
| 247 | +##### Delete Specific Branch |
| 248 | + |
| 249 | +```shell |
| 250 | +$ git push origin --delete <branch-name> # Delete a specific branch |
| 251 | +``` |
| 252 | + |
| 253 | +## Branch |
| 254 | + |
| 255 | +If a repository is a tree, branches are the different versions of the working code. In the Git workflow, the *master* branch is reserved for working versions of the code, while other branches are used to modify the code, develop new features or fixed. |
| 256 | + |
| 257 | +Unlike tree branches, branches in Git can be *merged* with each other. This allows the *master* branch to effectively be updated with new features or bug fixes when they are complete. |
| 258 | + |
| 259 | +### Command Line |
| 260 | + |
| 261 | +##### Create a Branch |
| 262 | + |
| 263 | +```shell |
| 264 | +$ git branch <branch-name> |
| 265 | +``` |
| 266 | + |
| 267 | +##### Delete a Branch |
| 268 | + |
| 269 | +```shell |
| 270 | +$ git branch -d <branch-name> |
| 271 | +``` |
| 272 | + |
| 273 | +##### Delete a Remote Branch |
| 274 | + |
| 275 | +```shell |
| 276 | +$ git push origin --delete <branch-name> |
| 277 | +``` |
| 278 | + |
| 279 | +##### List Branches |
| 280 | + |
| 281 | +```shell |
| 282 | +$ git branch |
| 283 | +``` |
| 284 | + |
| 285 | +### GitKraken |
| 286 | + |
| 287 | +To create a branch in GitKraken, just right click any specific commit and select *Create Branch Here*. Then name the branch accordingly. |
| 288 | + |
| 289 | +The branch will not be visualized until divergent commits have been made between the *master* branch and the new branch. |
| 290 | + |
| 291 | +To delete a branch, click the small triple dotted area of a branch in the left-hand panel. Then select delete. This can be done for both local and remote branches. |
| 292 | + |
| 293 | +## Checkout |
| 294 | + |
| 295 | +A checkout is when you intend to swap to a specific branch. Unlike a normal tree, branches actually do not exists as separate files. Effectively each branch is a version of the same workspace - thus to get to the workspace you want, you checkout to a specific branch and it modifies your workspace accordingly. |
| 296 | + |
| 297 | +### Command Line |
| 298 | + |
| 299 | +##### Swap to a Branch |
| 300 | + |
| 301 | +```shell |
| 302 | +$ git checkout <branch-name> |
| 303 | +``` |
| 304 | + |
| 305 | +##### Switch to the Branch last Checked Out |
| 306 | + |
| 307 | +```shell |
| 308 | +$ git checkout -- |
| 309 | +``` |
| 310 | + |
| 311 | +##### Create New Branch and Switch to It |
| 312 | + |
| 313 | +```shell |
| 314 | +$ git checkout -b <branch-name> |
| 315 | +``` |
| 316 | + |
| 317 | +##### Clone a Remote Branch and Switch to it |
| 318 | + |
| 319 | +```shell |
| 320 | +$ git checkout -b <branch-name> origin/<branch> |
| 321 | +``` |
| 322 | + |
| 323 | +### GitKraken |
| 324 | + |
| 325 | +To checkout a branch in GitKraken, simply double click the branch itself in the left panel. Clicking a branch in the remote repository will cause GitKraken to checkout the associated local branch. |
| 326 | + |
| 327 | +## Merge |
| 328 | + |
| 329 | +Merging is the act of combining 2 branches together and taking the aggregate of their changes. In general, this is done when a feature branch has completed implementation of a new feature and is ready to be merged with the main production code after testing. |
| 330 | + |
| 331 | +Merge conflicts may occur as the result of improper division of work, resulting in multiple local repositories modifying the same file. More on how to resolve conflicts later. |
| 332 | + |
| 333 | +### Command Line |
| 334 | + |
| 335 | +```shell |
| 336 | +$ git merge <branch-name> # Merge a branch into the currently active branch |
| 337 | + |
| 338 | +$ git merge <source-branch> <target-branch> # Specifies which 2 branches to merge |
| 339 | +``` |
| 340 | + |
| 341 | +### GitKraken |
| 342 | + |
| 343 | +To merge 2 branches, checkout into the branch you intend to merge to and then select the branch actions (triple dotted area) of branch you intend to merge from. Barring conflicts, the merge should be complete. |
| 344 | + |
| 345 | +## Conclusion |
| 346 | + |
| 347 | +Congratulations! You have learnt nearly everything essential to making Git a part and parcel of your everyday software development. Feel free to stop here - the next section will covered more advanced concepts that are situationally useful but will nonetheless broaden your understanding of Git if you choose to continue. |
0 commit comments