3. Git¶
Like most tools we present in this course there is a difference between what git can do and what you should know about it.
3.1. What is Git?¶
Git is a distributed version control system that tracks versions of files. It is often used to control source code by programmers who are developing software collaboratively (Wikipedia).
In the introductory lecture we mentioned GitHub Desktop which is a very useful app to learn git from a graphical user interface (GUI), rather than from the command line interface (CLI).
3.2. Git platforms¶
There are three main platforms hosting git repositories on the web:
In this course we use GitHub and Gitlab.
GitHub is the most popular (as of now), and GitLab is the one contracted by the University (also very popular).
Here we focus on GitHub. The workflow is almost identical for GitLab, it is just the web interface that is slightly different.
3.3. Forks¶
If you are working on a project already hosted on github you should fork it to your account. This will allow you to modify it, keep track of what you are doing, and contribute to it.
To fork a repo you can click on the fork button on the top right of the repository github page.
Exercise: fork the repository ResearchComputing to your account.
3.4. Clones¶
A clone is a copy of a repository. You can clone any github repository to your account with the git clone command.
Exercise: clone your fork of the repository ResearchComputing to your account.
3.5. Branches¶
A git repository can have many branches which intend to develop different features of the project. At some point, changes from different branches can be merged together. When changes are approved by project owners, they can be merged into the main branch.
To look at the branches in your repository use:
git branch -a
The -a option shows all branches, including remote ones. A star * will appear next to the branch you are currently on.
To switch to an existing branch use:
git switch <branch_name>
To create a branch use:
git switch -c <branch_name>
3.6. Pulls¶
When multiple people are collaborating on a same repository, it is possible that your local branch is out of date because someone else has made an update. To check the status of your local branch, use:
git status
If the git status command shows that your branch is behind the origin branch, you can update it with:
git pull
Try to do this every time before you start working on the repository, as otherwise when you stage and commit changes (see next paragraph) you may find yourself in trouble caused by file conflict.
3.7. Commits¶
Commits are what allow you to keep track of what you are doing. You can check status of your files by typing:
git status
And you can see the commit history with:
git log
If the git status shows that some files have been modified, you can stage them for commit with:
git add <file_name1> <file_name2> ...
or all of them with:
git add .
To commit your changes use:
git commit
This will open an editor like vim where you can write a commit message. You can exit the editor by pressing Esc then :wq.
You can push your changes to the remote repository with:
git push
3.8. Pull requests¶
Pull requests are used to merge changes from your branch into the main branch of the remote repository (the original one, not your fork if you have one).
You can do this online, on the web interface of the repository.
The pull request are reviewed by the owner, which can also do it via the web interface.
3.9. Updating your fork¶
To update your fork, you can use the web interface of GitHub.
3.10. Merge changes between branches¶
You can update your branches by merging changes from one branch into another
Switch to the branch you want to update:
git switch <branch_name>
And then merge the changes from the branch you want to update into the current branch with:
git merge <branch_name>
Exercise: Practise all of these steps with our course repository. If you have GitHub Desktop, try to do all of these steps on GitHub Desktop instead of the command line.