Introduction to Git –- Fall 2023
We will use Git from the command line in this course. This is normally how you will use it on the various HPC centers, and this way it will also be easier to understand what is going on while you are learning to use Git. On Windows, this means you will be using Git Bash.
Graphical tools exists for Git, see below list for a few. All entries on the list are free and unless otherwise mentioned, available for Windows, macOS, and Linux:
git init
git config
(local, global, system). More info in a moment.git add
git commit
git log
that all looks well.When this is done, you will clone the course materials.
NOTE: if you have a problem getting this to work on your own computer, and you have an account at Rackham, then you can use that.
We have some documentation for you:
https://hackmd.io/@git-fall-2023/LB-rackham.
You may also use any other HPC system you have an account at, of course. The above documentation would need only minor adjustements.
If you have installed XCode (or its Command Line Tools), Git may already be installed. To find out, open a terminal and enter git --version
.
If Git is not installed, you have several installation options. Apple maintains their own fork of Git, but it is usually a few versions behind, so we do not recommend installing that.
brew install git
Installing Git on Linux depends on which distro you are running.
sudo apt-get install git
(Ubuntu, Debian)sudo dnf install git
(RHEL, CentOS)First check that you have git installed (in a terminal or in Git Bash):
$ git --version
Now configure git with
git config (local, global, system)
You should at least set your global name and email (just once):
$ git config --global user.name "Your Name"
$ git config --global user.email "yourname@example.com"
Setting the editor (once) is also a good idea:
$ git config --global core.editor <editor>
Choices for editor could be (on Linux, though can be installed together with Git for other OS):
You should be able to use notepad on Windows by setting:
git config --global core.editor "<path-to>/notepad++.exe"
Another option could be to install VS Code and do this config instead:
git config --global core.editor "code --wait"
GitHub has some documentation on choosing and setting editors for various OS:
https://docs.github.com/en/get-started/getting-started-with-git/associating-text-editors-with-git
See more about configuring and using editors with Git at the end of this document.
Create an example folder and change to that, then create a file test.txt. On Linux you would do this:
$ mkdir <mydir>
$ cd <mydir>
$ touch test.txt
Now initialize a repository and stage the new file:
$ git init
Initialized empty Git repository in /home/bbrydsoe/test-git/.git/
$ git add test.txt
Now commit the change. The editor which you configured earlier should open. Add an example commit message:
$ git commit test.txt
[master (root-commit) ff8b6f6] Test of git
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 test.txt
Now let us look at the log:
$ git log
commit ff8b6f699d98c72d5cffc64d65a1c618b976b45a (HEAD -> master)
Author: Birgitte Brydsö <bbrydsoe@cs.umu.se>
Date: Thu Sep 17 13:53:59 2020 +0200
Test of git
When you do git log
, you should see something like the above, but with name, email, and commit message different. If that is the case, your Git should be configured correctly.
For the individual hands-on part of the course, we have created some course materials which you will download from the course GitHub: https://github.com/hpc2n/course-intro-git (normally you click the green "Code" button to get the link to clone or download)
git clone https://github.com/hpc2n/course-intro-git.git
There are several web based Git repository. Some of the more popular ones are:
We are going to use GitHub for the part of the hands-on where you will be working together in groups.
Please go to
and sign up for an account. You will need to setup 2FA also.
This part will be done before the section "Working with remotes" on day 4, but you can create and add your SSH key to GitHub now if you want to.
$ ssh-keygen -t ed25519 -C "GitHub"
b. If you have an older system, this may work better$ ssh-keygen -t rsa -b 4096 -C "GitHub"
.ssh/id_rsa
for the legacy system):$ eval "$(ssh-agent -s)"
$ ssh-add ~/.ssh/id_ed25519
.ssh
folder, open the file id_ed25519.pub
with some editor and copy it (id_rsa
for legacy systems). Do NOT add any newlines or whitespace!This part will be done during the exercises on day 5, but you can create and add your SSH key to GitHub now if you want to.
$ ssh-keygen -t ed25519 -C "GitHub"
b. If you have an older system, this may work better$ ssh-keygen -t rsa -b 4096 -C "GitHub"
.ssh/id_rsa
for the legacy system):$ eval "$(ssh-agent -s)"
$ ssh-add ~/.ssh/id_ed25519
.ssh
folder, with some editor, open the file id_ed25519.pub
(or id_rsa.pub
for the legacy systems) and copy it. Do NOT add any newlines or whitespace!$ ssh -T git@github.com
$ ssh -T git@github.com
The authenticity of host 'github.com (140.82.121.3)' can't be established.
ECDSA key fingerprint is SHA256:p2QAMXNIC1TJYWeIOttrVc98/R1BUFWu3/LiyKgUfQM.
ECDSA key fingerprint is MD5:7b:99:81:1e:4c:91:a5:0d:5a:2e:2e:80:13:3f:24:ca.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,140.82.121.3' (ECDSA) to the list of known hosts.
Hi bbrydsoe! You've successfully authenticated, but GitHub does not provide shell access.
Vim
sudo apt-get install vim
)vim <filename>
to open a file for editing. The file will be created if it does not exist before.i
to enter 'insert' mode to be able to write in the editor.ESC
to go to 'command' mode and then :wq
to save and exit the editor.dd
will delete the whole line your cursor is on.Nano
sudo apt-get install nano
)nano <filename>
to open a file for editing. The file will be created if it does not exist before.git config --global core.editor notepad
git config --global core.editor "<path-to>\notepad++.exe"
git config --global core-editor "C:\Program Files (x86)\Notepad++\notepad++.exe"
git config --global core.editor vim
GitHub has a page for setting some editors for various OS'es: https://docs.github.com/en/get-started/getting-started-with-git/associating-text-editors-with-git
GitHub has a page for setting some editors for various OS'es:
https://docs.github.com/en/get-started/getting-started-with-git/associating-text-editors-with-git
GitHub also has a command line interface that you can use if you want to.
It is available for Windows, macOS, and Linux.
You can use it if you prefer to do your workflow through a terminal, and you can call the GitHub API to script various actions as well as set a custom alias for any command.
More information and download here: https://github.blog/2020-09-17-github-cli-1-0-is-now-available/