Difference between revisions of "Git repository management"
| Line 97: | Line 97: | ||
== Git commands == | == Git commands == | ||
This section provides a brief description of the key git commands (and their extensions) that have been found useful so far. '''NOTE:''' <code>git merge</code> has not been detailed here, it is often best to handle this on the GitLab website instead, and is a more difficult thing to handle, but should be learned at some point. | |||
==== Help ==== | ==== Help ==== | ||
<code>git</code> | The following are git commands that provide help and guidance for how to use git in terminal.</br> | ||
<code>git <TAG> -h</code> | <code>git</code> = provide a brief help</br> | ||
<code>git <TAG> --help</code> | <code>git <TAG> -h</code> = provide a brief help with <TAG></br> | ||
<code>git <TAG> --help</code> = provide an indepth help with <TAG>, like the man command</br> | |||
==== Initial git commands ==== | ==== Initial git commands ==== | ||
<code>git clone</code> | The following are git commands that are only used when initialising/setting up a git repository.</br> | ||
<code>git init</code> | <code>git clone <ADDRESS></code> = clone a remote repository into a new directory</br> | ||
<code>git init</code> = create an empty git repository, or reinitialises an existing one. '''PLEASE CONFIRM''' If I am right, needs to be done in the directory you want as the repository)</br> | |||
==== Version history ==== | ==== Version history ==== | ||
<code>git add .</code></br> | The following are git commands that allow one to stage and commit code changes. As convention, it is good practise to regularly perform commits, to keep track of changes. Each set of changes should be their own commits, e.g. do not stage bug fixes and a new procedure in the same commit.</br> | ||
<code>git mv</code> | <code>git add .</code></br> = stage all changes in the current repository (recursively) for a commit | ||
<code>git rm</code> | <code>git add <FILE></code></br> = stage changes in <FILE> for a commit | ||
<code>git commit -m "< | <code>git mv</code> = move or rename a file. '''WARNING!''' Never use the <code>mv</code> command in a git repository. Always use <code>git mv</code>, otherwise the filepath change will not be tracked</br> | ||
<code>git restore <FILE></code> | <code>git rm <FILE></code> = delete <FILE>. '''WARNING!''' Never use the <code>rm</code> command in a git repository. Always use <code>git rm</code>, otherwise the deletion will not be tracked</br> | ||
<code>git commit -m "<MESSAGE>"</code> = commit staged changes to the local repository history. <code><MESSAGE></code> is the commit message. '''WARNING!''' Refer to the [[#Commit message convention]] for how to properly format commit messages.</br> Always use future tense, as if you are intending on doing it, i.e. "Add parameter", "Test code", "Fix typos". Be clear and concise!</br> | |||
<code>git restore <FILE></code> = remove unstaged changes from file, i.e. revert file to last commit</br> | |||
==== Recent changes to code ==== | ==== Recent changes to code ==== | ||
<code>git status</code> | The following are git commands that provide details of recent changes between the current tree and prior commits.</br> | ||
<code>git diff</code> | <code>git status</code> = show all unstaged and staged changes to working tree)</br> | ||
<code>git diff <FILE></code> | <code>git diff</code> = show unstaged changes to working tree.</br> | ||
<code>git diff <FILE></code> = show unstaged changes of <FILE>.</br> | |||
==== Connecting with remote repository ==== | ==== Connecting with remote repository ==== | ||
<code>git push</code> | The following are git commands that provide means of connecting with a remote repository to share and distribute changes.</br> | ||
<code>git pull</code> | <code>git push</code> = push all local commits to remote repository, warning, on first instance, won't work, but error tells you exactly what to do, set upstream</br> | ||
<code>git fetch</code> | <code>git pull</code> = pull all remote commits to local repository.</br> | ||
<code>git fetch</code> = '''unsure''', like pull, but just stages remote changes for local commit.</br> | |||
==== Managing worktree branches ==== | ==== Managing worktree branches ==== | ||
<code>git branch</code> | The following are git commands that provide more elaborate, but vital, capabilities to work tree handling and roaming.</br> | ||
<code>git branch < | <code>git tag</code> = list known tags.</br> | ||
<code>git switch < | <code>git tag <TAG></code> = create tag <TAG> reference to current version of code (for easy future referencing back to, such as <code>1.0.0</code> release.</br> | ||
<code>git branch -d < | <code>git branch</code> = list local branches.</br> | ||
<code>git checkout < | <code>git branch <BRANCH></code> = create a new branch from current called < BRANCH >.</br> | ||
<code>git checkout tags/< | <code>git switch <BRANCH></code> = switch to existing branch <BRANCH>.</br> | ||
<code>git checkout -B < | <code>git branch -d <BRANCH></code> = delete local branch <BRANCH>.</br> | ||
<code>git checkout <BRANCH></code> = create new branch and move to it.</br> | |||
<code>git checkout tags/<TAG></code> = change files to version found in tag <TAG>, for accessing older versions of the code that are useful to access.</br> | |||
<code>git checkout -B <BRANCH></code> = 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.</br> | |||
Revision as of 12:27, 22 September 2023
This page should provide a guide that should help you to set up and use git history/version control for developing software.
WARNING! Ensure that you are using git/2.37.3 or later! In terminal, run module load git/2.37.3
WARNING! Whenever initialising a git, ensure it sets main as the default branch, not master.
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
In the new local repository directory, run emacs .git/config. Ensure that it contains the following lines
[remote "origin"]
url = git@git.exeter.ac.uk:hepplestone/<CODENAME>.git
fetch = +refs/heads/*:refs/remotes/origin/*
The [remote] and url lines are the important ones (the fetch line can likely be ignored as it should be handled by default when you first set an upstream/remote repository). Ensure that the url line is set up to reference a git ssh address, not a http web address. If it references an ssh address, it will mean all pushes and pulls are performed via ssh, and not http access (which is just more annoying and cumbersome).
Git commands
This section provides a brief description of the key git commands (and their extensions) that have been found useful so far. NOTE: git merge has not been detailed here, it is often best to handle this on the GitLab website instead, and is a more difficult thing to handle, but should be learned at some point.
Help
The following are git commands that provide help and guidance for how to use git in terminal.
git = provide a brief help
git <TAG> -h = provide a brief help with <TAG>
git <TAG> --help = provide an indepth help with <TAG>, like the man command
Initial git commands
The following are git commands that are only used when initialising/setting up a git repository.
git clone <ADDRESS> = clone a remote repository into a new directory
git init = create an empty git repository, or reinitialises an existing one. PLEASE CONFIRM If I am right, needs to be done in the directory you want as the repository)
Version history
The following are git commands that allow one to stage and commit code changes. As convention, it is good practise to regularly perform commits, to keep track of changes. Each set of changes should be their own commits, e.g. do not stage bug fixes and a new procedure in the same commit.
git add .
= stage all changes in the current repository (recursively) for a commit
git add <FILE>
= stage changes in <FILE> for a commit
git mv = move or rename a file. WARNING! Never use the mv command in a git repository. Always use git mv, otherwise the filepath change will not be tracked
git rm <FILE> = delete <FILE>. WARNING! Never use the rm command in a git repository. Always use git rm, otherwise the deletion will not be tracked
git commit -m "<MESSAGE>" = commit staged changes to the local repository history. <MESSAGE> is the commit message. WARNING! Refer to the #Commit message convention for how to properly format commit messages.
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 changes from file, i.e. revert file to last commit
Recent changes to code
The following are git commands that provide details of recent changes between the current tree and prior commits.
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
The following are git commands that provide means of connecting with a remote repository to share and distribute changes.
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
The following are git commands that provide more elaborate, but vital, capabilities to work tree handling and roaming.
git tag = list known tags.
git tag <TAG> = create tag <TAG> reference to current version of code (for easy future referencing back to, such as 1.0.0 release.
git branch = list local branches.
git branch <BRANCH> = create a new branch from current called < BRANCH >.
git switch <BRANCH> = switch to existing branch <BRANCH>.
git branch -d <BRANCH> = delete local branch <BRANCH>.
git checkout <BRANCH> = create new branch and move to it.
git checkout tags/<TAG> = change files to version found in tag <TAG>, for accessing older versions of the code that are useful to access.
git checkout -B <BRANCH> = 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.