---
# System prepended metadata

title: Git & Github Lecture

---

# Git & Github Lecture 

## Git start

* We have to set the personal configuration

> such as : 
> ```linux
> $git config --global user.name "yourname"
> ```
> ```
> $git config --global user.email "youremail"
> ```

* We can use the command line to check the config

```linux
$ git config --list
```

## First Commit
1. Choose the folder you will use Git to control.

2. Initial the Git
```linux
$ git init
```

3. Now you can create new file or others folders

4. Use instruction to check the status

```linux
$git status
```
5. Decide your action
   5-1. Add the file from **Working Directory** to **Staging Area**
   ```linux
   $ git add xxxxfile
   ```
   5-2. Add the file from **Staging Area** to **Repository** 
   ```linux
   $git commit -m "message"
   ```


## Use SSH key to connect Github

1. Login in to the Github
2. Click the personal setting button
3. Choose SSH keys table
4. Add new SSH keys

5. To check whether your SSH keys  being sucessed

```ubuntu
$ssh -T git@github.com
```
## Clone
referrence : [jserv熟悉Github](https://hackmd.io/@sysprog/gnu-linux-dev/https%3A%2F%2Fhackmd.io%2F%40sysprog%2Fgit-with-github#Git-%E6%95%99%E5%AD%B8%E5%BD%B1%E7%89%87-%E4%B8%AD%E6%96%87)


如果你有寫入權限的話 (被加入成 “Collaborators”)，就可透過 SSH 通訊協定，以 git 去 “clone” 下來：

```linux
$ git clone git@github.com:username/repository.git
```

如果沒有寫入權限 (Collaborators) 的話，因為這個專案是公開的，所以你還是可以用 Git 通訊協定 clone 下來：

```
$ git clone git://github.com/Username/repository.git
```

若因防火牆限制 git 通訊協定的存取 (如成功大學校園環境)，請改用 HTTPS 通訊協定：

```
$ git clone https://github.com/Username/repository.git
```


## Synchronize

* Set the remote repository
```linux
$ git remote add origin git@github.com:[username]/[project].git
```

* Set the branch upstream
> Then you can use **push** to update the repository

```linux
$ git push -u [remote_name] [local_branch]:[remote_branch]
```
>Warnning! If you want to use simple instruction **$git push** that your branch have to be named the same.

>You can use **$git help config**
>Looking for **push.default**

>•   **simple** - in centralized workflow, work like upstream with an added safety to refuse to push if the upstream branch’s name is different from the local one.


* Use instrction to check the branch status
```linux
$git branch -vv
```

* You can enter
```linux
$ git push
```
* Then your can update your git repository on Github


## Pull

```linux
$ git pull
```
or

```linux
$git pull origin [barnch_name]
```

* The targets is to update the local repository to refference the remote repository.


* Actually it was based on two instruction 
```linux
$ git fetch
```
* and merge your local commit
```linux
$ git merge
```

## Push: 將 Commit 送出去
>reference to : [jserv_Git 教學和 GitHub 設定指引](https://hackmd.io/@sysprog/gnu-linux-dev/https%3A%2F%2Fhackmd.io%2F%40sysprog%2Fgit-with-github#Git-%E6%95%99%E5%AD%B8%E5%92%8C-GitHub-%E8%A8%AD%E5%AE%9A%E6%8C%87%E5%BC%95)

```linux
$ git push
```

或

```linux
$ git push origin master
```
實際的作用是將本地端的 master branch 與遠端的 master branch 作 fast-forward 合併。如果出現 [rejected] 錯誤的話，表示你必須先作 pull。

