# Github
[](https://hackmd.io/Xbz63becQtuhEvUtyd_3yg)
First we have to add our remote repository, into a new directory we run these 2 commands, one to initiate git and then add our repo with **origin** as its name.
```
git init
git remote add origin git@github:<name of the repo>
```
Check your repositories with this command
```
git remote -v
```
Now we check if we have the permissions to push
```
git push -u origin main
```
If we get this error we have to add our **ssh key** to our trusted keys in **Github**

First if we don't have one we'll have to create an **RSA key** associated with our email. To do this we can simply run this command
```
ssh-keygen -t rsa -b 4096 -C "eibanezpl@gmail.com"
```
The `-t` flag specifies with kind of key, in this case **rsa**, `-b` flag indicates que bits to the key **4096** being strong, `-C` to comment the mail associated with the key.
In your **Github Account** go to `Settings`

Head to `SSH and GPG Keys`

Click `New SSH Key`

And now you have to paste the content of your `id_rsa.pub` key. you can do so by running this command unless you changed it's default name.
:warning: Never share your `PRIVATE key` (`id_rsa` or any kind of name you gave it) only use your `public key` (with the `.pub` extension) :warning:
```
cat ~/.ssh/id_rsa.pub
```
Copy the contents right here, give it a title and hit `Add SSH Key`

After the **ssh key** has been added now we can start working with our repository
First `fetch` the remote
```
git fetch <ssh of the repo>
```
Now we have to create some content like a simple fingerprint check command:
```
echo "ssh-keygen -lf ~/.ssh/id_rsa.pub" > fingerprint-ssh.sh
```
With our new bash file we have to `add` it to the `commit`, comment it and `push` it
```
git add fingerprint-ssh.sh
git commit -m "fingerprint check bash"
git push -u origin main
```
We could simply `push` every file we have created with a `.` after the `add`, but we're choosing to only add our **bash file**, moreover we could `commit` without the `-m` flag however it's highly frowned upon to commit without **commenting** about the changes. We're pushing with the `-u` flag to the `origin` repo being the name we gave it in the beginning (instead of the long ssh link) to the `main branch`, if we're working in a team we have to learn about `forking` before committing such changes.
We can also delete the files locally using the `rm` command, if we want to remove it from the repo we need to use this one instead
```
# For a single file
git rm <file>
# To delete a whole directory
git rm -r path/to/directory
```
Now if we're working in a team and somebody `pushed` another commit we could simply `pull` the new files
```
git pull
git log
```

By modifying the new files we can simulate our workflow.
```
echo 'echo "collaborating to the project"' >> test.sh && cat test.sh
```
It'll display we have added a new line to our `test.sh`, we can check it's status running the following command
```
git status
```

We'll add the new changes using the flag `-u` to the `add` command
```
git add -u
git status
```

It's time to `commit` these changes and `push`, it'll push to the **working repository** on so it'll automatically set the `-u origin main` to it
```
git commit -m "Another line for the test"
git push
git log
```
We can confirm these changes happenned with the `log` command

If we mess up and want to clone our repository once again we're going to use the `clone` command
```
git clone <ssh or https-link> <name>
git log
```
Now we can keep working on our repo, if we want to "turn back in time" after every `commit` we can see an `commit-hash`:

If we use the `checkout` command we can add the `commit-hash` from the `commit`
```
git checkout 88bf60a9253cc716c7fee9754e801ca54a222cbc
```


In this commit we don't see the `clone-pull` change

With the `git branch` command we can see that we're in a different branch (detached)

We're branching off `main` while in `checkout` with this command
```
git switch -c <branch-name>
```

We can also create a branch directly or start a branch from another commit using these commands
```
# Creates a branch named staging
git branch staging
# Creates a branch from an existing commit
git branch <commit-hash>
# Create a branch linked to another one, in this case it's a local one called new/feature from origin's new/feature
git branch --track new/feature origin/new/feature
# Switches to an existing branch
git switch <branch-name>
# Rename a branch we're working on
git branch -m <new-branch-name>
# Rename another branch
git branch -m <other-branch> <new-branch-name>
```
You can always check your `commit-hash` from the last commits published

Now we can add a new file to work on
```
echo "df -H" > disk-check.sh && chmod +x disk-check.sh
```
Let's `add` to see what happens
```
git add .
git status
```

We can see that the file `test.sh` from the main branch is no longer here and we have a new file `disk-check.sh`
```
git commit -m "Uploading a disk-check"
```

Pushing now into our repo `scripts` with the new branch `no-clone`
```
git push scripts no-clone
```

We have successfully created, worked and published a new `branch`

Now we can merge the `branch` into `main`, we can specify a merge message or simply close the prompt
```
git switch main
git merge no-clone
git log
```

Finally we push the changes to `main`
```
git push -u scripts main
```


If we're done with the `branch` we can delete it, you have to be in **another branch**, you cannot delete the one you're currently on
```
git switch main
git branch -d no-clone
git push scripts --delete no-clone
```


We have successfully `merged` and `deleted` the leftover `branch`
