README.md 8.94 KB
Newer Older
1 2 3 4
---
comments: false
---

5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
# Training

This training material is the markdown used to generate training slides
which can be found at [End User Slides](https://gitlab-org.gitlab.io/end-user-training-slides/#/)
through it's [RevealJS](https://gitlab.com/gitlab-org/end-user-training-slides)
project.

---

## Git Intro

---

### What is a Version Control System (VCS)

- Records changes to a file
- Maintains history of changes
- Disaster Recovery
- Types of VCS: Local, Centralized and Distributed

---

### Short Story of Git

Pascal Borreli's avatar
Pascal Borreli committed
29
- 1991-2002: The Linux kernel was being maintained by sharing archived files
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
  and patches.
- 2002: The Linux kernel project began using a DVCS called BitKeeper
- 2005: BitKeeper revoked the free-of-charge status and Git was created

---

### What is Git

- Distributed Version Control System
- Great branching model that adapts well to most workflows
- Fast and reliable
- Keeps a complete history
- Disaster recovery friendly
- Open Source

---

### Getting Help

- Use the tools at your disposal when you get stuck.
50
  - Use `git help <command>` command
51
  - Use Google (i.e. StackOverflow, Google groups)
52
  - Read documentation at <https://git-scm.com>
53 54 55 56 57 58 59 60 61 62 63

---

## Git Setup
Workshop Time!

---

### Setup

- Windows: Install 'Git for Windows'
64
  - <https://git-for-windows.github.io>
65
- Mac: Type `git` in the Terminal application.
66 67
  - If it's not installed, it will prompt you to install it.
- Linux
68 69
  - Debian: `sudo apt-get install git-all`
  - Red Hat `sudo yum install git-all`
70 71 72 73 74

---

### Configure

75
- One-time configuration of the Git client:
76

77
```bash
78 79
git config --global user.name "Your Name"
git config --global user.email you@example.com
80
```
81

82
- If you don't use the global flag you can set up a different author for
83
  each project
84
- Check settings with:
85

86
```bash
87
git config --global --list
88
```
89 90 91 92 93 94 95 96 97 98 99 100 101
- You might want or be required to use an SSH key.
    - Instructions: [SSH](http://doc.gitlab.com/ce/ssh/README.html)

---

### Workspace

- Choose a directory on you machine easy to access
- Create a workspace or development directory
- This is where we'll be working and adding content

---

102
```bash
103 104 105 106 107 108
mkdir ~/development
cd ~/development

-or-

mkdir ~/workspace
109
cd ~/workspace
110
```
111 112 113 114 115

---

## Git Basics

116
---
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137

### Git Workflow

- Untracked files
    - New files that Git has not been told to track previously.
- Working area (Workspace)
    - Files that have been modified but are not committed.
- Staging area (Index)
    - Modified files that have been marked to go in the next commit.
- Upstream
    - Hosted repository on a shared server

---

### GitLab

- GitLab is an application to code, test and deploy.
- Provides repository management with access controls, code reviews,
  issue tracking, Merge Requests, and other features.
- The hosted version of GitLab is gitlab.com

138
---
139 140 141 142 143

### New Project

- Sign in into your gitlab.com account
- Create a project
144
- Choose to import from 'Any Repo by URL' and use <https://gitlab.com/gitlab-org/training-examples.git>
145
- On your machine clone the `training-examples` project
146 147 148 149 150

---

### Git and GitLab basics

151
1. Edit `edit_this_file.rb` in `training-examples`
152 153 154 155 156 157
1. See it listed as a changed file (working area)
1. View the differences
1. Stage the file
1. Commit
1. Push the commit to the remote
1. View the git log
158 159 160

---

161
```shell
162 163 164 165 166 167 168
# Edit `edit_this_file.rb`
git status
git diff
git add <file>
git commit -m 'My change'
git push origin master
git log
169
```
170

171
---
172 173 174

### Feature Branching

175
1. Create a new feature branch called `squash_some_bugs`
176 177 178
1. Edit `bugs.rb` and remove all the bugs.
1. Commit
1. Push
179 180 181

---

182
```shell
183 184 185 186 187 188
git checkout -b squash_some_bugs
# Edit `bugs.rb`
git status
git add bugs.rb
git commit -m 'Fix some buggy code'
git push origin squash_some_bugs
189
```
190 191 192 193 194 195 196 197 198 199 200 201

---

## Merge Request

---

### Merge requests

- When you want feedback create a merge request
- Target is the ‘default’ branch (usually master)
- Assign or mention the person you would like to review
202
- Add `WIP` to the title if it's a work in progress
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
- When accepting, always delete the branch
- Anyone can comment, not just the assignee
- Push corrections to the same branch

---

### Merge request example

- Create your first merge request
  - Use the blue button in the activity feed
  - View the diff (changes) and leave a comment
  - Push a new commit to the same branch
  - Review the changes again and notice the update

---

### Feedback and Collaboration

- Merge requests are a time for feedback and collaboration
- Giving feedback is hard
- Be as kind as possible
- Receiving feedback is hard
- Be as receptive as possible
- Feedback is about the best code, not the person. You are not your code
- Feedback and Collaboration

---

### Feedback and Collaboration

- Review the Thoughtbot code-review guide for suggestions to follow when reviewing merge requests:[Thoughtbot](https://github.com/thoughtbot/guides/tree/master/code-review)
- See GitLab merge requests for examples: [Merge Requests](https://gitlab.com/gitlab-org/gitlab-ce/merge_requests)

---

## Merge Conflicts

---

### Merge Conflicts
243 244 245 246 247

- Happen often
- Learning to fix conflicts is hard
- Practice makes perfect
- Force push after fixing conflicts. Be careful!
248 249 250 251

---

### Example Plan
252

253
1. Checkout a new branch and edit conflicts.rb. Add 'Line4' and 'Line5'.
254 255 256 257 258 259 260 261 262
1. Commit and push
1. Checkout master and edit conflicts.rb. Add 'Line6' and 'Line7' below 'Line3'.
1. Commit and push to master
1. Create a merge request and watch it fail
1. Rebase our new branch with master
1. Fix conflicts on the conflicts.rb file.
1. Stage the file and continue rebasing
1. Force push the changes
1. Finally continue with the Merge Request
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307

---

### Example 1/2

    git checkout -b conflicts_branch

    # vi conflicts.rb
    # Add 'Line4' and 'Line5'

    git commit -am "add line4 and line5"
    git push origin conflicts_branch

    git checkout master

    # vi conflicts.rb
    # Add 'Line6' and 'Line7'
    git commit -am "add line6 and line7"
    git push origin master

---

### Example 2/2

Create a merge request on the GitLab web UI. You'll see a conflict warning.

    git checkout conflicts_branch
    git fetch
    git rebase master

    # Fix conflicts by editing the files.

    git add conflicts.rb
    # No need to commit this file

    git rebase --continue

    # Remember that we have rewritten our commit history so we
    # need to force push so that our remote branch is restructured
    git push origin conflicts_branch -f

---

### Notes

308 309 310 311
- When to use `git merge` and when to use `git rebase`
- Rebase when updating your branch with master
- Merge when bringing changes from feature to master
- Reference: <https://www.atlassian.com/git/tutorials/merging-vs-rebasing/>
312 313 314 315 316 317 318 319 320

---

## Revert and Unstage

---

### Unstage

321
To remove files from stage use reset HEAD. Where HEAD is the last commit of the current branch:
322 323 324 325 326 327 328

    git reset HEAD <file>

This will unstage the file but maintain the modifications. To revert the file back to the state it was in before the changes we can use:

    git checkout -- <file>

329
To remove a file from disk and repo use 'git rm' and to rm a dir use the '-r' flag:
330 331 332 333

    git rm '*.txt'
    git rm -r <dirname>

334
If we want to remove a file from the repository but keep it on disk, say we forgot to add it to our .gitignore file then use `--cache`:
335 336 337 338 339 340 341

    git rm <filename> --cache

---

### Undo Commits

342
Undo last commit putting everything back into the staging area:
343 344 345 346 347 348 349 350 351 352 353

    git reset --soft HEAD^

Add files and change message with:

    git commit --amend -m "New Message"

Undo last and remove changes

    git reset --hard HEAD^

354
Same as last one but for two commits back:
355 356 357 358 359 360 361 362 363 364

    git reset --hard HEAD^^

Don't reset after pushing

---

### Reset Workflow

1. Edit file again 'edit_this_file.rb'
365 366 367 368 369 370 371 372 373
1. Check status
1. Add and commit with wrong message
1. Check log
1. Amend commit
1. Check log
1. Soft reset
1. Check log
1. Pull for updates
1. Push changes
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391

----

    # Change file edit_this_file.rb
    git status
    git commit -am "kjkfjkg"
    git log
    git commit --amend -m "New comment added"
    git log
    git reset --soft HEAD^
    git log
    git pull origin master
    git push origin master

---

### Note

392 393 394
git revert vs git reset
Reset removes the commit while revert removes the changes but leaves the commit
Revert is safer considering we can revert a revert
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423

    # Changed file
    git commit -am "bug introduced"
    git revert HEAD
    # New commit created reverting changes
    # Now we want to re apply the reverted commit
    git log # take hash from the revert commit
    git revert <rev commit hash>
    # reverted commit is back (new commit created again)

---

## Questions

---

## Instructor Notes

---

### Version Control
 - Local VCS was used with a filesystem or a simple db.
 - Centralized VCS such as Subversion includes collaboration but
   still is prone to data loss as the main server is the single point of
   failure.
 - Distributed VCS enables the team to have a complete copy of the project
   and work with little dependency to the main server. In case of a main
   server failing the project can be recovered by any of the latest copies
   from the team