Git repository management
This page should provide a guide that should help you to set up and use git history/version control for developing software.
Accessing the group git
The git host that we use for group work is the Exeter GitLab (we use this because it is offered to us and no one has to create a new login).
Use the following steps to access the Hepplestone git group:
- Go to the Exeter GitLab website https://git.exeter.ac.uk
- Log in using your university credentials
- Request an invite from one of the Hepplestone research group Maintainers (or higher), this is done verbally or via email
- FOR MAINTAINER OR HIGHER:
- Go to the Hepplestone git group page: https://git.exeter.ac.uk/groups/hepplestone
- On the left sidebar of the page, select Manage -> Members
- On the Members page, select "Invite members" on the top-right of the page
- Enter the person's username, select their role (defaults = Reporter for Masters/code users, Developer for basic coders (safe version), Maintainer for high-level coders)
- Optional: set an expiration date for their access
- Invite
- Go to https://git.exeter.ac.uk/groups/hepplestone to confirm access
Note: A hand-wavy description of the roles follows:
- Developer:
- access to creating new group projects (PLEASE CONFIRM)
- access to existing projects within the group
- clone repositories
- create branches
- commit to branches
- cannot commit to main (the default) branch of a project
- Maintainer:
- Developer+
- can commit to main branch
Connecting via ssh to remote repository
Firstly, we want to connect to GitLab using ssh (so that we don't have to put in our username and password every time we upload changes to the remote repository). This requires a few steps:
- go to git.exeter.ac.uk, go to the left sidebar and click on your avatar (top right of sidebar), go to Preferences.
- In Preferences, go to the left sidebar and select "SSH Keys".
- Click on "Add new key" on the right-hand side of the page.
- Follow the instructions on the page (or here). This will set up your device with an ssh key for a defined amount of time to authenticate your access to GitLab.
- Open a terminal
- Run
ssh-keygen -t rsa -b 2048 [-C "<GitLab>]".<GitLab>is an optional comment associated with the key. - Press ↵ Enter.
- You will then be prompted to provide a filepath/filename for your new id key (with RSA encryption).
- Pressing ↵ Enter without inserting anything defaults to the filepath in parentheses.
- Providing just a filename will save it in the default directory (/home/links/<USER>/.ssh/<//code>)
- As an example: one could provide "id_rsa_gitlab" to have a separate ssh key for GitLab.
- Press ↵ Enter.
- Specify a passphrase and confirm. This will be requested of you in the future the first time your session is connecting to GitLab, so effectively once every computer restart.
- A public and private key are now generated.
- Go to where you saved your ssh key to (e.g. /home/links/<USER>/.ssh/).
- Find the associated public key, e.g.
id_rsa.pubfor default (or, <FILENAME>.pub, where <FILENAME> is the filename you entered when prompted earlier). - Copy the public key file contents.
- Back on the GitLab webpage, paste the public key into the "Key" input field.
- Provide a title to remind you of what device it relates to.
- Leave the Usage type as default and set an expiration date (or leave blank if you want it never to expire).
You have now set up your ssh protocol for accessing GitLab.
Next, you want to change your local git config to automate more of this.
In terminal, type emacs ~/.gitconfig. Edit the git file so that it looks like:
[user]
email = <UNI EMAIL ADDRESS, i.e. a.b.cde@exeter.ac.uk>
name = <YOUR NAME, i.e. A Cde>
[push]
default = simple
[credential "https://git.exeter.ac.uk/"]
username = <UNI USERNAME, i.e. abc123>
[init]
defaultBranch = main
This sets your default username for requests sent to the remote repository.
Starting out with git
Clone remote repository: git clone <ADDRESS> (USE SSH CLONING, button on most repository websites, near top right hand side of webpage)
Or initialise new local repository: git init
Git commands
Help
git (get a brief git help)
git <TAG> -h (get a brief help with <TAG>)
git <TAG> --help (get an indepth help with <TAG>, like the man command)
Initial git commands
git clone (clone a remote repository into a new directory)
git init (create an empty git repository, or reinitialises an existing one. If I am right, needs to be done in the directory you want as the repository)
Version history
git add .
git mv (move or rename a file, NEVER USE THE mv COMMAND IN CODE, ALWAYS USE git mv, won't be tracked otherwise)
git rm (remove a file, NEVER USE THE rm COMMAND IN CODE, ALWAYS USE git rm, won't be tracked otherwise)
git commit -m "<COMMIT MESSAGE>" ALWAYS USE FUTURE TENSE, as if you are intending on doing it, i.e. "Add parameter", "Test code", "Fix typos". Be clear and concise!
git restore <FILE> (remove unstaged changed from file, i.e. revert file to last commit)
Recent changes to code
git status (show all unstaged and staged changes to working tree)
git diff (show unstaged changes to working tree)
git diff <FILE> (show unstaged changes of <FILE>)
Connecting with remote repository
git push (push all local commits to remote repository, warning, on first instance, won't work, but error tells you exactly what to do, set upstream)
git pull (pull all remote commits to local repository)
git fetch (unsure, like pull, but just stages remote changes for local commit)
Managing worktree branches
git branch (lists local branches)
git branch <BRANCH_NAME> (create a new branch from current called <BRANCH_NAME>)
git switch <BRANCH_NAME> (switch to existing branch <BRANCH_NAME>)
git branch -d <BRANCH_NAME> (delete local branch <BRANCH_NAME>)
git checkout <BRANCH_NAME> (create new branch and move to it)
git checkout tags/<TAG_NAME> (change files to version found in tag <TAG_NAME>, for accessing older versions of the code that are useful to access)
git checkout -B <BRANCH_NAME> (set current branch back to HEAD, create new branch from currently unstaged changes and moves you to that. Useful if you start making a new feature and realise it should have been a separate branch before you've done too much work on it)