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
You can turn it to a Git repository with
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
.
Check the status of the repository:
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:
Exercise 2
Check the status again and add the file to be committed as instructed in the Git messages.
Check the 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
Make a version of the repository, i.e. commit the new file with
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.
Now you can check the differences against the previous version:
Exercise 4
Add and commit your changes as before.
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
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:
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:
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
OUTPUT
fatal: destination path 'git-example' already exists and is not an empty directory.
Give it another name, e.g. git-example-friend
Exercise 6
What is the remote for this local repository?
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
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:
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.