---
# System prepended metadata

title: Git / Github 常用進階指令

---

# Git / Github 常用進階指令 

## `git pull` v.s. `git fetch`

### 本機上 git 的 branch 與 GitHub 上的 Remote Branch

![](https://i.imgur.com/UlgGCam.png)

<br >

#### `git pull` v.s. `git fetch`
> #### 都是把 code 從 GitHub 上拿下來的方式
> #### Retrieve changes from a remote repository

#### 情況舉例
![](https://i.imgur.com/8HV9iKx.png)

### 比較

![](https://i.imgur.com/umBxlLq.png)

### `git fetch`
Fetching allows us to download changes from a remote repository, BUT those **changes will NOT be automatically integrated into our working files.** 

:arrow_right: It lets you see what others have been working on, WITHOUT having to merge those changes into your local repo.


:thought_balloon: "please go and get the latest information from Github, but don't screw up my working directory."

#### 圖解
![](https://i.imgur.com/VfF6isO.png)

I now have those changes on my machine, but if I want to see them I have to checkout origin/master.  My master branch is untouched!

#### syntax
<img src="https://i.imgur.com/uzKeMma.png" width="270px">

<img src="https://i.imgur.com/mMP1Hzw.png" width="330px">

<br >

### `git pull`
![](https://i.imgur.com/5CczYmK.png)

#### :thought_balloon: "go and download data from Github AND immediately update my local repo with those changes"

### 圖解
![](https://i.imgur.com/kK4D7dW.png)

:exclamation: **pulls can result in merge conflicts!!**

<br >

## `git reset`
> #### Undo & Time-Traveling

### `git reset <commit-hash>`

#### 情境：git commit 以後，才發現想要那個 commit 在另一個 branch

<img src="https://i.imgur.com/PV71SNH.png">

:arrow_up: 已經 commit 了，卻發現不是要 commit 在這個 branch 上

<img src="https://i.imgur.com/Mp8LMDe.png">

:arrow_up: 用 **`git reset <commit-hash>`** 可以 undo 這個 commit 的紀錄，但到這個 commit 的時間點所做的內容仍然還在（然後就可以重新 commit 在另一個 branch ）


### `git reset --hard <commit-hash>`
![](https://i.imgur.com/lH91W86.png)

:arrow_up: 用 **`git reset --hard <commit-hash>`** ，會 undo 這個 commit，並且 time travle 到這個 commit 時間點之前的內容

<br >

### `git reset HEAD~<commitBackwardNum>`
#### 情境：想要回頭看前面的 commit 的狀態

![](https://i.imgur.com/4VoI5nh.png)

:arrow_up: 用 **`git reset HEAD~<commitBackwardNum>`** ，可以讓我們 time travel 回到前 n 個 commit

<br >

## `git rebase`
(下次再補細節，先大致介紹)

> ### 1. As an alternative to merging
> ### 2. As a cleanup tool

### As an alternative to merging
#### issue with `git merge` during collaboration
<img src="https://i.imgur.com/UFa4yz2.png" width="400px">

:arrow_up: 同時有很多branch，想要 update 到最新的版本的話，我們的 commit 紀錄會有很多是 merge

![](https://i.imgur.com/VD5ZR67.png)


<img src="https://i.imgur.com/QK0XSqS.png" width="300px">



<br >

### As a cleanup tool
> #### REWRITE, DELETE, RENAME, or even REORDER commits

#### Interactive Rebase
搭配使用：`pick`, `reword`, `edit`, `fixup`, `drop`

![](https://i.imgur.com/fAbfvOR.png)

![](https://i.imgur.com/EuWgkLX.png)
