LESSON 1: Fundamental Operations with Git#
Lecture notes on the fundamental operation with Git. A hands-on lesson for introducing version control, tracking changes, and the tracking history. These notes contains the following pointers for the instructor:
Numbers between
[]
are indicative of how much time should be spend in each topic or exercise to keep in track with the lesson schedule.The text in Instructor note contain explanations or reminders for the instructor. For example:
Instructor’s Note
Ensure that all users have git and bash available at the start of the course.
Presentation
This contains general information about the lesson and illustrations for supporing the explanations of some of the concepts, and
Episode 1: Git repositories for version control#
[ca 50 min total]
1.1.0 Welcome slides#
[10 min]
1.1.1 Introduction to Git#
[10 min]
Create a directory for the course
cd ~/Desktop/
ls
mkdir -p 2410-gitcodev/git
ls
ls 2410-gitcodev/
cd 2410-gitcodev/git/
ls
pwd
Check which shell is available
echo $SHELL
echo
Check installed version of Git
git
git --version
git version
1.1.2 Git Command Syntax and Getting Help#
[10 min]
git help
git help help
git config
git config --list
git config --global core.editor nano
git config --global core.autocrlf input # false for Win
git config --list
git help config
git help glossary
git help -g
1.1.3. Creating an Empty Reposiory#
[10 min]
pwd
ls
echo 'first line'
echo 'first line' >lines.txt
ls
cat lines.txt
echo 'second line' >>lines.txt
cat lines.txt
git status
git init
ls
ls -a
ls -aF
ls -aF .git
ls
1.1.4. Q&A#
[10 min]
Short Break#
[10 min]
Episode 2: Tracking Changes in Working Documents#
[ca 75 min instruction + 20 min break]
1.2.1 Tracking Changes with the Index#
[10 min]
git status
git add lines.txt
git status
ls -a .git
git diff lines.txt
echo 'third line' >>lines.txt
cat lines.txt
git diff lines.txt
git status
git add lines.txt
git status
git add lines.txt
git status
git diff lines.txt
Exercise 1 — Tracking changes with the Index [5 min]
Lesson 1 Episode 2 — Tracking changes with the index
Please perform the following tasks alone
The current file lines.txt
contains three lines
Revise in your terminal the commands we have used so far
Append to the file a fourth line (in the same style as the previous lines)
Check the new content of the file using…
Check whether Git realises that a change has occurred using…
Check which difference Git finds in the file using… Take note of the output of the Git commands
Follow the action that Git suggests next by using…
Same as 1.2
Same as 1.3
Same as 1.4
Take note of how the outputs of Git commands have changed
Formulate your own explanation of what you have observed The following questions are two sides of the same coin:
What did you ask Git to do?
What did Git do for you?
Answers
[No answers yet]
Tracking Directories#
[10 min]
mkdir directory
ls
ls -F
git status
touch directory/emptyfile.txt
ls -R
ls -RF
git status
git status -u
git help status
git add directory
git status
1.2.2 Not Tracking and Stop Tracking#
[10 min]
history
history 20
history 20 >history.log
cat history.log
git status
echo '*.log'
echo '*.log' >.gitignore
git status
ls -a
ls -aF
git add .gitignore
git status
echo 'lines.txt' >>.gitignore
cat .gitignore
git status
git add .gitignore
git status
Ignore Untracked Directories#
[10 min]
touch directory/trackme.txt
touch directory/donttrackme.txt
ls directory/
git status
echo 'directory' >> .gitignore
cat .gitignore
git status
echo '!directory' >> .gitignore
cat .gitignore
git status
cat .gitignore
echo 'directory' >>.gitignore
cat .gitignore
git status
git help gitignore
git status
git add .gitignore
git status
git rm --staged directory/emptyfile.txt
git rm --cached directory/emptyfile.txt
git status
ls -FR
Exercise 2 — Stop tracking Changes in a File [5 min]
Lesson 1 Episode 2 — Stop tracking changes in a file
Please perform the following tasks individually
Revise the commands we have used since the last exercise. Could you use a shell command for this?
Ask Git for the current state of the repository using…
Stop tracking the file
directory/emptyfile.txt
Caution
Be mindful of not deleting the file!
Verify the result of your action using…
Formulate your own explanation of what you have observed The following questions are two sides of the same coin:
What did you ask Git to do?
What did Git do for you?
Answers
[No answers yet]
Longer Break#
[20 min]
1.2.3 Undoing Changes with the Index#
[10 min]
cat lines.txt
echo 'fifth line' >>lines.txt
cat lines.txt
git status
git diff lines.txt
git restore lines.txt
cat lines.txt
git status
git diff lines.txt
git restore lines.txt
cat lines.txt
git status
echo '!directory' >>.gitignore
ls
git status
git status -u
git add directory/trackme.txt
git status -u
git add directory/donttrackme.txt
git add directory/emptyfile.txt
git status
git add .gitignore
git status
1.2.4 Deleting and renaming tracked files and directories#
[10 min]
git rm --cached directory/donttrackme.txt
git status
rm directory/
rm -r directory/
ls -FR
git status
git restore directory
git status
ls -FR directory/
touch directory/donttrackme.txt
git status
git rm directory
git rm -r directory
git status
git rm -rf directory
git status
ls -FR directory/
git restore directory
git status
git status -u
mv directory/donttrackme.txt directory/trackne.txt
git status -u
# use the command `git mv <oldname> <newname>`
# lines.txt Lines.txt
git status -u
git mv lines.txt Lines.txt
git status -u
Exercise 3 — Renaming Tracked Files [5 min]
Lesson 1 Episode 2 — Renaming tracked files
Please perform the following tasks individually
Revise the commands we have used since the last exercise. Could you use a shell command for this?
Ask Git for the current state of the repository using…
Rename the file
lines.txt
asLines.txt
using…Verify that the working tree contains
Lines.txt
using…Verify that Git tracks
Lines.txt
using…
Answers
[No answers yet]
Episode 3: Organizing Tracked Changes in a History#
[ca 75 min + 10 min break]
1.3.1 Commiting Changes with a Configured Identify and a Message#
[10 min]
cat Lines.txt
git diff
git status
git commit -m 'Add first four lines' Lines.txt
git status
git log
Exercise 4 — Commit Changes in a Tracked File [5 min]
Lesson 1 Episode 3 — Commit changes in a tracked file
Please perform the following tasks individually
Check if .gitignore
is ready to be committed using…
Commit .gitignore
using…
Attention
DO NOT forget the commit message describing your action
Answers
git status
git commit -m 'Add .gitignore' .gitignore
git status
rm -r directory # to keep things clean
git status
ls
git log
Exercise 5 — Follow the state of the repository in the commit routine [5 min]
Lesson 1 Episode 3 — Follow the state of the repository in the commit routine
Please perform the following tasks individually.
The baseline
Revise the commands launched since the last exercise using…
Check the output of
git status
The working tree
Append the fifth line to
Lines.txt
using…Repeat 1.2 and compare its output with the previous
The index
Stage the changes of
Lines.txt
in the index using…Repeat 1.2 and compare its output with the previous ones
The history
Store the changes of
Lines.txt
in the history using…Repeat 1.2 and compare its output with the previous ones
Look up and parse the commit history using…
Formulate your own explanation of what git status showed you The following questions are two sides of the same coin:
What did you ask Git to do?
What did Git do for you?
Answers
# Procedure for exercise 5
echo 'fifth line' >>Lines.txt
cat Lines.txt
git status
git add Lines.txt
git status
git commit -m 'Add fifth lines' Lines.txt
git status
history
git log
1.3.2 Inspecting Changes Using the History#
Exercise 6 — Follow the state of the index in the commit routines [5 min]
Lesson 1 Episode 3 — Follow the state of the index in the commit routine
Please perform the following tasks individually
The baseline
Revise the commands launched since the last exercise using…
Check the output of
git diff
The working tree
Append the sixth line to
Lines.txt
using…Repeat 1.2 and compare its output with the previous
The index
Stage the changes of
Lines.txt
in the index using…Repeat 1.2 and compare its output with the previous ones
The history
Store the changes of
Lines.txt
in the history using…Repeat 1.2 and compare its output with the previous ones
Look up and parse the commit history using…
Formulate your own explanation of what git diff
showed you
The following questions are two sides of the same coin:
What did you ask Git to do?
What did Git do for you?
Answers
# procedure for exercise 6
git status
git diff
echo 'sixth line' >>Lines.txt
git diff
git add Lines.txt
git diff
git diff Lines.txt
git commit -m 'Add sixth line' Lines.txt
git diff
git log
git log --oneline
git status
echo 'seventh line' >>Lines.txt
git diff Lines.txt
git add Lines.txt
git diff Lines.txt
git status
git log --oneline
git diff HEAD Lines.txt
git diff e278702 Lines.txt
git diff HEAD~1 Lines.txt
git log --oneline
git diff 7f2ca Lines.txt
git diff HEAD~2 Lines.txt
git diff HEAD~3 Lines.txt
git diff HEAD~7 Lines.txt
git diff Lines.txt
git diff HEAD~3 Lines.txt
git diff HEAD HEAD~1 Lines.txt
git diff HEAD~1 HEAD Lines.txt
git diff HEAD~4 HEAD~2 Lines.txt
git diff HEAD~3 HEAD~2 Lines.txt
git log
#
pwd
git status
git log --oneline
git diff Lines.txt
git diff HEAD Lines.txt
git diff HEAD~1 Lines.txt
git diff HEAD HEAD~1 Lines.txt
git diff HEAD~1 HEAD Lines.txt
diff HEAD HEAD Lines.txt
git diff HEAD HEAD Lines.txt
git diff HEAD~2 HEAD~2 Lines.txt
###Short break [10 min]
Exercise 7 — Explore the changes recorded in the history [5 min]
Lesson 1 Episode 3 — Explore the changes recorded in the history
Please perform the following tasks individually
The baseline
Revise the commands launched since the last exercise using…
Perform the commit routine
Append the seventh line to
Lines.txt
using…Stage the changes of
Lines.txt
in the index using…Store the changes of
Lines.txt
in the history using…
As many times as you like
Check the history using…
Compare different versions of
Lines.txt
using…
Formulate your own explanation of what you have observed The following questions are two sides of the same coin:
What did you ask Git to do?
What did Git do for you?
Attention
Are there shortcuts to do the same with less typing?
Answers
# No answers yet
Exercise 8 is an optional exercise about comparing differences in the history.
1.3.3 Undoing Changes with the History#
[10 min]
Note
This topic involves using git restore
. Actual commands are missing.
Wrap Up#
[10 min]
Note
Give a short wrap up about what has been learned.