Jump to content

Git: Difference between revisions

From Bath Wiki
m Fix broken links
Line 90: Line 90:
== See also ==
== See also ==


{{Special:PrefixIndex|prefix=Git|namespace=0}}
{{Special:PrefixIndex|prefix=Git/|namespace=0}}


[[Category:Tutorial]]
[[Category:Tutorial]]

Revision as of 00:08, 4 June 2026

Git is a “version control software” which can be downloaded on their website. 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:

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.email "your_email@abc.com"
git config user.name "Your Name"
Make sure the email is also linked to your account on GitHub or GitLab, see our GitLab documentation.

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 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 and pull
  • 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: git@gitlab.bath.ac.uk:cs/wiki
To convert from http to ssh, you replace https:// with git@ (which is the user we are sshing as) and then replace the first / with :, specifying the folder location of the repo we are copying. It is exactly the same syntax as scp (or ssh copy).

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 keys.

You can generate one by following our documentation, 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

  1. Follow this workflow:
    1. Pull main
    2. Create a branch
    3. push
    4. Create a pull/merge request
    5. Merge into main
    6. Pull main
  2. Don’t push to main directly
  3. Add a README.md
  4. Add .gitignore file: you can choose what files and folders git should ignore, for example, .env file containing API keys.
  5. Write descriptive commit and PR/MR messages
  6. 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!
  7. 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

See also