Getting Started
Git is a “version control software”. It allows for version management:
- Git keeps track and history of all changes and version of the software you
are writing, and lets you:
- Revert and jump back and forth in history
- Create different versions or “branches” of your software
- Manage versions in a better way than “LATEST_COURSEWORK_X.zip”
- Easily see who last edited a line of code
- It makes collaborating on the same codebase easier
A lot of programs have ways to help you interact with git so you don’t have to go through the terminal for most things. Some programs that integrate well:
- VSCode
- All JetBrains products
- Vim with fugitive
- GitHub Desktop
If you want a UI in the terminal on:
- Lazygit (Linux/MacOS)
- GitUI (Linux/MacOS/Windows)
- And many more…
Here we will mostly focus on the parts of git and using the command line tool as it more easily translates to any of the tools above. Plus for the more complicated routines, you can only do it via the terminal.
Alternative tutorials
This page is covers a lot of topics (including some commands which are a bit more advanced but useful for managing projects), and can be read all in one go or can just be used as reference. But there are also other resources which may be a bit more your speed:
- W3Schools git tutorials
- Atlassians indepth git tutorials
- The colourful intro
- Git in VSCode
- Git Troubleshooting
- BCSS Technical Labs
Setup
When you first download git you need to do a bit of configuring before you start. This is just so when you start committing, it will come up with your name and email address.
git config user.name "Your Name"
You can also configure the name of the default branch to main
instead of
master
. See why.
git config --global init.defaultBranch main
Remote Repos
These are the web applications such as GitLab or GitHub. For bathcs.com, we use the Bath’s GitLab instance and have instructions here. But also note Bath has a GitHub enterprise instance running here.
They normally provide many features and tools for version control, collaboration and CI/CD.
- You can
push
your local repository to them. - Then on another device you can
pull
this code from them. - Collaborators that work on your code can also
push
andpull
- You can collaborate with other people, on the same codebase
- Provides web UI for
git
- Acts as a “cloud backup”
HTTP vs SSH
In the rest of the page, we will be using URLs to pull from remote repositories.
On both GitHub and GitLab you can do this in two ways:
http
, where urls look like:https://gitlab.bath.ac.uk/cs/wiki
ssh
, where urls look like:[email protected]:cs/wiki
In most cases this does not matter. With public repositories http
does not
require you to log in, however it cannot handle larger repos at lower
internet speeds (as it times out).
However when you have setup 2FA, you can’t just use your credentials to login,
which is what https
normally requires. Therefore, you need to setup ssh
to
work, which also requires you to have a SSH key.
You can generate one by following our documentation here, which then you copy and paste the public key into the relavent section on GitHub or GitLab (see our GitLab documentation, or the GitHub documentation).
Once you have set up this ssh key, you should be able to clone and push to a repo without specifying your login details.
Best Practices
- Follow this workflow:
- Pull main
- Create a branch
- push
- Create a pull/merge request
- Merge into main
- Pull main
- Don’t push to main directly
- Add a
README.md
- Add
.gitignore
file: you can choose what files and folders git should ignore, for example,.env
file containing API keys. - Write descriptive commit and PR/MR messages
- Git is simple in the basics, and very complicated but very powerful once you dive into it. Basics are enough to have version control. Use them!
- It’s hard to permanently lose data in git. Almost always you can restore in some way. Don’t panic. Use your favourite search engine or Dang It Git