+++
title = "HowTo Git" 
date = 2024-07-19
+++

## WhatIs Git?

Git is a version control system. It is a program that allows you to roll back changes in files to point where they were previously saved.    
It also helps multiple people work on a project. Thus it can combine changes made by several developers.

Repository (or repo) - directory with all source code and other necessary files for program to work + it contains Git metadata which allows to roll back files.

There are services that provide access to git repositories, the most popular is [GitHub](https://github.com/).


##  HowTo create repository?

### How to create remote repository

0. __Register__ on Github 

1. Create a Git __repository__
	![](./images/10.png)
	![](./images/11.png)
2. Choose a __license__
	- __"MIT License"__ if you don't know what to choose
	![MIT](./images/21.png)
	- __"GNU GPLv3"__ if you want your project to be free software.
	![GNU GPLv3](./images/22.png)
<!-- TODO licenses -->


### How to create local repository

1. Run `git init` in directory with code


## HowTo download repository?



1. Clone (download) it with `git clone REPO_LINK`  

`git clone https://github.com/oneshotws/hackerProg`

First time you will get SSH prompt, answer `yes`.

```bash
~ ➤ git clone https://github.com/oneshotws/hackerProg.git
Cloning into 'hackerProg'...
The authenticity of host 'github.com (140.82.121.4)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
```

### Link types 

Git allows to clone repositories via few HTTPS and SSH link   
HTTPS - `https://github.com/oneshotws/hackerProg.git` (.git in end isn't necessary)  
SSH - `git@github.com:oneshotws/hackerProg.git`

You can see them by clicking on "Code": ![](./images/31.png)

In case of SSH link you need to be authentificated to be able to use them. Otherwise you will get following error:
```bash
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
```  


## HowTo authentificate?

To be able to upload changes to repository - we need to be authorized and authentificated for GitHub via our console.

1. Tell who you are (authentificate)  
Run following commands: 
```bash
git config --global user.email "YOUR_EMAIL@EXAMPLE.COM"
git config --global user.name  "YOUR VISIBLE NAME"
```

2. Proof who you are (authorize)  
For that we need to associate SSH key with our account.
-  __Generate__ an SSH key (if you don't have one!) - `ssh-keygen` and press enter 3 times:
```bash
~ ➤ ssh-keygen
Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/casual/.ssh/id_ed25519):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/casual/.ssh/id_ed25519
Your public key has been saved in /home/casual/.ssh/id_ed25519.pub
The key fingerprint is:
SHA256:ut0/9NgtyQEmOozmVOUBFbnfAgScz9IkwPCKalOCff0 casual@Casual-PC
The key's randomart image is:
+--[ED25519 256]--+
|    .o.oo=oo     |
|     .. + *      |
|      .  X o     |
|.. . o  o O o    |
|..o.o .+So = o   |
| .o.  +o+   + o  |
|.o   +. E. . * + |
|. .   .o .  o * .|
|      . . .... . |
+----[SHA256]-----+

```
- Output the public (that ends with `.pub`) SSH key with `cat` and copy it. 
``` 
~ ➤ cat ~/.ssh/id_ed25519.pub
ssh-ed25519 AAAAC3NzaC1lZDI1HEX5AAAAIIMaB2mluyXjROHI8GJ2o9xfvj+uiol/GbPnwJDzZkFm username@hostname
``` 

- Add key to GitHub
Navigate to Settings -> SSH and GPG keys -> [New SSH key](https://github.com/settings/ssh/new)  
Paste your public key:
![](./images/35.png)




## HowTo upload changes to your repo?

0. [Authentificate](./#howto-authentificate)
	
1. __Develop__ your program.  
Once you make the changes you want, upload them to the repository using:  
`git add . && git commit -am 'new feature' && git push`.  
	- `git add .` - adds all new files to the repository, so git will start track their changes (files in repository folder aren't tracked, we even can make git ignore specific files so files with our secrets won't be public)  
	- `git commit -am 'new feature description'` - saves (commits) changes to the repository that you can rollback to later.   
	- `git push` - uploads changes to GitHub.
	
	After that, our local changes will be displayed on github.  






### Advanced

Git can be used in more advanced usages, like: 
 - get bug reports (via Issues tab)
 - get notifications for new bug reports, releases...
 - upload changes to someone's else repository
 - allow others to make changes to your repository
 - review and merge their changes
 - fork repository
 - wiki, web pages, automatization, webhooks, etc...

Also you need to keep in mind that your repository isn't private by default and if you write there private information / passwords / keys - advisory __will__ use it to harm you.  

And even if you will make repo private - I wouldn't trust Microsoft (owner of GitHub) to keep my secrets. So I would host my own private Git service.


---

[Other typical Git problems.](https://ohshitgit.com/)


<!-- Btw, it's experemental post. Do you like this approach that we deal with problem as we are doing it first time together? Or do you prefer more easier to read - step by step (HowTo) guide? -->

{{< source >}}
My PHD2 presentation
{{< /source >}}

