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

Collaborative software development

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

  1. Revise in your terminal the commands we have used so far

    1. Append to the file a fourth line (in the same style as the previous lines)

    2. Check the new content of the file using…

    3. Check whether Git realises that a change has occurred using…

    4. Check which difference Git finds in the file using… Take note of the output of the Git commands

  2. Follow the action that Git suggests next by using…

    1. Same as 1.2

    2. Same as 1.3

    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

  1. Revise the commands we have used since the last exercise. Could you use a shell command for this?

  2. Ask Git for the current state of the repository using…

  3. Stop tracking the file directory/emptyfile.txt

    Caution

    Be mindful of not deleting the file!

  4. 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

  1. Revise the commands we have used since the last exercise. Could you use a shell command for this?

  2. Ask Git for the current state of the repository using…

  3. Rename the file lines.txt as Lines.txt using…

  4. Verify that the working tree contains Lines.txt using…

  5. 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.

  1. The baseline

    1. Revise the commands launched since the last exercise using…

    2. Check the output of git status

  2. The working tree

    1. Append the fifth line to Lines.txt using…

    2. Repeat 1.2 and compare its output with the previous

  3. The index

    1. Stage the changes of Lines.txt in the index using…

    2. Repeat 1.2 and compare its output with the previous ones

  4. The history

    1. Store the changes of Lines.txt in the history using…

    2. Repeat 1.2 and compare its output with the previous ones

    3. 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

  1. The baseline

    1. Revise the commands launched since the last exercise using…

    2. Check the output of git diff

  2. The working tree

    1. Append the sixth line to Lines.txt using…

    2. Repeat 1.2 and compare its output with the previous

  3. The index

    1. Stage the changes of Lines.txt in the index using…

    2. Repeat 1.2 and compare its output with the previous ones

  4. The history

    1. Store the changes of Lines.txt in the history using…

    2. Repeat 1.2 and compare its output with the previous ones

    3. 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

  1. The baseline

    1. Revise the commands launched since the last exercise using…

  2. Perform the commit routine

    1. Append the seventh line to Lines.txt using…

    2. Stage the changes of Lines.txt in the index using…

    3. Store the changes of Lines.txt in the history using…

  3. As many times as you like

    1. Check the history using…

    2. 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.

1.3.4 Marking the History Using Tags#

[10 min]

  • Lightweight tags

    git log --oneline
    git tag 'hey'
    git log --oneline
    git log
    git tag 'hey' HEAD~1
    git tag 'hey2' HEAD~1
    git log --oneline
    git tag 'hey3' HEAD~5
    git tag 'hey4' 87ab7b6
    git log --oneline
    git diff HEAD HEAD~1 Line.txt
    git diff HEAD HEAD~1 Lines.txt
    git diff hey hey2 Lines.txt
    git log --oneline
    git diff hey hey2 Lines.txt
    echo 'eighth line' >>Lines.txt
    cat Lines.txt 
    git status 
    git add Lines.txt
    git status 
    git commit -m 'Add eighth line' Lines.txt
    git log --oneline
    git tag -d hey1
    git tag -d hey4
    git log --oneline
    git diff hey hey4 Lines.txt
    git log --oneline
    git tag -d hey2
    git tag -d hey
    git tag HEAD~4 v1
    git tag v1 HEAD~4
    git log --oneline
    git tag v2 HEAD~3
    git tag v3 HEAD~2
    git log --onelein
    git log --oneline
    git tag v4 HEAD~1
    git tag v5 HEAD
    git log --oneline

Exercise 9 — Add lightweight tags to the history [5 min]

Lesson 1 Episode 3 — Add lightweight tags to the history

Please perform the following tasks individually

Two commits come from exercises (as opposed to instructor-led typing)

  1. Find out which commits they where

  2. Tag them as exercises using… Use capital letters for the tag name (for our convenience)

  3. Verify the outcome of tagging with two Git commands, namely … and …

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
  • Annotated Tags

    git tag -a
    git tag -a -m 'First annotated tag' 
    git tag -a -m 'First annotated tag' TAG1
    git log --oneline
    git tag
    git show
    git show HEAD~1
    true

Wrap Up#

[10 min]

Note

Give a short wrap up about what has been learned.