user_training.md 7.4 KB
Newer Older
1 2 3 4
---
comments: false
---

Sean Packham's avatar
Sean Packham committed
5 6 7 8
# GitLab Git Workshop

---

9
## Agenda
Sean Packham's avatar
Sean Packham committed
10

11 12 13 14
1. Brief history of Git.
1. GitLab walkthrough.
1. Configure your environment.
1. Workshop.
Sean Packham's avatar
Sean Packham committed
15 16 17

---

18
## Git introduction
Sean Packham's avatar
Sean Packham committed
19

20
<https://git-scm.com/about>
Sean Packham's avatar
Sean Packham committed
21

22 23 24 25 26 27
- Distributed version control.
  - Does not rely on connection to a central server.
  - Many copies of the complete history.
- Powerful branching and merging.
- Adapts to nearly any workflow.
- Fast, reliable and stable file format.
Sean Packham's avatar
Sean Packham committed
28 29 30

---

31
## Help!
Sean Packham's avatar
Sean Packham committed
32 33 34

Use the tools at your disposal when you get stuck.

35 36 37
- Use '`git help <command>`' command.
- Use Google.
- Read documentation at <https://git-scm.com>.
Sean Packham's avatar
Sean Packham committed
38 39 40

---

41
## GitLab Walkthrough
Sean Packham's avatar
Sean Packham committed
42 43 44 45 46

![fit](logo.png)

---

47
## Configure your environment
Sean Packham's avatar
Sean Packham committed
48 49 50

- Windows: Install 'Git for Windows'

51
> <https://git-for-windows.github.io>
Sean Packham's avatar
Sean Packham committed
52 53 54 55 56

- Mac: Type '`git`' in the Terminal application.

> If it's not installed, it will prompt you to install it.

57
- Debian: '`sudo apt-get install git-all`' or Red Hat '`sudo yum install git-all`'
Sean Packham's avatar
Sean Packham committed
58 59 60

---

61
## Git Workshop
Sean Packham's avatar
Sean Packham committed
62

63
### Overview
Sean Packham's avatar
Sean Packham committed
64

65 66 67 68 69 70 71
1. Configure Git.
1. Configure SSH Key.
1. Create a project.
1. Committing.
1. Feature branching.
1. Merge requests.
1. Feedback and Collaboration.
Sean Packham's avatar
Sean Packham committed
72 73 74

---

75
## Configure Git
Sean Packham's avatar
Sean Packham committed
76

77
One-time configuration of the Git client:
Sean Packham's avatar
Sean Packham committed
78

79
```sh
Sean Packham's avatar
Sean Packham committed
80 81 82 83 84 85
git config --global user.name "Your Name"
git config --global user.email you@example.com
```

---

86
## Configure SSH Key
Sean Packham's avatar
Sean Packham committed
87

88
```sh
Sean Packham's avatar
Sean Packham committed
89 90 91
ssh-keygen -t rsa -b 4096 -C "you@computer-name"
```

92
```sh
Sean Packham's avatar
Sean Packham committed
93 94 95 96 97 98 99 100 101 102 103
# You will be prompted for the following information. Press enter to accept the defaults. Defaults appear in parentheses.
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/you/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/you/.ssh/id_rsa.
Your public key has been saved in /Users/you/.ssh/id_rsa.pub.
The key fingerprint is:
39:fc:ce:94:f4:09:13:95:64:9a:65:c1:de:05:4d:01 you@computer-name
```

104
Copy your public key and add it to your GitLab profile:
Sean Packham's avatar
Sean Packham committed
105

106
```sh
Sean Packham's avatar
Sean Packham committed
107 108 109
cat ~/.ssh/id_rsa.pub
```

110
```sh
Sean Packham's avatar
Sean Packham committed
111 112 113 114 115
ssh-rsa AAAAB3NzaC1yc2EAAAADAQEL17Ufacg8cDhlQMS5NhV8z3GHZdhCrZbl4gz you@example.com
```

---

116
## Create a project
Sean Packham's avatar
Sean Packham committed
117

118 119
- Create a project in your user namespace.
  - Choose to import from 'Any Repo by URL' and use <https://gitlab.com/gitlab-org/training-examples.git>.
Sean Packham's avatar
Sean Packham committed
120
- Create a '`development`' or '`workspace`' directory in your home directory.
121
- Clone the '`training-examples`' project.
Sean Packham's avatar
Sean Packham committed
122 123 124

---

125
## Commands (project)
Sean Packham's avatar
Sean Packham committed
126

127
```sh
Sean Packham's avatar
Sean Packham committed
128 129 130 131 132 133 134 135 136 137 138 139 140 141
mkdir ~/development
cd ~/development

-or-

mkdir ~/workspace
cd ~/workspace

git clone git@gitlab.example.com:<username>/training-examples.git
cd training-examples
```

---

142
## Git concepts
Sean Packham's avatar
Sean Packham committed
143

144
### Untracked files
Sean Packham's avatar
Sean Packham committed
145 146 147

New files that Git has not been told to track previously.

148
### Working area
Sean Packham's avatar
Sean Packham committed
149 150 151

Files that have been modified but are not committed.

152
### Staging area
Sean Packham's avatar
Sean Packham committed
153 154 155 156 157

Modified files that have been marked to go in the next commit.

---

158
## Committing
Sean Packham's avatar
Sean Packham committed
159

160 161 162 163 164 165 166
1. Edit '`edit_this_file.rb`' in '`training-examples`'.
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.
Sean Packham's avatar
Sean Packham committed
167 168 169

---

170
## Commands (committing)
Sean Packham's avatar
Sean Packham committed
171

172
```sh
Sean Packham's avatar
Sean Packham committed
173 174 175 176 177 178 179 180 181 182 183
# Edit `edit_this_file.rb`
git status
git diff
git add <file>
git commit -m 'My change'
git push origin master
git log
```

---

184
## Feature branching
Sean Packham's avatar
Sean Packham committed
185

186 187 188 189 190 191
- Efficient parallel workflow for teams.
- Develop each feature in a branch.
- Keeps changes isolated.
- Consider a 1-to-1 link to issues.
- Push branches to the server frequently.
  - Hint: This is a cheap backup for your work-in-progress code.
Sean Packham's avatar
Sean Packham committed
192 193 194

---

195
## Feature branching steps
Sean Packham's avatar
Sean Packham committed
196

197
1. Create a new feature branch called 'squash_some_bugs'.
Sean Packham's avatar
Sean Packham committed
198
1. Edit '`bugs.rb`' and remove all the bugs.
199 200
1. Commit.
1. Push.
Sean Packham's avatar
Sean Packham committed
201 202 203

---

204
## Commands (feature branching)
Sean Packham's avatar
Sean Packham committed
205

206
```sh
Sean Packham's avatar
Sean Packham committed
207 208 209 210 211 212 213 214 215 216
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
```

---

217
## Merge requests
Sean Packham's avatar
Sean Packham committed
218

219 220 221 222 223 224 225
- 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.
- Add 'WIP' to the title if it's a work in progress.
- When accepting, always delete the branch.
- Anyone can comment, not just the assignee.
- Push corrections to the same branch.
Sean Packham's avatar
Sean Packham committed
226 227 228

---

229
## Merge requests steps
Sean Packham's avatar
Sean Packham committed
230

231
Create your first merge request:
Sean Packham's avatar
Sean Packham committed
232

233 234 235 236
1. Use the blue button in the activity feed.
1. View the diff (changes) and leave a comment.
1. Push a new commit to the same branch.
1. Review the changes again and notice the update.
Sean Packham's avatar
Sean Packham committed
237 238 239

---

240
## Feedback and Collaboration
Sean Packham's avatar
Sean Packham committed
241

242 243 244 245 246 247
- 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.
Sean Packham's avatar
Sean Packham committed
248 249 250

---

251
## Feedback and Collaboration resources
Sean Packham's avatar
Sean Packham committed
252 253

Review the Thoughtbot code-review guide for suggestions to follow when reviewing merge requests:
254
<https://github.com/thoughtbot/guides/tree/master/code-review>.
Sean Packham's avatar
Sean Packham committed
255

256
See GitLab merge requests for examples: <https://gitlab.com/gitlab-org/gitlab-ce/merge_requests>.
Sean Packham's avatar
Sean Packham committed
257 258 259

---

260
## Explore GitLab projects
Sean Packham's avatar
Sean Packham committed
261 262 263 264 265 266 267 268 269 270 271 272 273

![fit](logo.png)

- Dashboard
- User Preferences
- ReadMe, Changelog, License shortcuts
- Issues
- Milestones and Labels
- Manage project members
- Project settings

---

274
## Tags
Sean Packham's avatar
Sean Packham committed
275

276 277 278 279 280
- Useful for marking deployments and releases.
- Annotated tags are an unchangeable part of Git history.
- Soft/lightweight tags can be set and removed at will.
- Many projects combine an annotated release tag with a stable branch.
- Consider setting deployment/release tags automatically.
Sean Packham's avatar
Sean Packham committed
281 282 283

---

284
## Tags steps
Sean Packham's avatar
Sean Packham committed
285

286 287 288
1. Create a lightweight tag.
1. Create an annotated tag.
1. Push the tags to the remote repository.
Sean Packham's avatar
Sean Packham committed
289

290
Additional resources: <http://git-scm.com/book/en/Git-Basics-Tagging>.
Sean Packham's avatar
Sean Packham committed
291 292 293

---

294
## Commands (tags)
Sean Packham's avatar
Sean Packham committed
295

296
```sh
Sean Packham's avatar
Sean Packham committed
297 298 299 300 301 302 303 304 305 306 307 308 309 310
git checkout master

# Lightweight tag
git tag my_lightweight_tag

# Annotated tag
git tag -a v1.0 -m ‘Version 1.0’
git tag

git push origin --tags
```

---

311
## Merge conflicts
Sean Packham's avatar
Sean Packham committed
312

313 314 315
- Happen often.
- Learning to fix conflicts is hard.
- Practice makes perfect.
Sean Packham's avatar
Sean Packham committed
316 317 318 319
- Force push after fixing conflicts. Be careful!

---

320
## Merge conflicts steps
Sean Packham's avatar
Sean Packham committed
321 322

1. Checkout a new branch and edit `conflicts.rb`. Add 'Line4' and 'Line5'.
323
1. Commit and push.
Sean Packham's avatar
Sean Packham committed
324
1. Checkout master and edit `conflicts.rb`. Add 'Line6' and 'Line7' below 'Line3'.
325 326
1. Commit and push to master.
1. Create a merge request.
Sean Packham's avatar
Sean Packham committed
327 328 329

---

330
## Merge conflicts commands
Sean Packham's avatar
Sean Packham committed
331 332 333 334

After creating a merge request you should notice that conflicts exist. Resolve
the conflicts locally by rebasing.

335
```sh
Sean Packham's avatar
Sean Packham committed
336 337 338 339 340 341 342 343 344 345 346 347
git rebase master

# Fix conflicts by editing the files.

git add conflicts.rb
git commit -m 'Fix conflicts'
git rebase --continue
git push origin <branch> -f
```

---

348
## Rebase with squash
Sean Packham's avatar
Sean Packham committed
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365

You may end up with a commit log that looks like this:

```
Fix issue #13
Test
Fix
Fix again
Test
Test again
Does this work?
```

Squash these in to meaningful commits using an interactive rebase.

---

366
## Rebase with squash commands
Sean Packham's avatar
Sean Packham committed
367 368 369

Squash the commits on the same branch we used for the merge conflicts step.

370
```sh
Sean Packham's avatar
Sean Packham committed
371 372 373 374 375 376 377
git rebase -i master
```

In the editor, leave the first commit as 'pick' and set others to 'fixup'.

---

378
## Questions?
Sean Packham's avatar
Sean Packham committed
379 380 381 382 383

![fit](logo.png)

Thank you for your hard work!

384
## Additional Resources
Sean Packham's avatar
Sean Packham committed
385

386 387
- GitLab Documentation: <http://docs.gitlab.com/>.
- GUI Clients: <http://git-scm.com/downloads/guis>.
388
- Pro Git book: <http://git-scm.com/book>.
389 390 391
- Platzi Course: <https://courses.platzi.com/courses/git-gitlab/>.
- Code School tutorial: <http://try.github.io/>.
- Contact us at `subscribers@gitlab.com`.