Git 基礎工作坊

https://slides.com/dmoon/git/live

DMoon

dmoon.t@gmail.com

Facebook

GitHub

Blog

Version Control?

檔案本的管理方法

講人話.....

備份

想像成遊戲的記錄存檔

Why should I use version control ?

那些coding的痛苦回憶

總有把程式改壞的時候...

趕快瘋狂 Ctrl + Z 

hmm 原本應該是這樣子的.......嗎?

或是更聰明的你

瘋狂 Ctrl + Shift + S 

It works

Git

分散式版本控制系統

Git 不是 GitHub !!!

Git

  • Since  2005

  • Linus Torvalds

  • 免費

  • 開源

分散式架構

遠端、本地都能操作

Git 能做什麼?

  • 對一個資料夾下的所有檔案做備份管理
  • 將檔案的Snapshot(快照)保存到歷史紀錄
  • 為每次檔案更動留下說明
  • 將檔案復原到某個歷史紀錄
  • 顯示不同記錄版本的檔案差異
  • 與他人協同合作
  • 找戰犯

還記得遊戲的紀錄點嗎?

git 裡叫做 commit

未保存的變更

已保存的記錄 (commit)

Timeline

環境準備

  • 安裝 Git
  • 安裝 SourceTree
  • 註冊 GitHub

備份三步驟

  • 編輯檔案
  • 選擇此次版本要存的檔案
  • Commit 儲存版本

SourceTree

Init Repository

  1. File > New / Clone > Create Local Repository
  2. Enter Destination Path & Project Name
  3. Type Git
  4. Create

練習

Create a Git Local Repository

File Status

Commit

History

練習:第一個 Commit

CLI

# set your default info
$ git config --global user.email "name@mail.com"
$ git config --global user.name "userName"

# set info of current git project
$ git config user.name "userName"

# list config
$ git config -l

Tell Git who you are

要學的指令有點多...

重要的是記得有哪些功能!

忘了這邊查 -> Git 命令快速參考

Create a new local repository

# init git in your project directory
$ git init

檔案分類

  • ignored

  • tracked

  • unTracked

ignore

忽略不需要追蹤/記錄的檔案

建立 .gitignore 檔案

.gitignore sample

# 輸入你想忽略的檔案路徑名稱

# ignore ./test.html
test.html


# ignore whole ignore_dir directory
ignore_dir/

# ignore all css file
*.css

Branch

Saving changes

Status

List the files you've changed

and those you still need to add or commit

Add

# add file
$ git add fileName

# add all files
$ git add .

# check added files
$ git status
  1. untracked -> tracked

  2. add file to Staging Area

remove added file

# untrack & delete tracked file
$ git rm fileName

# only untrack tracked file 
$ git rm --cached fileName

commit

# check current status
$ git status

# commit added files
$ git commit

# commit with message
$ git commit -m "Write some message here"

提交一個新的版本

Log

# show git commit histories
$ git log

Commit all tracked files

修改最後一個提交訊息

$ git commit --amend

Ref: HEAD

永遠指向目前分支最新Commit

Diff

# diffs between tracked but unStaged files and HEAD
$ git diff

# diffs between staged files and HEAD
$ git diff --cached

# src and dest can be commitID or branch
$ git diff <src> <dest>

Stash

# 暫存當前工作目錄
$ git stash

# show stash list
$ git stash list

# 還原暫存工作目錄
$ git stash pop

# delete last stash
$ git stash drop

# clear all stash
$ git stash clear

checkout

# switch branch
$ git checkout branchName

# checkout old version commit
$ git checkout <commitID>

切換 HEAD

Undoing Changes

Reset

# reset file to last commit
$ git reset HEAD fileName

# reset all files to last commit
$ git reset HEAD

# reset with option
$ git reset --option HEAD 
Reset Option Work Directory Staging Area HEAD
soft X X O
mixed (default) X O O
hard O O O

Reset Option

O:do  reset  

X:not reset 

Branches

為何要新增 branch?

# list all branches
$ git branch

# create a new branch
$ git branch <branchName>

# switch to another branch
$ git checkout <branchName>

# create a new branch and switch to it
$ git checkout -b <branchName>

# delete the branch
$ git branch -d <branchName>

Branch

Brach 不用錢

開幾個練習看看吧

Merge

Merge

合併分支

# merge a <branchName> into your active branch
$ git merge <branchName>

Conflict

Collaborating

Central Repo

軟體原始碼代管服務

GitHub

全球最大同性交友平台上線啦

Clone project from Repo

# clone a repo
$ git clone <srcRepoPath> <destDirName>

Remote Repository

# connect local repository to a remote repository
$ git remote add <remoteRepoName> <remoteRepoPath>

# list all connected remote repository
$ git remote -v

Syncing

Remote Branch

A's local Branch

B's local Branch

Push

# send changes to the remote branch 
# of your remote repository
$ git push <remoteRepoName> <remoteBranchName>

fetch

# fetch the latest history from the server
$ git fetch

# remoteRepoName default origin
# remoteBranchName default master
$ fetch result is in FETCH_HEAD
$ git fetch <remoteRepoName> <remoteBranchName>


Pull

fetch + merge

# fetch and merge remoteBranch to current branch
$ git pull <remoteRepo> <remoteBranch>

Git Flow

Blame

# show file history
$ git blame <fileName>

深入介紹 GitHub

Free for Public Project

Github

Pull Request

  1. push your branch to remote

  2. create a pull request

  3. wait for admin to merge

合併請求

Fork

你可能沒有編輯他人 Repo 的權限

這時可以Fork一份自己權限的 Repo

修改後新增 Pull Request 向原本專案提出合併申請

GitLab

Integration with Editor / IDE 

Visual Studio Code

git

By dmoon

git

  • 2,144