# Git
###### tags: `tutorial` `electrical_system` `NTURT`
## Reference
[30 天精通 Git 版本控管](https://github.com/doggy8088/Learn-Git-in-30-days/blob/master/zh-tw/README.md)
[Git 與 Github 版本控制基本指令與操作入門教學](https://blog.techbridge.cc/2018/01/17/learning-programming-and-coding-with-python-git-and-github-tutorial/)
[Git-Tutorials 基本使用教學](https://github.com/twtrubiks/Git-Tutorials)
[[Git教學] 寫給 Git 初學者的入門 4 步驟](https://www.maxlist.xyz/2018/11/02/git_tutorial/)
[ref1]:https://github.com/twtrubiks/Git-Tutorials
## Advise
Use command ```--help``` frequently, it helps a lot.
Most of your queries can be answered with these document
## Pre-request
+ Know what **git** is.
+ Download **git** on your device.
+ Have an account on [**GitHub**](https://github.com/)
:warning: Assume your using Linux system, but should quite same on other platforms.
----------
## Basic Manipulation
### Check Your Git Version
```bash
$ git --version
```
:point_down: Version Information
```bash
git version 2.25.1
```
### Set Your Git User Name and Email Address
```bash
$ git config --global "your name"
$ git config --global "your email"
```
> ```--global``` means "use global config file"
>> check out for further usage with command ```git config``` to show hint.
:point_right: try command below to ensure your configurations
```bash
$ git config --list
```
:point_down:
```bash
user.name= [your_username]
user.email= [your_email@email.com]
```
### Create your ```.git``` Menu on your Workspace
Create a ```.git``` folder on current workspace. (We create a folder ```git_test1``` first as our workspace)
```bash
$ mkdir git_test1; cd git_test1;
$ git init
```
> Create an empty Git repository or reinitialize an existing one.
Or you can assign the directory you want to initialize.
```bash
$ git init <directory>
```
Set Git config.
```bash
$ git config --global user.name "Name"
$ git config --global user.email "user@gmail.com”
```
### Create Index with ```git add```
Assume we already have ```test1.py``` and ```test2.py``` in my workspace. To add index for my files
```bash
$ git add test1.py
```
> Or you can add multiple files in one line command
If you want to add all the files in the folder, try
```bash
$ git add .
```
> In Linux, wildcard ```*``` also works for ```.```
### Git ```commit``` to Local Repository
Now we have add these files to the index (which means we want to save/ update these files). We want to really keep these files up to date. Therefore, we need to ```commit``` it.
```bash
$ git commit -m "Your_message"
```
> ```-m``` means **message**
:warning::exclamation: ***message should not be left empty*** or the commit would fail. (How could you commit a change without describing it!!)
:memo: Every time you want to commit, you should have the files ```add``` first.
### Look into the Changes
To see the Git commit record on current working directory,
```bash
$ git log
$ git reflog
```
> Show the detailed content
> > Show the commit content (message)
### Delete/ Restore Files from Repository
+ To remove a file from current workspace and git repository
```
rm [file]
git rm [file]
```
:point_right: Now your action has been sent to index, just commit to apply your changes. (```git commit -m [message]```)
+ If you accidentally deleted a file, we can use git to restore it.
```bash
rm [file] # we say you accidentally deleted this file
git checkout -- [file]
```
----------
## Branch
----------
## Create GitHub Repository
So, what can we do on the renowed website **GitHub**?
### Create Your Own Repository
:point_right:Just type in your Repository name and create. I name it as ```test_repo``` here.

:point_right:If you create your repository successfully, you should see the page bellow

### ```push``` Your Project on GitHub
Now we have a local repository created earlier, it's time to connect it with the repository on **GitHub**.
We take the commands in the middle.
:point_right: The first line names the repo name for your connecting url.
```
$ git remote add origin [your_repo_url]
```
> Once you had add the remote repository with its url, you don't have to do this again next time.
>
> You can change ```origin``` as you want. This indicates the git manage platform repository your connecting to.
>> If your connecting to a [**STM32**](https://github.com/NTUracing/STM_Library.git) repo, it might be a option to name it as ```stm32_lib``` or somewhat you like.
>>
>> example ```git remote add stm32_lib https://github.com/NTUracing/STM_Library.git```
:point_right: Then we create a new branch (will be introduced later), and ```push``` the current repository on GitHub.
```bash
$ git branch -M main
$ git push -u origin main
```
> ```-M``` means **new branch**
>> ```main``` is the branch name
>>> ```-u``` for "set-upstream"
Git should prompt message as below :point_down:
```bash
Username for 'https://github.com': [type in your username]
Password for 'https://[username]@github.com': [type in your password]
```
Now the files on your local repository should be loaded on GitHub.
## Clone the Repository on GitHub
[REF](https://reactjs.org/docs/how-to-contribute.html)
:exclamation: Read ```Read.md``` carefully before you pull the repository and sure your usage is within the request of the source code
To clone the repository on GitHub
```bash
$ git clone [url]
```
We have a repo for [STM32_library](https://github.com/NTUracing/STM_Library.git) here.
## Advanced Manipulation
### Reset
## Appendix
### Some Tips on Cloning
[REF](ref1)
It will take quite a long time if the repository is fat for the logs are too long.
If we don't need so much history commit, we don't need to download too much log.
:point_right: Try ```--depth [num]``` command
```bash
git clone https://github.com/NTUracing/STM_Library.git --depth 5
```
Then you only load the most recent 5 logs instead of all of them
### How to Delete a Local Repository?
:point_right: Just delete the ```.git``` folder.
```bash
rm -r .git
```
:pencil: If you want to reinitialize the repository
```bash
git init
```
### How to Undo my ```add``` Command?
[REF](https://stackoverflow.com/questions/348170/how-do-i-undo-git-add-before-commit)
If you just added a file to the index pending, and you want to undo it, try this
```bash
git rm --cached [file_to_undo]
```
> Information for ```--cached``` : Use this option to unstage and remove paths only from the index.