Exercises

Last updated on 2024-06-26 | Edit this page

Overview

Questions

  • How to create a repository?
  • What is local, what is remote?
  • How to push your updates?
  • How to use branches?
  • And many more…

Objectives

  • Have Git installed
  • Configure Git user name
  • Create a GitHub account
  • Create an ssh key to authenticate to GitHub

Create a local Git repository


Create a new directory, e.g. git-example

BASH

mkdir git-example
cd git-example

You can turn it to a Git repository with

BASH

git init

If you have not configured the default branch name, you will see this message:

OUTPUT

hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint:   git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint:   git branch -m <name>

Exercise 1

If you got the output above, follow the advice in the message: change the branch name to main and configue your default initial branch name to main.

BASH

git config --global init.defaultBranch main
git branch -m main

Check the status of the repository:

BASH

git status

OUTPUT

On branch main

No commits yet

nothing to commit (create/copy files and use "git add" to track)

Now add a new file to the directory. You can use the following:

BASH

echo sometext > newfile.txt

Exercise 2

Check the status again and add the file to be committed as instructed in the Git messages.

Check the status:

BASH

git status

OUTPUT

On branch main

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        newfile.txt

nothing added to commit but untracked files present (use "git add" to track)

Add the file (or a snapshot of it at this moment) to the “staging” area with

BASH

git add newfile.txt

Make a version of the repository, i.e. commit the new file with

BASH

git commit -m "First version of newfile.txt"

OUTPUT

[main (root-commit) 0a0951c] First version of newfile.txt
 1 file changed, 1 insertion(+)
 create mode 100644 newfile.txt

Flag -m is followed by a commit message. When things go wrong, you will learn to appreciate clear and descriptive commit message.

Exercise 3

Find the git command to check the version history. Use e.g. Git cheat sheet.

Won’t show it, find it yourself!

Modify the file, e.g.

BASH

echo somenewtext >> newfile.txt

Now you can check the differences against the previous version:

BASH

git diff

Exercise 4

Add and commit your changes as before.

BASH

git add .
git commit -m "Update of newfile.txt"

Upload an existing local repository to GitHub


For now, your code is only in your local repository. You will now create a remote repository, so that you can store the code there and share it.

Go to your GitHub area (https://github.com/[yourgithubname]), choose the “Repositories” tab and click on New.

Choose git-example as the repository name, choose Public and leave other options as they are. This will create the repository and generate an instruction page, and you can copy the commands under the title “…or push an existing repository from the command line”.

Note that GitHub provides the command to change the branch name to main which we did already.

The git remote add... command

BASH

git remote add origin git@github.com:[yourgithubname]/git-example.git

defines your new GitHub repository as the remote repository and names it as origin. This is a common choice and is used when pushing the code in the repository:

BASH

git push -u origin main

The option -u links your local main to the remote main. Therefore you only need to do this once.

You can now check the remote repository location with:

BASH

git remote -v

OUTPUT

origin  git@github.com:[yourgithubname]/git-example.git (fetch)
origin  git@github.com:[yourgithubname]/git-example.git (push)

Check also that the code has appeared in the GitHub. If you still have the instruction page open, click on <> Code top-left to see the repository contents.

Optional exercise 5

Create a another GitHub repository following the instructions under the title “…or create a new repository on the command line”.

Note: create a new local directory first, do not nest Git repositories.

Go to your GitHub area (https://github.com/[yourgithubname]), choose the “Repositories” tab and click on New.

Choose git-example-web as the repository and leave other options as they are. This will generate an instruction page, and you can copy the commands under the title “…or create a new repository on the command line”.

You will notice that the commands are the same what we have done above. You can use these commands if you start from an empty directory.

Clone an existing repository from GitHub


Ask an another participant to provide you their GitHub repository address and clone it locally. As we all used the same name, plain cloning will fail with

BASH

git clone git@github.com:[someoneelse]/git-example.git

OUTPUT

fatal: destination path 'git-example' already exists and is not an empty directory.

Give it another name, e.g. git-example-friend

BASH

git clone git@github.com:[someoneelse]/git-example.git git-example-friend

Exercise 6

What is the remote for this local repository?

Git has created a new directory with the chosen name. First, move to it and then check the remote.

BASH

cd git-example-friend/
git remote -v

Using branches


If you are using GitHub as a remote storage for your code and you are the only contributor, you will most likely push your changes to the main branch.

However, when you contribute to other remote repositories, you would always use a branch of your own which can then be merged to the remote repo main branch.

In the git-example-friend repository, create a new branch:

git checkout -b [yourname]-new-feature

You are free to choose a name, but it is useful agree on some rules or practices in a project with several contributors. A common choice is something with your name and something that indicates what this branch is for.

You can check the branch with

git branch

The star indicates in which branch you are.

Exercise 7

Create a new file in the repository, add and commit it locally and then push it to the remote repository.

Note: instead of main in git push origin main, you will now use your new branch name.

Create a file

BASH

echo sometext > yourname-file.txt

Check the status, add, commit and push:

BASH

git status
git add .
git commit -m "[yourname]: some descriptive message"
git push origin [yourname]-new-feature

Did you get an error? Ask your friend to invite you as a collaborator in the repository from Settings -> Collaborators and teams -> Add people.

In the GitHub Web UI of the repository, a message about recent pushes has now appeared. You can now open a pull request from the new branch to the main branch.

Click on Compare and pull.

Describe the pull request in the text field.

Then click on Pull.

You can then explore the changes, and add some discussion.

There is an option to review changes and approve them. It can be set as obligatory in the repository settings.

Usually, the owner of the project merges the pull request.

Once the pull request is merged, remember to update your local repository. Do the following:

BASH

git checkout master
git pull

Optional exercise 8

Try this for fun:

Create a Git repository on your university account (or lxplus at CERN if you have an account), add and commit some files in it.

Clone the repository to your laptop: instead of git@github.com:/<repository>.git, use youraccount@domain:/full/directory/path in git clone.

Check the remote repository address on your laptop.

Try if you can push changes from your laptop to the remote on your university account.

Note that this not necessarily what you would ever do, but it illustrates that Git is completely independent from GitHub or GitLab.

Note also that Git does not allow to push to a branch that is checked out in the remote repository. You will have to push to another branch.

Key Points

  • You can turn any directory to a versioned code repository with Git.
  • You can upload the content of a local repository to a remote repository such as GitHub.
  • You can contribute to other Git repositories.