# Git/Github 協作常用 ###### tags: `Git/GitHub` ## `git clone`, `git branch -r`, `git switch <remote-branch-name>` :::spoiler 觀念與使用情境  ### Remote Tracking Branch > Remote Tracking Branch 會指向你**最後一次與 remote repo (GitHub repo)溝通的地方** > 它就像是一個書籤 與 remote repo 溝通指的包括: - clone, pull, fetch 下來 - push 上去 #### remote tracking branch syntax pattern: `<remote>/<branch>` ex: ```bash origin/main upstream/feature ``` <br > ### Local Repo v.s. GitHub repo <img src="https://i.imgur.com/ZILoKDI.png" width="350px"> :arrow_up: 本機端可以在自己的電腦上任意移動操作 <br > <img src="https://i.imgur.com/2kK9aqs.png" width="700px"> :arrow_up: **Remote Tracking Branch** 則是一個對於某個 GitHub remote branch 上一次溝通的時候的狀態的 reference,我們無法在自己的電腦上直接操作他的位置 <br > <img src="https://i.imgur.com/HtI9ieh.png" width="550px"> <img src="https://i.imgur.com/MrZYes2.png" width="550px"> :arrow_up: *因為 remote tracking branch 與 local branch 的機制,我們才會在 `git status`得到這樣的訊息* <br > ### `git clone` 後的 Local Branch 與 Remote Branches > 當我們 `git clone` 一個 GitHub repo,我們的本機就擁有了那個 GitHub Repo 在那個時刻所有的 data 和 Git history, 但是...  這個 GitHub Repo 有兩個 branch 不過,此時如果我們用 **`git branch`** 指令查看 local branch,會發現的我們 local repo 只有 GitHub repo 的 default branch <img src="https://i.imgur.com/IShxO2s.png" width="250px"> :arrow_up: 我們 local 端只有 default branch <br > ### `git branch -r` > 檢視所有 local 知道的 remote branch > <img src="https://i.imgur.com/mCBjBfD.png" width="250px"> :arrow_up: 當我們用 `git branch -r` 檢視,可以發現其實我們本機是知道所有的 remote branch 的 **事實上 `git clone` 所做的事**  ### `git switch <remote-branch-name>` > 1. 創建一個與 remote branch 同名的 local branch > 2. 設定好這個同名的 branch 會去 track remote branch example: 想要本機有 `origin/puppies` 這個 branch,並且 connect local 與 remote 的 puppie branch <img src="https://i.imgur.com/h2sfGxi.png" width="400px"> <img src="https://i.imgur.com/sYiShxs.png" width="600px"> ::: *** ### `git checkout <remote>/<branch>` :::spoiler 用途 #### 情境:想要看我上次跟 remote repo(GitHub repo)溝通時候,這個專案 code 是怎麼樣 syntax example ```bash= git checkout origin/main #回到上次跟 remote tracking branch 溝通的地方 git switch main # 再回來目前本機的 main branch 所在的地方與狀態 ``` <img src="https://i.imgur.com/7zi9Yl8.png" width="500px"> ::: <br > *** ## `git pull` v.s. `git fetch` > #### 都是把 code 從 GitHub 上拿下來的方式 > #### Retrieve changes from a remote repository ::: spoiler 解釋 #### 情況舉例  ### 比較  ### `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." #### 圖解  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`  #### :thought_balloon: "go and download data from Github AND immediately update my local repo with those changes" ### 圖解  :exclamation: **pulls can result in merge conflicts!!** ::: <br >
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up