Git 手冊

設定

指令設定

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
git config --list # git config -l

# account
git config --global user.name "[name]"
git config --global user.email "[email address]"

# push
git config --global push.default simple

# core
git config --global editor vim
git config --global ignoreCase false

# color
git config --global color.diff auto
git config --global color.status auto
git config --global color.branch auto
git config --global color.log auto

# alias
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.aa add --all
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cblueby %an %Cgreen(%cr)%Creset'"
git config --global alias.lg "log --graph --pretty=format:'%C(reverse ul cyan)%h%Creset -%C(yellow)%d%Creset %C(ul)%s%Creset %C(dim white)<%an>%Creset %Cgreen(%cr)%Creset %ci'"

編輯 ~/.gitconfig

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[user]
name = your_user_name
email = account@domain.com
[core]
editor = vim
ignorecase = false
[push]
default = simple
[color]
diff = auto
status = auto
branch = auto
log = auto
[alias]
co = checkout
br = branch
aa = add --all
ci = commit
st = status
lg = log --graph --pretty=format:'%C(reverse ul cyan)%h%Creset -%C(yellow)%d%Creset %C(ul)%s%Creset %C(dim white)<%an>%Creset %Cgreen(%cr)%Creset %ci'

基本使用

  • .gitignore 忽略不需要的檔案
  • .gitkeep 保留空資料夾
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
git init
git status
git status -s

git add <pathspec>... # 新增特定檔案 to staging: file name, *.extension, folde name
git add . # 新增所有檔案 to staging
git commit -m '[commit messgae]'
git commit -am '[commit messgae]' # 自動 stage 被修改, 刪除的檔案
git commit --amend # 修改最後一次的 commit
git commit --amend -m '[commit messgae]' # modify last commit
git rm <file>...
git rm --cached <file>... # 刪除檔案
git rm --cached -r . # 所有 staging 檔案 to untracked
git mv <file_name_old> <file_name_new> # 重新命名檔案
git mv file_from file_to # git mv <options>... <args>...
git checkout master <file_name> # 從 master 取回檔案

git diff
git diff --staged # git diff --cached

# 提交空目錄
mkdir img
cd img
touch .gitkeep

# 清除 untrack file or folder
git clean -f
git clean -df

# 查看 log
git show 9083cdc # git show <object>...
git blame wtf.html # git blame index.html
git log
git log --graph --oneline
git log --stat
git log --pretty=oneline
git log -p README.md
git log -p -2
git log --pretty=format:"%h %s" --graph
git log --since=2.weeks
git log -Sfunction_name # git log -S<string> -G<regex>
git log --pretty="%h - %s" --author=gitster --since="2008-10-01" --before="2008-11-01" --no-merges
git reflog # 所有的 local 歷史紀錄,遠端不會有,存放在 ~/.git/logs/refs/
git log -g # reflog 詳細版

git reset HEAD README.md # Unstageing
git checkout -- README.md # Unmodofying, 修改紀錄會消失!!!!!

git clone <GIT URL>
git remote -v
git remote add pb <GIT URL>
git fetch pb # git fetch <remote>
git remote show origin
git remote rename pb paul
git remote remove paul

Git Branching

  • 特性:輕量化、快速切換、鼓勵多合併(commit 圖解請參閱 Git Branching)
  • 預設分支:master
  • 目前分支的指標: HEAD
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
git branch -l
git branch -r
git branch -v
git branch -vv

# 建立分支
git branch <newbranchname>
git checkout -b <newbranchname> # 等同 git branch <newbranchname> && git checkout <newbranchname>
# 建立分支 - 針對節點
git branch <newbranchname> <object>
git checkout -b <newbranchname> <object>

# 切換分支
git branch <branchname>
git checkout <branchname>

# 合併分支
git merge <branchname>

# 刪除分支
git branch -d <branchname> # --delete
git branch -D <branchname> # 強制刪除 --delete --force

git branch --merged
git branch --no-merged
git branch --no-merged master

# remote
git ls-remote [remote]
git remote show [remote]
git fetch <remote>
git remote add
git push <remote> <branch>
# tracking
git checkout -b <branch> <remote>/<branch> # or git checkout serverfix
git branch -u origin/serverfix

# pulling
git pull # git fetch && git merge

## delete remote branch
git push origin --delete serverfix

Rebase

不要 rebase 其他人正在進行的 branch

1
2
3
4
git checkout <branchname>
git rebase master
git rebase <basebranch> <topicbranch>
git pull --rebase

Git Branching - Remote

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# push
git push origin serverfix
git fetch origin
git checkout -b serverfix origin/serverfix

# track
git checkout --track origin/serverfix
git checkout serverfix
git checkout -b sf origin/serverfix
git branch -vv
git fetch --all; git branch -vv

# pull
git pull

# delete
git push origin --delete serverfix

# create empty branch
git checkout --orphan empty-branch
git rm -rf .
git commit --allow-empty -m "root commit"
git push origin empty-branch

Git on the Server

Hosting

1
2
3
4
5
6
7
8
9
10
# remote 操作
git clone git@github.com:[username]/[repo].git
git push -u
git remote add [name] git@github.com:[username]/[repo].git

git remote
git remote -v
git remote show origin
git pull [name]
git remote rm [name]

產生 SSH Public Key

  • ssh-keygen: Linux/macOS 內建、Git for Windows
    • passphrase: 使用 key 時需要密碼就填入(ssh-keygen -o 可防暴力密碼破解),不需要留空即可
  • id_rsa or id_dsa: private key
  • id_rsa.pub or id_dsa.pub: public key
  • public key 新增至 Github
1
2
3
4
5
6
cd ~/.ssh
ssh-keygen -t rsa -b 4096 -C "[email address]"
cat id_rsa.pub

# 測試 Github public key
ssh -T git@github.com

疑難雜症

1
2
# git help <verb>
git help config

修改歷史紀錄

1
2
3
4
5
git rebase -i <earliercommit>
git commit --amend --author="Author Name <email@address.com>"
git commit --amend -m="message"
git rebase --continue
git push -f

https://stackoverflow.com/questions/3042437/how-to-change-the-commit-author-for-one-specific-commit

參考資源

評論

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×