Skip to content

Commit

Permalink
Merge pull request #46 from C2SM/reorder
Browse files Browse the repository at this point in the history
Make beginners and advanced course independent of each other
  • Loading branch information
jonasjucker authored Sep 20, 2022
2 parents 69aded9 + 5134a11 commit 60f6c63
Show file tree
Hide file tree
Showing 36 changed files with 108 additions and 82 deletions.
49 changes: 5 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,47 +1,8 @@
# Git Course for Beginners
This repository contains the exercises for the C2SM Git Workshop "Git for Beginners".
Feel free to download the material to practice and enhance your Git skills.
# C2SM Git Courses

The corresponding [slides of this course](https://wiki.c2sm.ethz.ch/CM/WorkshopBestPractices2013) can be found
at the C2SM wiki.
## Git Course for Beginners
The folder [beginner](beginner) contains the exercises for the C2SM Git Workshop "Git for Beginners".

A Markdown version of the .ipynb files for each exercise is located in the [Markdowns folder](Markdowns).

We recommend to make use of the convienient way of doing the exercises with Jupyter Notebook itself.
For installation instructions see section below.

## How to run the exercises
First of all, you need a Git installation on your computer.
To do so, please follow the [instructions from GitHub](https://github.com/git-guides/install-git).

> **_Important note:_** You must have at least Git 2.28 (released 27 July 2020) installed.
> You can check your Git version by typing `git --version` in your terminal.
Second, you need a Python installation as well.
We provide you here with [instructions from realpython](https://realpython.com/installing-python/),
but of course many other instruction will do it as well.

Third, you need some non-standard python packages, namely:
- jupyterlab
- bash_kernel

To install these two packages, execute the following command in your terminal:
```
python -m venv git-course_env
source git-course_env/bin/activate
pip install -r requirements.txt
```
On some machines an additional step is necessary:
```
python -m bash_kernel.install
```

Finally, you can start with the exercises by running:
```
jupyter notebook
```
This command opens the Jupyter Notebook interface in your browser.
If it does not open automatically, you have to copy and paste the URL manually.
In that case, please follow the instructions in your terminal.

**Have fun!**
## Git Course for Advanced
The folder [advanced](advanced) contains the exercises for the C2SM Git Workshop "Git for Advanced"
5 changes: 3 additions & 2 deletions advanced/Exercise_3.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Exercise 3 - Ignoring files

In this exercise, you will first use a provided script to generate a simple git repository containing materials for planning a conference. You will then create a README file for the conference planning repository using the Markdown language. Then you will generate an HTML README file, and finally create a `.gitignore` file for the repository. This exercise is closely adapted from [this website](https://oesa.pages.ufz.de/git-exercises/exercise-5.html).
In this exercise, you will first use a provided script to generate a simple git repository containing materials for planning a conference. You will then create a README file for the conference planning repository using the Markdown language. Then you will generate an HTML README file, and finally create a `.gitignore` file for the repository. This exercise is closely adapted from the [OESA Git Workshop](https://oesa.pages.ufz.de/git-exercises/exercise-5.html).

* [Initialize the git repository](#initialize)

Expand All @@ -12,7 +12,7 @@ In this exercise, you will first use a provided script to generate a simple git

## Initialize the git repository <a name="initialize"></a>

Use the helper scripts provided in https://www.github.com/C2SM/git-course to set up a simple git repository.
Use the [helper scripts](helpers.sh) provided in folder [advanced](../advanced) to set up a simple git repository.

First, clone the git-course repository if you have not already done so in Exercise 1.

Expand All @@ -35,6 +35,7 @@ git config --global init.defaultBranch main
Source the file containing the helper scripts: `helpers.sh`

```plaintext
cd advanced
source helpers.sh
```
Run the `init_advanced_repo` script. This script will create a folder at the same level as the git-course repository containing a simple git repository called `conference_planning`.
Expand Down
4 changes: 2 additions & 2 deletions advanced/Exercise_4.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ This exercise uses the same git repository that was created in Exercise 3. If yo

## Stash your changes <a name="stash"></a>

Let's make a change to the repository by adding a conference breakfast to the schedule on day 1.
Let's make a change to the repository by adding a conference breakfast to the schedule on day 1. The following are examples of how to use the `sed` command line tool, which were tested on Linux but may not work on other platforms. You can also simply open the file in a file editor to make the change.

```plaintext
sed -i '/Daily/ a 08:30-09:00: Conference breakfast' schedule_day1
Expand Down Expand Up @@ -79,4 +79,4 @@ Let's suppose that in addition to working in the main branch of the repository,
git worktree add feature
```

Because the branch `feature` did not already exist, one has been created from the master branch. It is now located in the `feature` folder, and you can now navigate there and work as you normally would in your git working directory. Note that you can also push and fetch commits as you normally would to and from remote repositories if you need to.
Because the branch `feature` did not already exist, one has been created from the main branch. It is now located in the `feature` folder, and you can now navigate there and work as you normally would in your git working directory. Note that you can also push and fetch commits as you normally would to and from remote repositories if you need to.
6 changes: 3 additions & 3 deletions advanced/Exercise_5.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Exercise 5 - Using git rebase

In this exercise, we will learn to use git rebase to rewrite the history of a feature branch in order to keep the history of the main branch cleaner by avoiding merge commits. First we will use the merge strategy to add a feature to a main branch that is being simultaneously developed. Then we will use the rebase strategy to do the same thing and compare the differences.
In this exercise, we will learn to use `git rebase` to rewrite the history of a feature branch in order to keep the history of the main branch cleaner by avoiding merge commits. First we will use the merge strategy to add a feature to a main branch that is being simultaneously developed. Then we will use the rebase strategy to do the same thing and compare the differences.

This exercise uses the same git repository that was created in Exercise 3. If you have not already done so, you can create it by following the instructions in the `Initialize the git repository` section [here](./Exercise_3.md).
This exercise uses the same git repository that was created in Exercise 3. If you have not already done so, you can create it by following the instructions in the ["Initialize the git repository" section of Exercise 3](./Exercise_3.md#initialize).

* [Use the merge strategy to incorporate a change into a moving main branch](#merge)

Expand All @@ -18,7 +18,7 @@ Create and switch to a new feature branch.
git switch -c merge_feature
```

Make a change to the schedule for day 2. Let's add a presentation session. The following gives examples using the `sed` command line tool, which were tested on Linux but may not work on other platforms. You can also simply open the file in a file editor to make the change.
Make a change to the schedule for day 2. Let's add a presentation session. The following are examples of how to use the `sed` command line tool, which were tested on Linux but may not work on other platforms. You can also simply open the file in a file editor to make the change.

```plaintext
sed -i '/Coffee/ a 11:15-12:30: Presentation session' schedule_day2
Expand Down
2 changes: 1 addition & 1 deletion advanced/Exercise_6.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

In this exercise, we will learn to use git to cherry pick a commit. We will add a series of commits to a feature branch, and then use cherry pick to apply only one of these commits to the main branch.

This exercise uses the same git repository that was created in Exercise 3. If you have not already done so, you can create it by following the instructions in the `Initialize the git repository` section [here](./Exercise_3.md).
This exercise uses the same git repository that was created in Exercise 3. If you have not already done so, you can create it by following the instructions in the ["Initialize the git repository" section of Exercise 3](./Exercise_3.md#initialize).

* [Add feature branch and commits](#feature)

Expand Down
6 changes: 3 additions & 3 deletions advanced/Exercise_7.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Exercise 7 - Using git submodule

This exercise is designed to be paired with Exercise 7 to help you compare and contrast the two main methods for embedding a git repository into another git repository. This exercise illustrates the submodule method, while the next exercise illustrates the subtree method. In this exercise, we will learn to use git submodule to nest a git repository inside another one. We will first add the git submodule and examine how the host and nested repositories deal with changes in their respective files. We will then learn how to pull and push changes to and from the sub-repository.
This exercise is designed to be paired with Exercise 8 to help you compare and contrast the two main methods for embedding a git repository into another git repository. This exercise illustrates the submodule method, while the next exercise illustrates the subtree method. In this exercise, we will learn to use git submodule to nest a git repository inside another one. We will first add the git submodule and examine how the host and nested repositories deal with changes in their respective files. We will then learn how to pull and push changes to and from the sub-repository.

This exercise uses the same git repository that was created in Exercise 3. If you have not already done so, you can create it by following the instructions in the `Initialize the git repository` section [here](./Exercise_3.md).
This exercise uses the same git repository that was created in Exercise 3. If you have not already done so, you can create it by following the instructions in the ["Initialize the git repository" section of Exercise 3](./Exercise_3.md#initialize).

* [Fork a repository on Github and add it as a submodule](#submodule)

Expand Down Expand Up @@ -45,7 +45,7 @@ The output shows that there are some changes to the repository, and they are alr
git commit -m "Add the posters submodule"
```

Note that the .gitmodules file has been added, which contains the path of the submodule in the host repository and the address where the submodule is hosted which is the Github URL in our case. It could also be another git web host, a file path, or anywhere else a repository is hosted.
Note that the `.gitmodules` file has been added, which contains the path of the submodule in the host repository and the address where the submodule is hosted which is the Github URL in our case. It could also be another git web host, a file path, or anywhere else a repository is hosted.

Check the status of the submodule.

Expand Down
10 changes: 6 additions & 4 deletions advanced/Exercise_8.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Exercise 8 - Using git subtree

This exercise is designed to be paired with Exercise 6 to help you compare and contrast the two main methods for embedding a git repository into another git repository. In this exercise, we will learn to use git subtree to nest a git repository inside another one. We will first add the git subtree and examine how the host and nested repositories deal with changes in their respective files. We will then learn how to pull and push changes to and from the sub-repository.
This exercise is designed to be paired with Exercise 7 to help you compare and contrast the two main methods for embedding a git repository into another git repository. In this exercise, we will learn to use `git subtree` to nest a git repository inside another one. We will first add the git subtree and examine how the host and nested repositories deal with changes in their respective files. We will then learn how to pull and push changes to and from the sub-repository.

This exercise uses the same git repository that was created in Exercise 3. If you have not already done so, you can create it by following the instructions in the `Initialize the git repository` section [here](./Exercise_3.md).
This exercise uses the same git repository that was created in Exercise 3. If you have not already done so, you can create it by following the instructions in the ["Initialize the git repository" section of Exercise 3](./Exercise_3.md#initialize).

* [Add the posters repository using git subtree](#subtree)

Expand All @@ -20,13 +20,15 @@ Navigate to the folder containing the `conference_planning` folder; in other wor
cp -r conference_planning conference_subtree
```

Let's add your `posters` Github fork we created in Exercise 6 as a subtree to our conference planning repository.
Let's add your `posters` Github fork we created in Exercise 7 as a subtree to our conference planning repository.

```plaintext
cd conference_subtree
git subtree add --prefix posters YOUR_FORK_ADDRESS main --squash
```

> **_Note:_** If you get an error message of the type `Working tree has modifications. Cannot add.`, please first try `git switch main`. If the error still persists, type `git reset --hard`. For more information about this, check [this stackoverflow question](https://stackoverflow.com/questions/3623351/git-subtree-pull-says-that-the-working-tree-has-modifications-but-git-status-sa).
The `--prefix` option gives a folder name relative to the root of the parent repository where the subtree will be installed. And the `--squash` option squashes the history of the posters repository into one commit in the parent repository.

Check the status of the repository.
Expand Down Expand Up @@ -69,7 +71,7 @@ cd posters
git status
```

Here we see the same change listed as not staged for commit. This is a difference to the behavior we saw with submodules. With git subtrees, git considers both the host and nested repository to have the same staging area. The commits from the subtree are all put directly into the commit list of the host repository, unlike with git submodule where they are kept completely separate.
Here we see the same change listed as not staged for commit. This is a difference to the behavior we saw with submodules. With git subtrees, git considers both the host and nested repository to have the same staging area. The commits from the subtree are all put directly into the commit list of the host repository, unlike with `git submodule` where they are kept completely separate.

Now try making a change inside the submodule. Let's add another poster title to the poster schedule.

Expand Down
File renamed without changes.
File renamed without changes.
24 changes: 24 additions & 0 deletions advanced/helpers.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

init_advanced_repo () {
mkdir ../advanced_git
cd ../advanced_git
mkdir -p conference_planning
cd conference_planning
git init
cp ../../advanced/examples/schedule_day1 .
cp ../../advanced/examples/schedule_day2 .

git add schedule_day1 && git commit -m "Add schedule_day1"
git add schedule_day2 && git commit -m "Add schedule_day2"

ed -s schedule_day1 <<< $'/program/\na\n09:00-11:00: Poster session\n.\nw\nq' > /dev/null
ed -s schedule_day2 <<< $'/program/\na\n09:00-11:00: Poster session\n.\nw\nq' > /dev/null
git add * && git commit -m "Add poster sessions in the morning"
ed -s schedule_day1 <<< $'/session/\na\n11:00-11:15: Coffee break\n.\nw\nq' > /dev/null
ed -s schedule_day2 <<< $'/session/\na\n11:00-11:15: Coffee break\n.\nw\nq' > /dev/null
git add * && git commit -m "Add coffee break"
git branch -m main
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
47 changes: 47 additions & 0 deletions beginner/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Git Course for Beginners
This folder contains the exercises for the C2SM Git Workshop "Git for Beginners".
Feel free to download the material to practice and enhance your Git skills.

The corresponding [slides of this course](https://wiki.c2sm.ethz.ch/CM/WorkshopBestPractices2013) can be found
at the C2SM wiki.

A Markdown version of the .ipynb files for each exercise is located in the [Markdowns folder](Markdowns).

We recommend to make use of the convienient way of doing the exercises with Jupyter Notebook itself.
For installation instructions see section below.

## How to run the exercises
First of all, you need a Git installation on your computer.
To do so, please follow the [instructions from GitHub](https://github.com/git-guides/install-git).

> **_Important note:_** You must have at least Git 2.28 (released 27 July 2020) installed.
> You can check your Git version by typing `git --version` in your terminal.
Second, you need a Python installation as well.
We provide you here with [instructions from realpython](https://realpython.com/installing-python/),
but of course many other instruction will do it as well.

Third, you need some non-standard python packages, namely:
- jupyterlab
- bash_kernel

To install these two packages, execute the following command in your terminal:
```
python -m venv git-course_env
source git-course_env/bin/activate
pip install -r requirements.txt
```
On some machines an additional step is necessary:
```
python -m bash_kernel.install
```

Finally, you can start with the exercises by running:
```
jupyter notebook
```
This command opens the Jupyter Notebook interface in your browser.
If it does not open automatically, you have to copy and paste the URL manually.
In that case, please follow the instructions in your terminal.

**Have fun!**
File renamed without changes.
File renamed without changes.
7 changes: 7 additions & 0 deletions beginner/examples/schedule_day1
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
*********** Schedule for Day 1 ***************
Daily program



**********
Evening activity
7 changes: 7 additions & 0 deletions beginner/examples/schedule_day2
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
*********** Schedule for Day 2 ***************
Daily program



**********
Evening activity
23 changes: 0 additions & 23 deletions helpers.sh → beginner/helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -118,29 +118,6 @@ init_broken_repo () {
ls
}
init_advanced_repo () {
mkdir ../advanced_git
cd ../advanced_git
mkdir -p conference_planning
cd conference_planning
git init
cp ../../git-course/examples/schedule_day1 .
cp ../../git-course/examples/schedule_day2 .
git add schedule_day1 && git commit -m "Add schedule_day1"
git add schedule_day2 && git commit -m "Add schedule_day2"
ed -s schedule_day1 <<< $'/program/\na\n09:00-11:00: Poster session\n.\nw\nq' > /dev/null
ed -s schedule_day2 <<< $'/program/\na\n09:00-11:00: Poster session\n.\nw\nq' > /dev/null
git add * && git commit -m "Add poster sessions in the morning"
ed -s schedule_day1 <<< $'/session/\na\n11:00-11:15: Coffee break\n.\nw\nq' > /dev/null
ed -s schedule_day2 <<< $'/session/\na\n11:00-11:15: Coffee break\n.\nw\nq' > /dev/null
git add * && git commit -m "Add coffee break"
git branch -m main
}
init_repo_remote () {
mkdir -p conference_planning
cd conference_planning
Expand Down
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.

0 comments on commit 60f6c63

Please sign in to comment.