指令設定 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 # accountgit config --global user.name "[name]" git config --global user.email "[email address]" # pushgit config --global push.default simple # coregit config --global editor vim git config --global ignoreCase false # colorgit config --global color.diff auto git config --global color.status auto git config --global color.branch auto git config --global color.log auto # aliasgit 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 foldergit clean -f git clean -df # 查看 loggit 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
特性:輕量化、快速切換、鼓勵多合併(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 # remotegit ls-remote [remote] git remote show [remote] git fetch <remote> git remote add git push <remote> <branch> # trackinggit checkout -b <branch> <remote>/<branch> # or git checkout serverfix git branch -u origin/serverfix # pullinggit pull # git fetch && git merge # # delete remote branchgit push origin --delete serverfix
不要 rebase 其他人正在進行的 branch
1 2 3 4 git checkout <branchname> git rebase master git rebase <basebranch> <topicbranch> git pull --rebase
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 # pushgit push origin serverfix git fetch origin git checkout -b serverfix origin/serverfix # trackgit checkout --track origin/serverfix git checkout serverfix git checkout -b sf origin/serverfix git branch -vv git fetch --all; git branch -vv # pullgit pull # deletegit push origin --delete serverfix # create empty branchgit checkout --orphan empty-branch git rm -rf . git commit --allow-empty -m "root commit" git push origin empty-branch
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 keyssh -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
參考資源