-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path1.2-tracking_changes.qmd
307 lines (210 loc) · 8.82 KB
/
1.2-tracking_changes.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
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
---
title: "Intro to `git`"
subtitle: "Block 1.2: Tracking Changes in git"
---
## The 3 Levels of Changes in git
![Changes can be either unstaged, staged or commited.](images/carpentries/git-staging-area.svg)
- When we first make a change it is *unstaged*
- Once we `add` the change to the staging area it is *staged*
- We can then `commit` all *staged* changes
## Tracking Changes: `git add` and `git commit`
![](images/carpentries/git-staging-area.svg)
Files are added to the staging area with <br> `git add <path to file or directory>`
All files in the staging area are commited with <br> `git commit`
## Tracking Changes: `git add`
Files are added to the staging area with <br> `git add <path to file or directory>`.
```bash
# ...we write something into example.txt
git status
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
# example.txt
```
Add `example.txt` to the staging area
```bash
git add example.txt
git status
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
# new file: example.txt
```
## Tracking Changes: `git add`
Changes are added to the staging area with <br> `git add <path to file or directory>`.
- You can use `git add .` to add all unstaged changes to the staging area
- `.` always refers to the current directory
- You can also use `*` to represent any sort of filename
- e.g. add all `.txt` files via `*.txt`
## Tracking Changes: `git commit`
All changes in the staging area are commited with <br> `git commit`
```bash
git status
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
# new file: example.txt
```
Let's commit the new change
```bash
git commit -m "Creating example.txt"
# [main (root-commit) ed61bb5] Adding example.txt
# 1 file changed, 1 insertion(+)
# create mode 100644 example.txt
git status
# On branch main
# nothing to commit, working tree clean
```
## Tracking changes: `git commit`
- The name git "commit", might already suggest it, but once a change is *commited* it becomes significantly harder to remove it.
- The classic way to undo a commited change in git would be to make another commit with the reverse change.
- Modifying a commit is possible, but you should now what you are doing. Typically this is called "changing history" and is (esp. in collaborative settings) frowned upon.
## Stuck in an Editor 🛗
Forgot the `-m "insightful commit message"`?
- Every commit needs a message
- If you don't provide one, `git` will open an editor for you to write the message in
- This editor may also open itself for other `git` commands
- Every line with a `#` at the beginning is a comment and will be ignored by `git` (usually some helpful extra info)
- Save and close the editor for `git` to proceed
## Stuck in an Editor: Breaking Free ⛓️💥
The default editor in git is (usually) `vim`
- `vim` is notoriously hard to get out of
- Press `ESC`, then write `:wq` and hit `Enter`
- You can also change the default editor to e.g. VSCode
- ⚠️ Important: Verify first whether you can start VSCode from the comand line by running the `code` command
- `git config --global core.editor "code"`
- For fun: <https://github.com/hakluke/how-to-exit-vim>
# Questions?
# Demo: <br> Using git via the CLI
## Tracking changes: Practical (1/2) {background-color="black"}
1. Open VS Code in the directory `git-exercise` you created earlier
2. Create a new text file called `hello.txt` with the contents "Hello!"
3. Add the file to the staging area (`add`)
4. Commit the new file (`commit`)
Check what's happening between steps with `git status`.
## Tracking changes: Practical (2/2) {background-color="black"}
Check what's happening between steps with `git status`.
6. Change the text in `hello.txt` to read "Hello there!"
7. Add the file to the staging area (`add`)
8. Commit the new file (`commit`)
## Seeing Changes: `git status` 👀
You can see the high-level changes and what is about to happen with <br>
`git status`
```bash
# ...we change the contents of example.txt
git status
# On branch main
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git restore <file>..." to discard changes in working directory)
# modified: example.txt
# no changes added to commit (use "git add" and/or "git commit -a")
```
## Seing Changes: `git diff` 👁️👄👁️
You can see the actual *unstaged* changes, line by line with <br>
`git diff`
```bash
# ...we changed the contents of example.txt
git diff
# diff --git a/example.txt b/example.txt
# index 10ddd6d..980a0d5 100644
# --- a/example.txt
# +++ b/example.txt
# @@ -1 +1 @@
# -Hello!
# +Hello World!
```
:::{.callout-note}
`git diff` only allows you to see changes to files which have already been commited before i.e. changing the contents of an existing file.
:::
## Un-Tracking changes: `git reset`
- You can reset your staging area with `git reset`, removing all staged files
- To remove specific files from the staging area use `git reset <filename / directory>`
:::{.fragment}
```bash
# We add all changes
git add .
git status
# On branch main
# Changes to be committed:
# (use "git restore --staged <file>..." to unstage)
# modified: example.txt
git reset
# Unstaged changes after reset:
# M example.txt
```
:::
## Un-Doing changes: `git restore`
You can *undo* any changes to files with <br>
`git restore`
```bash
# ...we changed the contents of example.txt
git status
# On branch main
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git restore <file>..." to discard changes in working directory)
# modified: example.txt
# no changes added to commit (use "git add" and/or "git commit -a")
git restore example.txt
git status
# On branch new-branch
# nothing to commit, working tree clean
```
## Un-Doing changes: `git restore`
You can *undo* any changes to files with <br>
`git restore`
- This is one of the super-powers of using git, allowing you to just go ahead and change your files without wasting time having to create backups
## Interim: The anatomy of a git command 🔍️
Git commands, like many other CLI tools follow a certain structure:
```bash
git status
git commit -m "Adding example.txt"
git config --global user.name "Your Name"
```
:::{.fragment}
With `-h` you can get help on any git command 🚨
```bash
git status -h
git commit -h
```
:::
# Questions? {background-opacity="0.4" background-image="images/backgrounds/questions.gif" background-position="top"}
I'm aware this was a lot to take in.
<!-- Src: https://giphy.com/gifs/dogs-puppy-confused-xT0xeuOy2Fcl9vDGiA -->
## Practical: git Command Library (1){background-color="black"}
1. Create a new file `library.txt` in your `git-exercise` directory
2. Run `git init -h` and copy the output into your `library.txt`
3. Explore the repo status using `git status` and `git diff`
4. Commit the changes (with a good commit message)
5. Delete the `library.txt` and restore it
## Let's get Graphical: Tracking Changes in SourceTree
![The labels in Sourcetree mirror the names within the git CLI](images/screenshots/sourcetree-test.png)
- Demo!
## Practical: git Command Library (2){background-color="black"}
1. Run `git <command> -h` and copy the output into your `library.txt`
2. Explore the repo status *using the GUI*
3. Commit the changes (with a good commit message)
4. Repeat steps 2. - 4. for **another command** we learned so far, using a separate commit
5. Delete the `library.txt` and restore it
## Demo: Tracking Changes in VSCode
![Since VSCode is primarily a text editor, git functionality is hidden in the sidebar](images/screenshots/vscode-git.png)
- Demo!
## Practical: git Command Library (3){background-color="black"}
1. Run `git <command> -h` and copy the output into your `library.txt`
2. Explore the repo status *using the VSCode*
3. Commit the changes (with a good commit message)
4. Repeat steps 2. - 4. for **another command** we learned so far, using a separate commit
5. Delete the `library.txt` and restore it
## Note: Git and Code
- You have noticed that we're not using `git` with source code here
- Mainly for teaching purposes
- Not everyone is comfortable coding in the same programming language
- However, also good to be aware that `git` can handle more than just code! 💪
## 🎉 Bonus Practical: Binary Files {background-color="black"}
1. Create a Word document called `library.docx` and copy over the contents from `library.txt`
2. Run `git stash -h` and copy the output into both `library.docx` and `library.txt`
3. Use `git diff` to compare how the same change is recognized by git in the two documents
## *End of Block* 🎉 {background-color="black" background-opacity="0.4" background-image="images/backgrounds/dolomites.jpg"}
:::{.r-fit-text}
Any Questions?
:::
[[🏡 Back to Overview]](./index.html)
[[⏩️ Next Block]](./2.1-gitignore.html)