# GitHub Cheat Sheet
## Getting Started
### 1. To check if you already have git installed in your device:
In terminal, type:
:::info
git
:::
if you get help options, that means git is installed.
### 2. To install git:
#### For Mac
:::info
brew install git
:::
#### For Linux/Unix
:::info
sudo apt-get install git
:::
### 3. Configure your git
Customise your git by setting your name and email
:::success
git config --global user.name "FirstName LastName"
git config --global user.email: "yourlogin@youruni.ac.uk"
:::
You can also add your favourite editor (e.g., nano below)
:::success
git config --global core.editor "nano -w"
:::
You can also choose the line endings (below will set line endings to be "\n" )
:::success
git config --global core.autocrlf input
:::
Now check that all your configurations are correct
:::success
git config --list
:::
# Initialising your repository
In your terminal, go to the directory you want to save to git. Example shown below
:::info
cd PhD/code
:::
Now you need to initialise this directory,
:::info
git init
:::
if directory is initialised, then ls -a will show a hidden file named .git
# Adding & Commiting your files
!
On your local machine, for the files you want to send to git you need to:
1. First add them (also known as ``staging'' your files):
>For one file
:::info
git add galaxies.txt
:::
>For all files:
:::info
git add --all
:::
2. Second you need to commit the files:
:::info
git commit -m " added galaxy ids "
:::
# Remote Git Repository
## 1. Create an account on GitHub
https://github.com/
## 2. Generating an ssh-key
>**IMPORTANT**, you **need** to generate an **ssh key** to link the local and remote repositories!!
In your working directory, type:
:::info
ssh-keygen
:::
Then go to home directory, you do:
:::info
cd ~
ls -a
:::
The latter command should display a hidden directory name .ssh
Now go to the .ssh directory:
:::info
cd .ssh
:::
this should display two files:
:::warning
id_rsa
:::
:::warning
id_rsa.pub
:::
Open the id_rsa.pub file and copy its content, that is the key! To open file, use **cat command** to not miss any characters that may not be displayed when you open file with nano or vim, for example.
:::info
cat id_rsa.pub
:::
## 3. Adding the key to your remote Git
1- On your GitHub, go to settings by clicking on your avatar:

2- Then click on SSH and GPG keys:

3- Add a new key by pasting the content of id_rsa.pub below:

Now your local and remote gits are connected! woohoo 😃
## 4. Creating a new repository on GitHub
>To create a new repository, click on the "New" green box.

>Name your repository and set it to be either public/private:

This will provide you with few options to setup your repository.
>

> Choose the one that says ``**…or push an existing repository from the command line**''
>
Paste the command above on terminal in your working directory.
For **my case**, this would be:
:::info
git remote add origin git@github.com:blackhole-girl/My_Cool_Project.git
git branch -M main
git push -u origin main
:::
Now the galaxies.txt file I have added earlier will appear on the remote repository! All done! 🥳
## 5. Next time you push
Now that you already have your local and remote repositories connected, if you made edits to your files and you want them to appear on your remote repository, all you need to do is:
:::info
git add "galaxies.txt"
git commit -m "changed galaxy ids"
git push -u origin main
:::
Now the new version of galaxies.txt will appear in the remote repository!
**Always: add, commit, push** (in that order)
>git add: puts the file in the staging area
>git commit: updates local git of new changes/files
>git push: send these changes/files from local git to remote git
# Creating and pushing to a new branch
Your main branch should be called "main". If you are collaborating on a project and you can create a branch for each person in the collaboration. Once everyone is happy with the results, you can then merge your branch to appear on the "main" branch.
## 1. To create a new branch:
1. Login on GitHub.com
2. Go to the repository you want to creat an new branch for.
3. Near main, you should be able to see "branches" (as shown below) - click on it.

4. This will prompt a list of your branches, and also gives you the option to delete them, or add new branches. To create a new branch click on the "new branch" buttom in green as shown below

5. Name your branch. I personally name it after the person who will be working on it. E.g. Houda for my own branch
## 2. Pushing to a new branch
1. On terminal, go to the directory that is connected to this repository. For me, this would be:
:::info
cd PhD/code
:::
3. Get the list of your branches in this directory by using the following command:
:::info
git branch -a
:::
The latter command should return:
:::warning
main
remotes/origin/main
:::
2. Switch to the new branch (my branch is named Houda)
:::info
git switch -c Houda
:::
3. Now add, commit your files as you normally do
:::info
git add "new_file.txt"
git commit -m "adding a new file"
:::
3. To push your files to the new branch **for the first time**:
:::info
git push --set-upstream origin Houda
:::
(where Houda is the name of my new branch)
You **only have to add** --set-upstream origin when you push for the **first time** to a new branch. After this a simple:
:::info
git pull origin main
:::
should work for your branch.
## 3. RESET your git commits:
Sometimes, it may happen that you want to un-stage your files (ie remove them from the staging area). In this case, use the following command:
:::info
git reset --soft HEAD~1
:::
To check the status of your git
:::info
git status
:::
This will show you all the changes that need to be commited
## 4. Merging:
TBC
# Cloning someone's repository
TBC