Content from Introduction
Last updated on 2024-06-26 | Edit this page
Overview
Questions
- What is Git?
- What is the point of these exercises?
Objectives
- Learn what is Git and why you should be using it
What is Git?
Let’s learn about Git Source Code Management (SCM) system, and why we all use it!
From the Git website:
Callout
Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.
Git is easy to learn and has a tiny footprint with lightning fast performance. It outclasses SCM tools like Subversion, CVS, Perforce, and ClearCase with features like cheap local branching, convenient staging areas, and multiple workflows.
Why should I use Git?
You should use Git for storing and sharing your work.
Your should use Git to version your code.
You should learn Git to be able to contribute to common projects.
What can I learn here?
This is not intended as a full tutorial: you will get familiar with some basic concepts, and do some hands-on exercising to gain confidence with Git. You will find links to full-fledged lessons, there are many of them!
Key Points
- Git is a version control system.
- It is widely used, it is the most popular source code management tool.
- We will use Git during the hands-on sessions to store and share code.
Content from Learning Git
Last updated on 2024-06-25 | Edit this page
Overview
Questions
- Where do I find tutorials on Git?
Objectives
- Get an overview of existing Git tutorials
Is this a Git tutorial?
No, this is a hands-on session for you to exercise basic Git commands so that you do not have to do it with your real projects.
Take your time to do it now, not when you are busy finalizing your analysis code.
Git tutorials
Excellent Git tutorials exist, the teaching approach varies.
- Git’s website: https://git-scm.com/doc
- introductory videos
- quick reference guides
- reference manual
- Git Pro book
- curated external links
- W3 schools: https://www.w3schools.com/git/default.asp
- CodeRefinery: https://coderefinery.github.io/git-intro/
- good emphasis on “why”
- starts by exploring an existing GitHub repository
- HEP Software Foundation: https://hsf-training.github.io/analysis-essentials/git/README.html
- starts by working hands-on on a local repository
- explains CERN GitLab features
Where do we start?
Key Points
- Excellent Git tutorials exist.
- Take your time to work through them.
Content from Basic setup
Last updated on 2024-06-26 | Edit this page
Overview
Questions
- Do you have Git installed?
- How to configure Git?
- How to connect to GitHub?
Objectives
- Have Git installed
- Configure Git user information
- Create a GitHub account
- Create a SSH key to authenticate to GitHub
Installing Git
Check if you have Git installed with:
If not found, go to the offical Git download instructions and install Git. If you working on WSL2 Ubuntu, choose Linux/Unix, not Windows.
Configuring Git
You might have used Git without user configuration, e.g. when you
just clone a repository from GitHub with
git clone https://github.com/<XXX>/<YYY>.git
.
To be able to add code (“push”) in repositories, you will need to to
configure user information.
Check if you have it already with:
If user.name
and user.email
appear in the
list, user configuration is done.
If not, configure user information with
Replace [...]
with your input, and keep the quotes
around your name if there are spaces.
You can read more about configuring Git in https://coderefinery.github.io/git-intro/configuration/#configuring-git-command-line-and-editor
GitHub
Create a GitHub account
In this workshop, we will use GitHub as a “remote” repository. If you do not have a GitHub account yet, create one by following “Sign up” from the GitHub homepage. Note that the email address should match the one that you configured for local Git.
Generate a SSH key and add it to GitHub for authentication
To be able to push code to GitHub from your terminal, you will need to authenticate. SSH is the recommended method.
Check if you already have set it up:
It is done, if you get:
OUTPUT
Hi yourusername! You've successfully authenticated, but GitHub does not provide shell access.
If not, follow these instructions:
Key Points
- You need to configure Git user information for your local repository.
- You can use an SSH key to authenticate your connection to GitHub from terminal.
Content from 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.