Skip to main content

[Git] Git and Github

What is Git?

Git 是一個免費開源的版控系統,相對於傳統的集中式版控系統(CVS, Centrailize Version Control),需要一個專用的伺服器,Git是一種分散式的板控系統(Distributed Version Control) ,實際上Git的操作不需要連上網路,就可以在電腦主機進行操作,在本地端進行版本控制。

現在的開發模式規模越來越大,許多開發者都在不同的電腦進行開發,git透過簡單明瞭的指令,幫助開發者們進行分散式的版控,開發時有各自的分支去控管檔案,合併時會顯示分支間的差異,由使用者去選擇要保留或刪除檔案,達成很棒的合作模式。

What is Github?

Github 則是一個使用git的大型伺服器,有著世界上最大的開發者社群,加速了開發者合作與溝通,我們常常看見開發者有自己的帳號,用Github提供的圖形介面來管理自己的作品和程式碼,也可以去參考其他人的repo,或是參與些開源作品。

Download git & git commands

我們到官網下載git,下載完之後可以去command prompt測試有沒有安裝成功,安裝成功,會出現常見的指令列表畫面

git common commands

  1. git clone

    git clone <repository url> 下載遠端repo的最新版本

    git clone <repository url> -b <branch name> 複製指定特定的分支

  2. git init

    git init <repository name> 設定git,產生.git的資料夾來進行版控,通常用在新資料夾

  3. git status

    git status 秀出工作repo的變化,包括haven't staged、staged和沒有被git追蹤的(are staged, unstaged, and untracked)。

    ex.

  4. git diff

    git diff --staged 秀出add到stage裡面的變化

    gif diff <branch1> <branch2> 比較兩個branch的差別

    :q離開輸出視窗

  5. git add

    git add <file path> 加入特定檔案到staged

    git add . 加入全部變化的檔案到staged

  6. git commit

    git commit -m "<commit message>" 所有change都stage之後,我們可以提交成一個版本,帶有提示這個版本的訊息(-m)

  7. git push

    git push --set-upstream <remote branch> <branch name> 當提交的分支還沒有遠端的連結,我們需要設定遠端的連結分支

    git push 推送到遠端連結的分支

    git push <remote branch> <specific branch> 推送特定分支到遠端

    git push <remote branch> <branch name> : <remote specific branch> 推送到遠端的特定分支(如specific一開始不存在,會建立出名為specific的分支)

    ex.

    git push origin second:second

  8. git branch

    git branch <new branch> 建立新分支

    git branch -d <branch name> 刪除特定分支

  9. git checkout 還原檔案與切換分支

    git checkout index.html 還原一個檔案, index.html

    git checkout . 還原成上一版的全部檔案

    git checkout <branch name> 切換到特定分支

    git checkout -b <branch name> 建立並切換分支

  10. git fetch

    git fetch <remote branch> 取得遠端分支的全部更新進度,包含新分支

    此指令會更新目前資料夾的檔案,但不會更新到當前正在編輯的資料夾。

  11. git merge

    git merge <master branch> <being merged branch> 合併後面定義的分支檔案到當前分支檔案

  12. git pull

    git pull <remote> 取得遠端分支全部變化,並合併到現在工作的本地分支

    此指令是 git fetch + git merge,而如果 git merge 有衝突的話,會被中斷,等於只有執行 git fetch

  13. git remote

    git remote -v 顯示所有遠端連接分支

    git remote add lab <repository url> 新增遠端分支(lab)

    git remote rename <origin name> <new name> 重新命名分支

    ex.

    git remote rename origin old-origin

https://blog.testproject.io/2021/03/22/git-commands-every-sdet-should-know/ https://www.atlassian.com/git/tutorials

Git global setup

git config --global user.name "Thibe"
git config --global user.email "example@gmail.com"

Github 推送本地資料夾

cd change directory

add, commit -m, push 三基本步驟

ex.

cd existing_folder
git init --initial-branch=main
git remote add origin git@github.com:thibe/example_repo.git
git add .
git commit -m "Initial commit"
git push -u origin main

Github 複製遠端資料夾

git switch -c <main> switch to "main" and create branch "main"

touch 建立新檔案

README.md 操作手冊、專案說明

ex.

git clone git@example.com:thibe/example_repo.git foldername
cd foldername
git switch -c main
touch README.md
git add README.md
git commit -m "add README"
git push -u origin main

Git 官網合作範例

Doug & Dana 協作程式

doug. $ git checkout master
doug. $ git commit -a -m"My new logo"
doug. $ git push

dana. $ git checkout -b danasfeature
dana. $ git commit -a -m"My feature code"
dana. $ git push origin danasfeature

doug. $ git pull
doug. $ git merge danasfeature

https://git-scm.com/video/what-is-git

note

常用的 log 查詢法

  1. git log --oneline --graph --all
  2. git log --graph --decorate --abbrev-commit --all --pretty=oneline

相關資源

To study

git config --global pull.ff only (pop error when conflict)

git bisect

git bisect start

git bisect reset