Lecture Notes 6#

ssh keys#

  • Generate a new pair of sshkeys with

$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/python-lll/.ssh/id_rsa): 
Created directory '/home/python-lll/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/python-lll/.ssh/id_rsa
Your public key has been saved in /home/python-lll/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:AkLdasm36JyaLixeOrT0maAAy5wvkF4Hf6esGsLNGtw python-lll@bat
The key's randomart image is:
+---[RSA 3072]----+
|  .. .           |
| .  . .          |
|  ...o           |
|.  o=..          |
|+o..oo..S        |
|BB+..o.o .       |
|O=*E+.o o        |
|+***=  o         |
|o**o...          |
+----[SHA256]-----+

}

  • The hidden .ssh folder is where the keys are stored:

$ ls .ssh
id_rsa  id_rsa.pub
$ more .ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDv1DgQURdmseEd7QKx1+vh6KZoN6E/d/VVP5x5/4OL
r0xj3w3lMFMvTKgH43vZ+NNTidT8TcLrJLMwq/tLUmxnLd/k13kK12qdBU/1ovZnwqO0ELAiz08kGBVy
Yw5POCPJVi5y5gfoU4bcmptsrNmWcUk/IFj6VzG+kh5YptqwPqpOYAnZ6ECK9yfmlurSExdSvotiPQAS
E1tH2ZnQuT443K+EZhOE2kMuNhhk0bVReIF83AtMfIrhyCyp4nieJmdfIxo28zfizqGOy4MnIfeRU43W
HFHQDvV00GhCcUIbF6nbgxc/8BiDX8GvqJnZLK4uKVMZz2w56Z8Lm0VXvyhI1yC8rWqNqXdGyTEH+ut3
jpwJ4slamkCFsUE2fJU0m2LrXBCYn9zLXNZaUrJjGno+M/TQi1JsvFgoTvCzo7odEF20xAXaazFs2NKY
KL2bbNAJUL7SyamS8SzU4VxoIDss105Zeu1ZmddQRRVVTt45uryS/F1SsA/bfDcLrQLN+rs= python-
lll@bat

Setting up your github account#

From profile menu select Settings

foo

In the sidebar select SSH and GPG Keys

bar

Add new key

baz

Copy-paste the public key (content of id_rsa.pub) to the Key field

boo

Cloning the repository#

Now we can clone the repository from KTH Github

$ git clone git@gits-15.sys.kth.se:BB1000/git-2023-04-19.git
Cloning into 'git-2023-04-19'...

The authenticity of host 'gits-15.sys.kth.se (2001:6b0:1:1300:250:56ff:fe01:109)' can't be established.
ECDSA key fingerprint is SHA256:OmuyB64vnycQufBPs7zDXyHYpCi89jTrHru802Q+4/E.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'gits-15.sys.kth.se' (ECDSA) to the list of known hosts.
warning: You appear to have cloned an empty repository.

that creates a new folder with the same name

$ cd git-2023-04-19

Create an initial file booklist.txt#

$ ls
booklist.txt
$ cat booklist.txt 
Harry Potter, J.K. Rowling

What is the status according to git?#


$ git status
On branch main

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	booklist.txt

nothing added to commit but untracked files present (use "git add" to track)

Tell git to track booklist.txt#

$ git add booklist.txt 
$ git status
On branch main

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
	new file:   booklist.txt

Commit the new file to history#

$ git commit -m "Starting a booklist"
[main (root-commit) 803036a] Starting a booklist
 1 file changed, 1 insertion(+)
 create mode 100644 booklist.txt
$ git log
commit 803036a8d643fc482189a28542bac64917130574 (HEAD -> main)
Author: Olav Vahtras 
Date:   Tue Apr 18 16:26:26 2023 +0200

    Starting a booklist
    

Get information of the remote repositories#

$ git remote
origin
$ git remote -v
origin	git@gits-15.sys.kth.se:BB1000/git-2023-04-19.git (fetch)
origin	git@gits-15.sys.kth.se:BB1000/git-2023-04-19.git (push)

Backup the new changes to Github#

git push takes two arguments, the name associated with the repository and the branch name involved. Here the changes in the local branch main will be copied to the remote’s branch with the same name

$ git push  origin main
Enumerating objects: 3, done.

Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 247 bytes | 247.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To gits-15.sys.kth.se:BB1000/git-2023-04-19.git
 * [new branch]      main -> main

Note that there is a new branch origin/main associated with the remote repository (KTH Github)

$ git log
commit 803036a8d643fc482189a28542bac64917130574 (HEAD -> main, origin/main)
Author: Olav Vahtras 
Date:   Tue Apr 18 16:26:26 2023 +0200

    Starting a booklist

Add a second book#

Assuming work on another computer: Editing the file booklist.txt


$ git add booklist.txt 

$ git commit -m 'Add second book'
[main 1df3614] Add second book
 1 file changed, 1 insertion(+)

Backup to remote repository#

When there is only one defined remote and one branch git push without arguments will try to use those

$ git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 299 bytes | 299.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To gits-15.sys.kth.se:BB1000/git-2023-04-19
   803036a..1df3614  main -> main

Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean

$ git log --oneline --all
1df3614 (HEAD -> main, origin/main, origin/HEAD) Add second book
803036a Starting a booklist

Include new changes in the remote#

E.g. another collaborator has contributed or you have saved work from another computer

  • On the first computer the git log shows old information - the local repository has not been synchronized with latest changes on Github

$ git log --oneline --all
803036a (HEAD -> main, origin/main) Starting a booklist
(bb1000) python-lll@bat:~/git-2023-04-19
  • Use git fetch to update local computer with knowledge about remote state

$ git fetch origin
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 279 bytes | 279.00 KiB/s, done.
From gits-15.sys.kth.se:BB1000/git-2023-04-19
   803036a..1df3614  main       -> origin/main
$ git log --oneline --all
1df3614 (origin/main) Add second book
803036a (HEAD -> main) Starting a booklist

The remote branch can be merged just as with local development branches

$ git merge origin/main
Updating 803036a..1df3614
Fast-forward
 booklist.txt | 1 +
 1 file changed, 1 insertion(+)
$ git log --oneline --all
1df3614 (HEAD -> main, origin/main) Add second book
803036a Starting a booklist

Fetch/merge vs pull#

  • remote branches are branches that reflect the state of remote repositories

  • git fetch updates local information on the state of the remotes

  • git merge can merge remote branches in the same way as local development branches

  • git pull combines git fetch and git merge to a single command

After a new change to booklist.txt in the web interface

$ git pull
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 346 bytes | 173.00 KiB/s, done.
From gits-15.sys.kth.se:BB1000/git-2023-04-19
   1df3614..a167720  main       -> origin/main
Updating 1df3614..a167720
Fast-forward
 booklist.txt | 1 +
 1 file changed, 1 insertion(+)
$ git log --oneline --all
a167720 (HEAD -> main, origin/main) Add a third book
1df3614 Add second book
803036a Starting a booklist