Difference between revisions of "Git repository management"
(Created page with "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...") |
|||
| Line 43: | Line 43: | ||
ENSURE YOU ARE USING git/2.37.3 OR LATER! Whenever initialising a git, ensure it sets main as the default branch, not master. | ENSURE YOU ARE USING git/2.37.3 OR LATER! Whenever initialising a git, ensure it sets main as the default branch, not master. | ||
== | == 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 repositry | |||
git init | |||
== Git commands == | |||
git (get a brief git help) | git (get a brief git help) | ||
Revision as of 10:45, 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.
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). The website is: git.exeter.ac.uk
Go to the website and log in using your uni credentials. Then, inform one of the Hepplestone research group Maintainers (or higher) to invite you as a member to the git.exeter.ac.uk/groups/hepplestone group (left sidebar on the group, Manage -> Members, top right of page "Invite members"). Developer gives you access to
Once you have been invited, you will be able to access (Developer level should allow you to make new projects) existing projects within the group. If you are a Developer, you will likely only be able to clone repositories, make branches, commit to branches, and set up merge requests (you will not be able to commit directly to the 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:
1. go to git.exeter.ac.uk, go to the left sidebar and click on your avatar (top right of sidebar), go to Preferences. 2. In Preferences, go to the left sidebar and select "SSH Keys". 3. Click on "Add new key" on the right-hand side of the page. 4. 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. 4.1. In terminal, run
ssh-keygen -t rsa -b 2048 -C "<GitLab>"
<GitLab> is a comment associated with the key. You can use whatever you want for that. Press Enter. You will then be prompted to provide a filepath/filename for your new id key (with RSA encryption). If you press Enter again, it will choose the default (in the parentheses). If you provide just a filename, it will put it in the specified directory (/home/links/<USER>/.ssh/). So, you could provide "id_rsa_gitlab" to have a separate ssh key for GitLab. Whatever your choice, enter it and press Enter.
You will then be prompted for a passphrase (to be entered in the future the first time your session is connecting to GitLab, so effectively once every computer restart). It needs to then be reentered to confirm.
Now, go to where you saved your public id to (e.g. /home/links/<USER>/.ssh/). In there, you will find a public version of your ssh key, e.g. id_rsa.pub (or, <FILENAME>.pub, where <FILENAME> is the filename you entered when prompted earlier). Copy its contents to the GitLab webpage into the "Key" input field. Then give it a title to remind you of what it is, it might provide a default. 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.
ENSURE YOU ARE USING git/2.37.3 OR LATER! Whenever initialising a git, ensure it sets main as the default branch, not master.
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 repositry git init
Git commands
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)
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)
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)
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>)
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)
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).