- Access code from a Git web interface
- Push code changes to a Git web interface
- Examine the code repository on a Git web interface
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.
For this exercise, we will use the following functions from this file:
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.reset: This will delete the beginners_git directory and allows you a clean restart of the exercise in case you messed it up completely.
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/sentence.
Tip
Some exercises may require the use of basic Unix commands. If you are unfamiliar with Unix systems, refer to the file Basic Unix Commands for a list of all necessary commands.
If everything is still set up from the last exercise, you can continue with this exercise directly. Otherwise, please refer to the Initialization from Exercise 1.
In this exercise, we will work with a repository hosted on the GitHub website.
First, we'll create a fork of the repository so that you can have your own copy of a C2SM repository to work with. A fork is a complete copy of a repository into your own account, where you have full permission to make any changes you want to your forked repository. To create your fork, open another tab in your browser and navigate here: https://github.com/c2sm/git-example
Use the web interface to make a fork:
GitHub will indicate at the top left that this repository has been forked (left arrow).
Now copy the forked repository to your local workspace using the link (right arrow, dropdown from Code). If you have an SSH key on GitHub that connects your local Git to GitHub, use the SSH link to clone, otherwise use HTTPS.
Note
If you clone the repository with HTTPS, you will not be able to exchange any information with the remote repository without setting up a token (git push, git pull and git fetch will NOT work). Follow the instructions in Create a user token to use HTTPS for working with remote repositories to do so.
However, we strongly recommend that you create an SSH key for GitHub if you plan to work with it in the future. It will make things much easier!
We don't want the repository in our git-course repository. So make sure you are in the beginners_git directory instead. Use the Basic Unix Commands we provided to navigate there in case you are in the wrong directory.
-
Navigate into beginners_git repo if not there already.
-
Use
git clone <path_to_repository>to download your forked repository. -
Navigate into the cloned repository.
Let's examine the repository. Does it have any remotes? What branches are in it?
- Use
git remote -v,git branch -a, andgit statusto examine the repository.
You should have noticed that your local repository has a remote called "origin" pointing to your fork on GitHub. This is the default behavior when you use git clone to copy a repository.
Next, let's create a new branch and add a commit to it.
- Use
git switch -c <branch_name>to create a new branch. - Make a change in schedule_day1.txt and commit the change to your new branch.
Now, let's send our new branch to our GitHub fork.
- Push your local changes to the remote repository and set the upstream using
git push -u origin <branch_name>. Note that you only need to set the upstream (using-u) once, for subsequent pushes you must omit the-u.
Go back to GitHub and take a look at your forked repository.
Let's use the web interface to examine the repository. Try the following things there:
-
Find the list of commits and examine the files for a specific commit.
-
Change a file and use the web interface to make a new commit to a new branch and automatically create a pull request.
- Find the pull request and take a look at it.
Now, let's get the commit we made on GitHub into our local repository.
- Use
git fetch originto download the new commit from your fork. - Check the status of your Git repository.
Our new commit has been downloaded into a remote branch, but is not yet available in our local branch.
- Use
git merge <remote_name>/<branch_name> -m '<commit_message>'to sync up your local branch with the remote one.
Let's examine the difference between git fetch and git pull. We just used git fetch to get a commit from our remote repository, and then we used git merge to merge it into our local branch.
First, go back to the web interface and use it to make a new commit by editing a file in the branch you created earlier.
Next, let's merge that commit into our repository.
- Use
git pull originto download the new commit from your fork. - Take a look at your local branch. You should see that the commit you made has already been merged into your local branch automatically, because
git pulldoes bothgit fetchANDgit mergeautomatically.


