---
# System prepended metadata

title: Github Tutorial

---

# Github Tutorial
:spiral_note_pad: https://hackmd.io/@sysprog/gnu-linux-dev/https%3A%2F%2Fhackmd.io%2F%40sysprog%2Fgit-with-github

### SSH key 產生連接 Github

https://hackmd.io/@sysprog/gnu-linux-dev/https%3A%2F%2Fhackmd.io%2F%40sysprog%2Fgit-with-github?fbclid=IwAR2dMiRtKmfpKHP9SwDTVdeaefefgV6f-wABjjh-VwNwwyeHsRvAoNlGey4#SSH-key-%E7%94%A2%E7%94%9F%E7%9A%84%E6%96%B9%E6%B3%95

教學網站
https://www.rs-online.com/designspark/github-nvidia-cn
### Create new folder and initialize
```
$ mkdir test_1109
$ cd test_1109
$ git init
```
### Git Clone
```
$ git clone {https://github.com/...}
```
### Create a file and git status
> 1. create a file
> 2. git add {file}
> 3. git status 
> 4. git commit -m "file"
> 5. git push 
```
$ vim test1.cpp   #create a file
```
![](https://i.imgur.com/LdPqxNG.png)

error to get git status -> need to type "git add" first

```
$ git add {file_name}
$ git add --all
$ git add -A
```

after "git add" -> file is tracked 
```
$ git status
```
![](https://i.imgur.com/pdLhaYk.png)

```
$ git commit -m “add new file”
```
:exclamation: If it is the first time to git the file, then you should login first
```
$ git config --global user.email "you@example.com"
$ git config --global user.name "Your Name"
```
### Git push to synchronous
在自己的github上打開空的 repository，Github 會指示你如何將本機 repository 的 remote 設定為遠端的 repository 目錄，並輸入以下命令，即可連線並把目前的 repository 同步到 Github 上面了。

master:
:::info
$ git remote add yourpkg-name git@github.com:你的帳號名稱/你的專案名稱.git
$ git push -u your-pkg-name master
:::

main:
:::info
git branch -M main
git remote add origin git@github.com:你的帳號名稱/你的專案名稱.git
git push -u origin main
:::
### branch
> 1. git branch {branch_a}
> #create a new branch called branch_a
> 2. git checkout {branch_a}
> #switched to branch_a

* ####  All branches list & current branch
```
$ git branch
$ git branch -a
```
![](https://i.imgur.com/JLL0C4H.png)

* #### create and switch branch at the same time
```
$ git checkout -b <branch>
```
* #### git push in branch
```
$ git push --set-upstream origin branch_a  #first time
$ git push   #after that
```
* #### git merge
```
$ git checkout main  #switched to main
$ git merge branch_a
```

### git reset/ git revert
https://roykwokcode.medium.com/%E8%AE%93%E4%BD%A0%E7%9A%84%E4%BB%A3%E7%A2%BC%E5%9B%9E%E5%88%B0%E9%81%8E%E5%8E%BB-git-reset-%E8%88%87-git-revert-%E7%9A%84%E7%94%A8%E8%99%95-6ba4b7545690

## problem
### git clone -> can't git checkout and git reset
* git checkout need to be in repository
* 