Git

Denny Huang

Denny Huang

Survey

是否寫過程式?

小專題/中大型
專案開發經驗

版本控制經驗

Shell 熟悉程度

Git / GitHub

It's different

ref:  https://www.facebook.com/photo.php?fbid=1592343944115344 
Source:  Code Smart, Don't Code hard  by 畢玉泉(CrBoy)<crboy@crboy.net>
Source:  Code Smart, Don't Code hard  by 畢玉泉(CrBoy)<crboy@crboy.net>
Source:  Code Smart, Don't Code hard  by 畢玉泉(CrBoy)<crboy@crboy.net>
Source:  Code Smart, Don't Code hard  by 畢玉泉(CrBoy)<crboy@crboy.net>
Source:  Code Smart, Don't Code hard  by 畢玉泉(CrBoy)<crboy@crboy.net>
Source:  Code Smart, Don't Code hard  by 畢玉泉(CrBoy)<crboy@crboy.net>
Source:  Code Smart, Don't Code hard  by 畢玉泉(CrBoy)<crboy@crboy.net>
Source:  Code Smart, Don't Code hard  by 畢玉泉(CrBoy)<crboy@crboy.net>
Source:  Code Smart, Don't Code hard  by 畢玉泉(CrBoy)<crboy@crboy.net>
Source:  Code Smart, Don't Code hard  by 畢玉泉(CrBoy)<crboy@crboy.net>
Source:  Code Smart, Don't Code hard  by 畢玉泉(CrBoy)<crboy@crboy.net>
Source:  Code Smart, Don't Code hard  by 畢玉泉(CrBoy)<crboy@crboy.net>
Source:  Code Smart, Don't Code hard  by 畢玉泉(CrBoy)<crboy@crboy.net>
Source:  Code Smart, Don't Code hard  by 畢玉泉(CrBoy)<crboy@crboy.net>
Source:  Code Smart, Don't Code hard  by 畢玉泉(CrBoy)<crboy@crboy.net>
Source:  Code Smart, Don't Code hard  by 畢玉泉(CrBoy)<crboy@crboy.net>
Source:  Code Smart, Don't Code hard  by 畢玉泉(CrBoy)<crboy@crboy.net>
Source:  Code Smart, Don't Code hard  by 畢玉泉(CrBoy)<crboy@crboy.net>
Source:  Code Smart, Don't Code hard  by 畢玉泉(CrBoy)<crboy@crboy.net>
Source:  Code Smart, Don't Code hard  by 畢玉泉(CrBoy)<crboy@crboy.net>
Source:  Code Smart, Don't Code hard  by 畢玉泉(CrBoy)<crboy@crboy.net>
Source:  Code Smart, Don't Code hard  by 畢玉泉(CrBoy)<crboy@crboy.net>
Source:  Code Smart, Don't Code hard  by 畢玉泉(CrBoy)<crboy@crboy.net>
Source:  Code Smart, Don't Code hard  by 畢玉泉(CrBoy)<crboy@crboy.net>
Source:  Code Smart, Don't Code hard  by 畢玉泉(CrBoy)<crboy@crboy.net>
Source:  Code Smart, Don't Code hard  by 畢玉泉(CrBoy)<crboy@crboy.net>
Source:  Code Smart, Don't Code hard  by 畢玉泉(CrBoy)<crboy@crboy.net>
Source:  Code Smart, Don't Code hard  by 畢玉泉(CrBoy)<crboy@crboy.net>
Source:  Code Smart, Don't Code hard  by 畢玉泉(CrBoy)<crboy@crboy.net>
Source:  Code Smart, Don't Code hard  by 畢玉泉(CrBoy)<crboy@crboy.net>
Source:  Code Smart, Don't Code hard  by 畢玉泉(CrBoy)<crboy@crboy.net>
Source:  Code Smart, Don't Code hard  by 畢玉泉(CrBoy)<crboy@crboy.net>
Source:  Code Smart, Don't Code hard  by 畢玉泉(CrBoy)<crboy@crboy.net>
Source:  Code Smart, Don't Code hard  by 畢玉泉(CrBoy)<crboy@crboy.net>
Source:  Code Smart, Don't Code hard  by 畢玉泉(CrBoy)<crboy@crboy.net>

Source:  PTT Soft_Job [請益] 個位的版本控制(EC)工具

攻城屍的救星

版本控制

版本控制類型

本地端版本控制


集中式版本控制系統


分散式版本控制系統

事前準備

  • 註冊 GitHub 帳號
  • 確認已安裝 Git
    • 在 Terminal 輸入 git 確認
    • sudo apt install git
    • sudo dnf install git
  • 選擇編輯器 (vim, emacs, gedit)

環境設定

git config --global user.email "you@example.com"

git config --global user.name "Your Name"

git config --global color.ui true

git config --global core.editor vim

環境設定

git config --global alias.co commit
git config --global alias.lg "log --color --graph --all --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --"

.gitconfig

Why --global?

Practice

Create a new repo on GitHub

Public / Private

Student Developer Pack

README.md

Markdown Cheatsheet / Mastering Markdown

.gitignore

.gitignore templates / Git Large File Storage

License

授權條款介紹 - OpenFoundry

Practice

Delete the repository

Practice

Create an new repo without initialization

Practice

Create a new local repository

git help <command>

git init

Practice

Add file to staging area

back
圖片來源

touch README.md

git status

Add file to staging area

  • git add README.md

  • git add .

.gitkeep

Unstage file

  • git reset <file>

Experimental alternatives after 2.23

  • git restore <file>
img

Practice

Commit to repository

img

Before commit

  • git status

  • git diff

  • git diff --cached

Commit

  • git commit

  • git commit -m "first commit"

何時該 commit?

commit log 怎麼寫?

圖片來源
yo

git commit -a

圖片來源

Practice

Show commit message

Show commit log

  • git log

  • git show <revisions>

SPECIFYING REVISIONS

gitrevisions(7) Manual Page

Practice

Untrack / Rename file

Remove file

  • git rm <file>

Untrack file

  • git rm --cached <file>

Rename file

  • git mv <file>

Practice

Back to the past

Checkout to specific revision

  • git checkout <revision>

Checkout file from specific revision

  • git checkout <revision> <file>

Stash files

  • git stash

  • git stash save "message"

  • git stash list

  • git stash pop

  • git stash show -p <revision>

Reset to previous version

  • git reset <revision>

  • git reset --hard <revision>

Revert commit

  • git revert <revision>

Remove untracked files

  • git clean -f

  • git clean -d

Practice

Push to remote server

Add remote repo

  • git remote add origin https://github.com/denny0223/workshop.git

  • git remote

  • git remote -v

  • git remote show origin

圖片來源

List branches

  • git branch

  • git branch -a

  • git branch -v

  • git branch -vv

Push to remote repo

  • git push -u origin master

  • git push origin master

  • git push

  • push.default

Cache https password

git config --global credential.helper cache

git config --global credential.helper "cache --timeout=3600"


more

Practice

Clone your repo via ssh

try to commit and push

git clone




Please make sure you have the correct access rights

and the repository exists.

Generating SSH keys

Practice

Pull from remote repo

Add a new commit at remote via https repo.

git pull

git fetch + git merge

git pull --rebase

Practice

Resolving conflict

git mergetool

Branch

GitHub Pages

Practice

Create a branch

Create a branch

  • git branch <branch name>

  • git checkout -b <branch name>

Experimental alternatives after 2.23

  • git switch -c <branch name>

Practice

commit and push new branch to remote

Push when local and remote branch name difference

  • git push <remote> <local_branch_name>:<remote_branch_name>

Git Flow

A successful Git branching model

Practice

merge branches

Merge

  • git merge <branch>

  • git merge --no-ff <branch>

Practice

Delete branches

Delete branch

  • git branch -d <branch>

  • git branch -D <branch>

  • git push <remote> :<branch>

  • git fetch -p

Move branch refs

  • git branch -f <branch>

  • git branch -f <branch> <revision>

Practice

Trace log

More about diff

  • git diff <revision>

  • git diff master@{2014-07-01}

  • git diff master@{"1 month ago"}

More about git log

  • git log -p --stat

  • git log --oneline

  • git log --graph

  • git log --abbrev-commit

  • git log --pretty=%an

  • git whatchanged

Limit

  • git log --author="Denny Huang"

  • git log --since={2014-07-01} --until="1 week ago"

search by regex

  • git log -p -i -G 'regex'

Who wrote this code?

  • git blame <filepath>

Use binary search to find the commit that introduced a bug

  • git bisect start

  • git bisect bad

  • git bisect good v1.0

Tag

  • git tag <tagname>

  • git push <remote> <tagname>

  • git tag -d <tagname>

  • git push <remote> :<tagname>

  • Release on GitHub

Rebase

  • git rebase

Cherry-pick

  • git cherry-pick <revision>

Patch file

  • git format-patch

  • git am <patch_file>

Submodule

  • git submodule add <repo>

  • git submodule init

  • git submodule update

  • git submodule foreach git pull origin master

Patch commit

  • git add --patch

  • git commit --patch

Practice

Modify you commit

Modify commit history

  • git commit --amend

  • git commit --amend --date="Mar 15 00:00 2014"

  • git rebase -i <revision>

Useful skill

  • git reflog

  • git fsck

Hosting a Git server

Create a bare repo

  • git init --bare

  • git clone ///path/to/repo

  • git clone ssh://[username]@[host]:[port]/path/to/repo

Add hooks

  • .git/hooks/

  • git help githooks

  • more

Rewrite branches

How Git works?

  • inotifywait -rm --format '%w%f %e' -e create -e close_write .

  • git init

  • echo test > file

  • git add file

9daeafb9864cf43055ae93beb0afd6c7d144bfa4

  • SHA1 of "blob 5␀test␊"

  • printf "blob 5\000test\n" | xxd

  • printf "blob 5\000test\n" | shasum

  • echo test | git hash-object --stdin

  • git cat-file -p 9daeafb9864cf43055ae93beb0afd6c7d144bfa4

cd .git/objects/9d/

$ python
Python 2.7.5 (default, Nov  3 2014, 14:26:24)
[GCC 4.8.3 20140911 (Red Hat 4.8.3-7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> fd = open("aeafb9864cf43055ae93beb0afd6c7d144bfa4", "rb")
>>> line = fd.read()
>>> import zlib
>>> zlib.decompress(line)
'blob 5\x00test\n'

cat-file -p <revision>

git cat-file -p 8b9af3b

tree 993624220e92bb14b1703b6005dccfda913e95bc
parent a5d55500219b63ac21bb3977ae1a69bf5f1f793f
author Denny Huang <denny0223@gmail.com> 1407702807 +0800
committer Denny Huang <denny0223@gmail.com> 1407702807 +0800

update

Issue tracking system

Continuous Integration (CI)

剩下就是你們的事了

Something useful

Something useful (cnt.

Just for fun

Show branch names in $PS1

Halloween Bash Profile Generator

function parse_git_branch {
    git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}

export PS1="\h:\W \u\$(parse_git_branch)$ "

Resources

Resources (cnt.

20 Years

10 Years

Thanks for listening



本投影片採用

創用 CC「姓名標示-相同方式分享 4.0 國際」授權條款釋出
Marp 製作

Google tag (gtag.js)