You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Track changes in files using `git add`, `git status` and `git commit`
6
-
* Know state of Git repo using `git diff` and `git log`
3
+
## Objective
4
+
* Create a Git repository from scratch
5
+
* Track changes to files using `git add`, `git status`, and `git commit`
6
+
* Know the state of the Git repository using `git diff` and `git log`
7
7
8
8
## Structure
9
-
This exercise consists of short descriptions about a specific git command, followed by a practical part where you can execute appropriate Git commands.
10
-
In order to allow a smooth exercise, there are some functions written by C2SM in the file *helpers.sh* that are **NOT** part of Git. For this exercise we use the following functions from that file:
11
-
***init_exercise:** It will create the *work* directory and navigate into it
12
-
***reset:** It will delete the *work* folder and enable you a clean restart of the exercise in case you completely mess it up.
13
-
14
-
In the following (and all further exercises), all text enclosed with `<>` denotes a placeholder to be replaced by a specific string appropriate in **your** context.
9
+
This exercise consists of short descriptions of specific Git commands, followed by a hands-on part where you will be able to execute the corresponding Git commands.
15
10
16
-
### Start exercises in correct folder
17
-
This exercise (and all further exercises) assume that the shell is already located in the folder of the exercise notebooks. It could happen, for some reason, that the notebook does not by default switch to the folder of the notebook. In that case you need to manually change the directory first in order to do the exercises.
11
+
##Helper Functions
12
+
The following helper functions in the file *helpers.sh* are written by C2SM and are **NOT****part of Git**. They will set up simple repositories for you that have a short Git history, so that you have something to work with.
18
13
19
-
If the command
14
+
For this exercise, we will use the following functions from this file:
15
+
***init_exercise:** This will create the *beginners_git* directory in the parent directory of the *git-course* directory. It will also delete any old version of the *beginners_git* directory, so don't use the *beginners_git* directory to save any work.
16
+
***reset:** This will delete the *beginners_git* directory and allows you a clean restart of the exercise in case you messed it up completely.
20
17
21
-
`pwd`
22
-
returns something similar to
23
-
`/home/juckerj/git-course/Exercise_1`
24
-
everything is fine.
25
18
26
-
In case it returns only
27
-
`/home/juckerj`
28
-
please go the right directory.
19
+
## Remarks
20
+
_**Note:** Any text enclosed in `<>` denotes a placeholder to be replaced with a specific string appropriate to your context, i.e. delete `<>` and replace it with the appropriate word._
21
+
22
+
_**Note:** The exercises require you to use basic Unix commands. If you are not familiar with Unix systems, we have listed all the necessary commands in the file [Basic Unix Commands](../Unix_Commands.ipynb)._
23
+
29
24
30
25
### Initialization
31
26
27
+
**Start exercises in correct folder:**
28
+
This exercise (and all the exercises that follow) assume that the shell is already in the folder where the exercise notebooks are located. For some reason, the notebook may not switch to the notebook folder by default. In this case, you will need to manually change the directory in order to complete the exercises.
29
+
30
+
If the `pwd` command returns something like `/home/juckerj/git-course/beginner/Exercise_1`, everything is fine.
31
+
32
+
If it returns something like `/home/juckerj`, change to the correct directory.
33
+
34
+
32
35
33
36
```bash
34
37
# check current directory with "pwd"
35
-
pwd
38
+
36
39
# go to folder of this exercise using "cd"
37
40
38
41
```
39
42
43
+
**To initialize the exercise properly, run this code at the very beginning. Check the Helper Functions section above for more explanation.**
44
+
40
45
41
46
```bash
42
-
#execute this code at the very beginning to initialize the exercise properly
47
+
#source the helpers.sh file to be able to use its functions
43
48
source ../helpers.sh
49
+
# init exercise
44
50
init_exercise
45
51
```
46
52
47
53
***
48
54
### Optional: clear notebook and restart
49
-
**In case you mess up your notebook completely,
50
-
execute*****reset*****in the following cell. This will restore a clean environment!**
55
+
**In case you messed up your notebook completely, execute*****reset*****in the following cell. Check the Helper Functions section above for more explanation.**
51
56
52
57
53
58
```bash
54
59
## only execute in case of (serious) trouble ##
55
-
## it will delete your entire work directory ##
60
+
## it will delete your entire beginners_git directory ##
56
61
reset
57
62
```
58
63
59
64
***
60
65
## Exercise
61
66
62
67
### Global Git configuration settings
63
-
Before we start to use Git, we are specifying a few global configuration settings. This has to be done only once and will be saved for all your future sessions.
68
+
Before we start using Git, we should set some global configurations. This only needs to be done once, and will be saved for all your future sessions.
64
69
65
70
First of all, we need to tell Git who we are.
66
-
To do so, execute the lines below with your credentials:
71
+
To do this, run the following lines with your credentials:
**Note:** The email must be identical to the one that is used for your Github account.
76
+
**Note:** The email must be identical to the one that is used for your GitHub account.
72
77
73
78
74
79
```bash
75
80
# tell Git who you are
76
81
82
+
77
83
```
78
84
79
-
At the end of this course, you will learn something about repository managers like Github. Recently, they changed their naming policy for the initial branch from 'master' to 'main'. Therefore, we want to tell Git to also set our default branch name to 'main'.
85
+
At the end of this course, you will learn something about repository managers like GitHub. Recently, they changed their naming policy for the initial branch from *master* to *main*. So we want to tell Git to set our default branch name to *main* as well.
80
86
81
-
**Note:**Check out the official Git documentation (https://git-scm.com/docs/git-init#Documentation/git-init.txt--bltbranch-namegt).
87
+
**Note:**See the official Git documentation (https://git-scm.com/docs/git-init#Documentation/git-init.txt--bltbranch-namegt).
82
88
83
89
84
90
```bash
85
-
#Set main as the default branch name
91
+
#set 'main' as the default branch name
86
92
git config --global init.defaultBranch main
87
93
```
88
94
89
95
### Create Git repository from scratch
96
+
> Hint: check the [Basic Unix Commands](../Unix_Commands.ipynb) if you don't know how to do the following.
90
97
91
98
92
99
```bash
93
-
# create new folder (e.g. <git_repo>) and enter it
100
+
# create a new folder (e.g. <git_repo>) and navigate to it
94
101
95
102
```
96
103
97
104
98
105
```bash
99
-
# use the command "git init" to initiate your first Git-repository
100
-
git init
106
+
# use the command "git init" to initiate your first Gitrepository
107
+
101
108
```
102
109
103
-
You should now get an output similar to that:
110
+
You should now get an output similar to:
104
111
```
105
-
Initialized empty Git repository in /Users/juckerj/Desktop/git_course/Exercise_1/work/git_repo/.git/
112
+
Initialized empty Git repository in <parent-dir-of-git-course>/beginners_git/git_repo/.git/
106
113
```
107
-
### Track changes in files using git addand git commit
114
+
### Make and track changes in files using `git add`, `git commit`and `git status`
108
115
109
-
In a next step you will add some files to you repository.
110
-
To do so, we make use of the *echo* command in combination with the ">" operator to direct its
116
+
In a next step you will add some files to your repository.
117
+
To do this, we will use the *echo* command in combination with the ">" operator to direct its
111
118
output to a file.
112
119
113
120
114
121
```bash
115
-
#to create a new file use the echo-command
116
-
# echo "<my text for file>" > first_file
122
+
# create a text file using the echo-command
123
+
# echo "<my text for file>" > first_file.txt
117
124
118
125
```
119
126
@@ -125,8 +132,7 @@ output to a file.
125
132
126
133
127
134
```bash
128
-
# check the status of your Git-repository with "git status"
129
-
135
+
# check the status of your Git repository with "git status"
130
136
131
137
```
132
138
@@ -140,15 +146,14 @@ No commits yet
140
146
141
147
Untracked files:
142
148
(use "git add <file>..." to include in what will be committed)
143
-
first_file
144
-
second_file
149
+
first_file.txt
150
+
second_file.txt
145
151
146
152
nothing added to commit but untracked files present (use "git add" to track)
147
153
```
148
154
149
155
150
-
Git recognized these two new files, but the files are not yet included in the repository.
151
-
156
+
Git has detected the two new files, but the files are not yet included in the Git repository.
152
157
153
158
154
159
```bash
@@ -158,7 +163,7 @@ Git recognized these two new files, but the files are not yet included in the re
158
163
159
164
```
160
165
161
-
Your output should look as follows:
166
+
Your output should look like this:
162
167
163
168
```
164
169
On branch main
@@ -167,15 +172,15 @@ No commits yet
167
172
168
173
Changes to be committed:
169
174
(use "git rm --cached <file>..." to unstage)
170
-
new file: first_file
171
-
new file: second_file
175
+
new file: first_file.txt
176
+
new file: second_file.txt
172
177
```
173
178
174
179
The last thing to do is to commit these files.
175
180
176
181
177
182
```bash
178
-
# use 'git commit -m "<meaningful message>"'
183
+
# use "git commit -m "<meaningful message>""
179
184
180
185
```
181
186
@@ -184,21 +189,19 @@ Your files are included in the Git repository.
184
189
185
190
186
191
187
-
### Know state of Git repository using git diff and git log
188
-
For now, we have two files in our Git repository.
189
-
Let's see what happens when we modify them. We therefore use the ">>" operator to append a new line of text to our files.
192
+
### Know state of Git repository using `git diff` and `git log`
193
+
Right now we have two files in our Git repository.
194
+
Let's see what happens when we modify them. We will use the ">>" operator to append a new line of text to our files.
190
195
191
196
192
197
```bash
193
-
# append a new line of text with "echo" and ">>"
194
-
198
+
# append a new line of text with "echo" and ">>" to one of the files
195
199
196
200
```
197
201
198
202
199
203
```bash
200
-
# check state of you repository with "git status"
201
-
204
+
# check state of your repository with "git status"
202
205
203
206
```
204
207
@@ -209,20 +212,18 @@ On branch main
209
212
Changes not staged for commit:
210
213
(use "git add <file>..." to update what will be committed)
211
214
(use "git restore <file>..." to discard changes in working directory)
212
-
modified: first_file
215
+
modified: first_file.txt
213
216
214
217
no changes added to commit (use "git add" and/or "git commit -a")
215
218
```
216
219
217
220
218
-
At some point you forget about the additional line we just added.
219
-
Git provides a command to check, what new changes are contained in a file:
220
-
**git diff**
221
+
When working in a repository, it easily happens that you forget about changes you have made, such as the extra lines you just added.
222
+
Git provides the `git diff` command to check, what new changes are contained in a file
221
223
222
224
223
225
```bash
224
-
# see local changes of modified file with "git diff your_filename"
225
-
226
+
# see local changes of a modified file with "git diff <your_filename>"
226
227
227
228
```
228
229
@@ -231,57 +232,50 @@ In the output
231
232
```
232
233
diff --git a/first_file b/first_file
233
234
index 3829ab8..a32d2f3 100644
234
-
--- a/first_file
235
-
+++ b/first_file
235
+
--- a/first_file.txt
236
+
+++ b/first_file.txt
236
237
@@ -1 +1,2 @@
237
238
myfirstline
238
239
+mysecondline
239
240
```
240
241
241
-
we see a lot of information. But all we care at the moment is the last line:
242
-
The + indicates that we have a new line in our file.
242
+
We see a lot of information, but all that we care about is the last line:
243
+
The "+" indicates that we have a new line in our file.
243
244
244
245
245
-
Because Git is so easy, we modify the second file as well.
246
+
Let's modify the second file as well.
246
247
247
248
248
249
```bash
249
250
# add a new line in the second file as well
250
251
251
-
252
252
```
253
253
254
-
The next lecture is starting soon, so let's add and commit our changes for safety reasons in Git.
254
+
The next lecture is starting soon, so let's add and commit our changes for safety reasons.
255
255
256
256
257
257
```bash
258
-
# add you two modified files with "git add"
259
-
260
-
261
-
# git status to check if your action was successful
258
+
# add the two modified files with "git add"
262
259
260
+
# use "git status" to check if your action was successful
263
261
264
262
```
265
263
266
264
267
265
```bash
268
-
# use 'git commit -m "<meaningful message>"' to commit your files
269
-
266
+
# use "git commit -m "<meaningful message>"" to commit your files
270
267
271
268
```
272
269
273
270
**Congrats!**
274
-
But how many commits do I already have in this repository?
271
+
But how many commits do you already have in this repository?
275
272
Git does all this tracking for us!
276
273
277
-
`git log` allows us to look back in time and explore what commits are contained in our repository.
278
-
274
+
The command `git log` allows us to look back in time and explore what commits are contained in our repository.
279
275
280
276
281
277
```bash
282
-
# type git log to get an overview of the (very short)
283
-
# life of your repository
284
-
278
+
# type "git log" to get an overview of the (very short) life of your repository
0 commit comments