LESSON 2: Branching and remote operations#

This is a summary of the lecture notes for this lesson. The original list of commands has been edited and decorated with headings corresponding to the lessons in the curriculum more clearly. Some commands have been either removed or relocated or added for clarity and help self-study after the lesson. Accidental errors have been removed, but intended errors have been kept.

The comments after each line are annotations on whether a command/option appears for the first time (new) or is a known action. Else, the annotation recalls why we typed that certain command, for example to observe the state of the play (typically before a certain change) or to verify the results of a change. routine commands are the typical commit sequence learned in Lesson 1.

The original list of commands is available at this commit as displayed by GitHub.

Note

The list of commands below has been grouped arbitrarily for readability’s sake.

Episode 1: Branching#

2.1.1 Create, rename, change and delete branches#

git branch                     # new command
git branch B1                  # new argument
git branch                     # verify
git status                     # verify
cat Lines.txt                  # verify
git branch -m B1 B2            # new short option
git log --oneline              # verify
git branch -d B2               # new short option
git log --oneline              # verify
git branch -m main foo       # known action
git log --oneline              # verify
git branch -d foo              # known action (fails)
git branch -m foo main         # known action
git branch                     # verify
git status                     # verify
git log --oneline              # verify
git branch B1                  # known action
git branch                     # verify
git log --oneline              # verify
git switch                     # new commmand (fails)
git switch B1                  # new argument: on branch B1
git log --oneline              # verify
git branch B2                  # known action
git log --oneline              # verify
git switch B2                  # known action: on branch B2
git log --oneline              # verify
git switch main                # known action: on branch main
git log --oneline              # verify

Exercise 1 — Get familiar with branches

Lesson 2 Episode 1 — get familiar with branches

Please perform the following tasks individually

  1. Revise the commands we have used so far

  2. Create at least three branches with any name you like, using…

  3. Rename them and switch between them at your pleasure, using…

  4. Check the status message and the history in each branch you are in, using… Take note of the output of the Git commands

Attention

  • DO NOT change content and name of the file

  • DO NOT stage and commit changes

At the end:

  1. If you have a banch master, rename it as main

  2. Return to the branch main

  3. Delete all other branches, using…

Formulate your 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
[Need to be completed]

```shell
(your work)
git branch                                                          # verify
git branch -d B1 B2                                                 # known action
git branch                                                          # verify
git log --oneline                                                   # verify
cat Lines.txt                                                       # verify
```

2.1.2 Develop and compare branches#

git status                                                      # observe
git branch B1                                                   # known action
git branch B2                                                   # known action
git branch                                                      # on branch main
git status                                                      # routine with git status
echo 'ninth line' >>Lines.txt                                   # routine with git status
git status                                                      # routine with git status
git add Lines.txt                                               # routine with git status
git status                                                      # routine with git status
git commit -m 'Add ninth line on main' Lines.txt                # routine with git status
git status                                                      # routine with git status
git log --oneline                                               # verify
git status                                                      # verify
git switch B1                                                   # on branch B1
git status                                                      # verify
git log --oneline                                               # verify
git log --oneline --all                                         # new long option
cat Lines.txt                                                   # verify
git status                                                      # routine with git status
echo 'ninth line' >>Lines.txt                                   # routine with git status
git status                                                      # routine with git status
git add Lines.txt                                               # routine with git status
git status                                                      # routine with git status
git commit -m 'Add ninth line on B1' Lines.txt                  # routine with git status
git status                                                      # routine with git status
git log --oneline                                               # verify
git log --oneline --all                                         # verify
git log --oneline --all --parents                               # new long option
git switch main                                                 # on branch main
git log --oneline --all                                         # verify
echo 'ninth line (duplicate)' >> Lines.txt                      # routine (an intended mistake)
git add Lines.txt                                               # routine
git commit -m 'Add tenth line on main (with mistake)' Lines.txt # routine
cat Lines.txt                                                   # verify

2.1.3 Visualise and merge branches, and resolve conflicts#

git log --oneline --all                                # explore
git log --oneline --all --parents                      # explore
git log --oneline --all --parents --graph              # explore
git switch B1                                          # on branch B1
cat Lines.txt                                          # verify
echo 'tenth line on B1' >> Lines.txt                   # routine
git add Lines.txt                                      # routine
git commit -m 'Add tenth line on B1' Lines.txt         # routine
git log --oneline --all --graph                        # verify
git switch main                                        # on branch main
git diff HEAD HEAD~1 Lines.txt                         # explore
git diff main B1 Lines.txt                             # new arguments
git diff main B1~1 Lines.txt                           # new arguments
git diff main~1 B1~1 Lines.txt                         # new arguments
git log --oneline --all --graph                        # verify
git status                                             # verify
nano Lines.txt                                         # edit file to fix mistake
cat Lines.txt                                          # verify
git status                                             # verify
git add Lines.txt                                      # routine
git commit -m  'Correct tenth line on main' Lines.txt  # routine
git log --oneline --all --graph                        # routine
cat Lines.txt                                          # verify

Exercise 2 — Explore differences across branches

Lesson 2 Episode 1 — explore differences across branches

Please perform the following tasks individually

  1. Move to branch B2

  2. Inspect several previous versions in the history using git diff with appropriate arguments

  3. Repeat at your own pleasure

See how far back in the history of another branch you can go!

Formulate your 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]

Exercise 3 — Commit in a secondary branch

Lesson 2 Episode 1 — commit in a secondary branch

Please perform the following tasks individually

  1. Stay in (or go to) branch B2 (the file Lines.txt contains eight lines)

  2. Append a ninth and a tenth line in the usual way

  3. Make a single commit of these two changes. Specify the branch name in the commit message!

  4. Inspect the working tree

Formulate your 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
```shell
git switch B2                                             # on branch B2
cat Lines.txt                                             # verify
echo 'ninth line' >>Lines.txt                             # routine
echo 'tenth line' >>Lines.txt                             # routine
git add Lines.txt                                         # routine
git commit -m 'Add ninth and tenth line on B2' Lines.txt  # routine
git status                                                # verify
git log --oneline --all --graph                           # verify
```
git switch B1                                             # on branch B1
echo 'eleventh line' >>Lines.txt                          # routine
echo 'twelfth line' >> Lines.txt                          # routine
git diff                                                  # routine
git add Lines.txt                                         # routine
git commit -m 'Add 11th and 12th lines on B1' Lines.txt   # routine
git log --oneline --all --graph                           # routine
git switch main                                           # on branch main
git diff main B1 Lines.txt                                # verify
git merge -m 'Merge development from branch B1' Lines.txt # wrong syntax
git merge -m 'Merge development from branch B1'           # wrong syntax
git merge -m 'Merge development from branch B1' B1        # right syntax: fails because of conflict
git diff Lines.txt                                        # verify
cat Lines.txt                                             # explore
nano Lines.txt                                            # edit the file within the conflict decorations
cat Lines.txt                                             # verify
git diff                                                  # verify
git status                                                # verify
git add Lines.txt                                         # mark resolution
git status                                                # verify
git commit -m 'Merge changes from B1 into main' Lines.txt # conclude merge wrong syntax
git commit -m 'Merge changes from B1 into main'           # conclude merge right syntax
cat Lines.txt 
git status                                                # verify
git log --oneline --all --graph                           # verify
git log --oneline --all --graph --parents                 # verify

Episode 2 Operations with remotes#

2.2.1 Create and explore a bare repository#

pwd                           # observe
cd ..                         # directory of the workshop
pwd                           # verify
ls -F                         # observe
git init --bare git-zero.git  # new long option
ls -F git-zero.git            # verify
ls -Fa git                    # observe
ls -Fa git/.git               # observe
cd git-zero.git               # directory of the bare repository
ls                            # observe
git status                    # observe (fails)
git log                       # observe (fails)
git branch                    # observe
cd ..                         # observe
ls                            # verify

2.2.2 Cloning and pushing to “remote” bare repositories (upstreams)#

git clone git-zero.git git-one                    # new command
ls -Fa git-one                                    # observe
ls -Fa git-one/.git                               # observe
ls -F                                             # observe
cd git-one                                        # directory of the first clone
git status                                        # observe
git branch                                        # observe
git log                                           # observe (fails)
git remote                                        # new command
git remote -v                                     # new short option
echo 1 >> numbers.txt                              # routine
cat numbers.txt                                   # verify
git status                                        # routine with git status
git add numbers.txt                               # routine with git status
git status                                        # routine with git status
git commit -m 'git-one: add first 1' numbers.txt  # routine with git status
git log --oneline                                 # verify
git status                                        # verify (ignore the but-warning)
git push                                          # new command
git status                                        # verify
cd ../git-zero.git                                # directory of the upstreamgit
git status                                        # observe (fails)
git log                                           # observe
ls -F                                             # observe
cd ..                                             # in the workshop directory
pwd                                               # verify
git clone git-zero.git/ git-two                   # known action
ls -F                                             # verify
cd git-two/                                       # in the second clone
ls -aF                                            # verify
cat numbers.txt                                   # observe
git log                                           # observe
git remote -v                                     # observe
echo 2 >>numbers.txt                              # routine
cat numbers.txt                                   # verify
git add numbers.txt                               # routine
git commit -m 'git-two: add first 2' numbers.txt  # routine
git log --oneline                                 # verify
git push                                          # known action
git status                                        # verify

Exercise 4 — A first type of merging

Lesson 2 Episode 1 — a first type of merge

Please perform the following tasks individually

How many more lines has Lines.txt in B1 than in main? Please merge B1 in main Verify the outcome with a graphed git log

Attention

  • DO NOT merge B2

  • DO NOT delete branches

Formulate your 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
```shell
# No answers yet
```

2.2.3 Fetching and merging (pulling) from upstreams#

cd ../git-one                                      # in the first clone
pwd                                                # verify
git status                                         # verify
git log --oneline                                  # routine
git fetch                                          # new command
git status                                         # routine
git log --oneline                                  # routine
git log --oneline --all                            # routine
cat numbers.txt                                    # routine
# git pull = git fetch + git merge                 # git has compound commands
git fetch                                          # known action
git merge                                          # see episode 2.1 (no conflict here)
git log --all                                      # verify
cat numbers.txt                                    # verify
echo 1 >>numbers.txt                               # routine
cat numbers.txt                                    # routine
git add numbers.txt                                # routine
git commit -m 'git-one: add second 1' numbers.txt  # routine
git log --oneline --all                            # verify
git status                                         # verify
git push                                           # known action
git status                                         # verify
git log --oneline --all                            # verify

2.2.3 (continued) Resolving conflicts when pushing#

cd ../git-two/                                     # in the directory of the second clone
ls                                                 # observe
cat numbers.txt                                    # observe
git status                                         # observe
git log --oneline                                  # observe
echo 2 >>numbers.txt                               # routine
cat numbers.txt                                    # verify
git add numbers.txt                                # routine
git commit -m 'git-two: add second 2' numbers.txt  # routine
git status                                         # verify
git log --oneline                                  # verify
git push                                           # known action with conflict
git fetch                                          # known action
git status                                         # observe
git merge                                          # known action with conflict
git diff                                           # observe
cat numbers.txt                                    # observe
nano numbers.txt                                   # edit the file within the conflict decorations
cat numbers.txt                                    # verify
git status                                         # verify
git add numbers.txt                                # mark resolution
git status                                         # verify
git commit -m 'git-two: conclude merge'            # conclude merge
git status                                         # verify
git log --oneline                                  # verify
git push                                           # known action
git log --oneline                                  # verify
git status                                         # verify

Exercise 5 — Another type of merge

Lesson 2 Episode 1 — another type of merge

Please perform the following tasks individually

Please merge B2 in main If a conflict arises, make choices of your liking Verify the outcome with a graphed git log

Attention

DO NOT delete branches (for the purpose of this workshop)

Formulate your 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
```shell
# No answers yet
```