Git 全局設(shè)置
配置
git config/co
git config user.name <lilith> [--global/--local]
git config user.email <lilith@163.com> [--global/--local]
# 刪除
git config -–unset/-u user.name [--global/--local]
# 編輯
git config --edit/-e [--global/--local]
# 查看
git config --list/-l [--global/--local]
# 查看/設(shè)置忽略大小寫
git config core.ignorecase [--global/--local]
git config core.ignorecase <false> [--global/--local]
git help
git help -a
創(chuàng)建和獲取倉庫
創(chuàng)建倉庫 git init/it
git init [directory]
克隆倉庫 git clone/cl
git clone <repo> [directory]
基本操作
添加文件到倉庫 git add/ad
git add [file1] [file2]
git add [dir]
git add .
查看倉庫當(dāng)前的狀態(tài) git status/st
git status
提交暫存區(qū)到本地倉庫 git commit/cm
git commit -m <message>
git commit [file1] [file2] ... -m <message>
git commit -a # 無需git add 直接提交
git commit --amend # -am 修改提交注釋
恢復(fù)工作區(qū)文件
git restore [file1] [file2]
git restore .
回退版本 git reset/re
# --mixed 默認(rèn)婆排,混合重置
git reset [--soft | --mixed | --hard] [HEAD | hash]
# 回退到當(dāng)前版本删掀,即重置暫存區(qū)
git reset HEAD [file1] [file2]
git reset HEAD . # 默認(rèn)
重命名一個(gè)文件 git mv
# 可用于修改大小寫文件统捶,重命名后會(huì)自行添加到暫存區(qū)
git mv -f [file] [newfile]
分支與合并
分支的增刪改查 git branch/br
# 增
git branch <branchname>
git push origin <branchname> [--set-upstream] # 推送到遠(yuǎn)端[關(guān)聯(lián)]
# 將localbranch與遠(yuǎn)端remote-branch分支關(guān)聯(lián)浪册,關(guān)聯(lián)后推送可直接推送
git branch --set-upstream-to=origin/<remote-branch> <localbranch>
# 刪
git branch -d <branchname>
git branch -D <branchname>
# 改
git branch -m <oldName> <newName>
# 查
git branch [-r] # 可查看遠(yuǎn)端分支
檢出分支 git checkout/ch 推薦直接使用 switch 代替
git checkout <branchname>
git checkout -b <branchname> # 新建并切換到新分支
切換分支 git switch/sw 具有檢出功能
git switch <branchname>
合并分支 git merge/mr
git merge <branchname>
查看歷史提交記錄 git log
git log [--oneline] [--graph] [--reverse]
# oneline 一行展示
# graph 查看歷史中什么時(shí)候出現(xiàn)了分支惠豺、合并
# reverse 反向展示
暫存到 git 棧中 git stash
git stash # 備份當(dāng)前工作區(qū)的內(nèi)容,保存到git棧中
git stash pop # 取出最近一次stash的內(nèi)容抛寝,并刪除對應(yīng)的stash
git stash list # 查看所有的stash
標(biāo)簽 git tag
# 增
git tag <tagname> [hash]
git push origin <tagname>
# 刪
git tag -d <tagname>
git push origin -d <tagname>
# 改
# 刪除原有標(biāo)簽涨颜,新建即可
# 查
git tag
# 批量操作
git push --tags # 推送
git fetch --tags # 拉取遠(yuǎn)端
git tag -l | xargs git tag -d # 刪除
git tag -l | xargs git push origin tag -d # 刪除遠(yuǎn)端
共享和更新項(xiàng)目
獲取本地沒有的數(shù)據(jù) git fetch/fe
git fetch [origin] [branch] # 倉庫別名
git fetch -p # 同步遠(yuǎn)程庫變化
從遠(yuǎn)程獲取代碼并合并本地的版本 git pull/pl
git pull
git pull [<遠(yuǎn)程主機(jī)名> <遠(yuǎn)程分支名>[:<本地分支名>]]
# 將遠(yuǎn)程主機(jī) origin 的 master 分支拉取過來,與本地的 localbranch 分支合并
git pull origin master:<localbranch>
git pull [--prune/-p] # 同步遠(yuǎn)程庫變化
上傳到遠(yuǎn)程并合并 git push/ps
git push [<遠(yuǎn)程主機(jī)名> <本地分支名>:<遠(yuǎn)程分支名>] # 推送到指定分支
git push --force/-f # 強(qiáng)制推送
git push origin <branchname> [--set-upstream] # 推送到遠(yuǎn)端[關(guān)聯(lián)]
git push origin <branchname> --delete/-d # 刪除遠(yuǎn)程分支
git push -u origin --all # 將本地分支推送到遠(yuǎn)程并關(guān)聯(lián)
git push -u origin --tags # 將本地標(biāo)簽推送到遠(yuǎn)程并關(guān)聯(lián)
遠(yuǎn)程倉庫的操作 git remote/rem
git remote -v # 查看遠(yuǎn)程倉庫信息
git remote add <origin> <url> # 添加遠(yuǎn)程倉庫
git remote rm <origin> # 刪除遠(yuǎn)程倉庫
git remote rename <old-origin> <new-origin> # 修改倉庫名
git remote update <origin> --prune # 同步遠(yuǎn)程庫變化
其他操作
合并多條 commit git rebase
git rebase -i HEAD~n | hash
推送現(xiàn)有的 Git 倉庫
cd existing_repo
git remote rename <origin> <old-origin>
git remote add <origin> 倉庫地址
git push -u <origin> --all
git push -u <origin> --tags
git push -u origin <branchname>
## 相當(dāng)于執(zhí)行一下操作
git push origin <branchname1>
git branch --set-upstream-to=origin/<branchname1> <branchname1>
配置 ssh
配置用戶名及郵箱
git config --global user.name <lilith>
git config --global user.email <lilith@163.com>
設(shè)置 ssh
ssh-keygen -t rsa -C <lilith@163.com>
查看
cat ~/.ssh/id_rsa.pub
復(fù)制
clip < ~/.ssh/id_rsa.pub
預(yù)設(shè)全局配置
[user]
name = lilith
email = lilith@163.com
[alias]
it = init
co = config
cl = clone
ad = add
st = status
cm = commit
rb = rebase
mr = merge
ch = checkout
sw = switch
br = branch
rm = remote
rs = reset
fe = fetch
pl = pull
ps = push
unstage = reset HEAD --
last = log -1 HEAD
lp = log --pretty=oneline
lg = log --color --graph --pretty=format:'%Cred%h%Creset%C(yellow)%d%Creset %s %Cgreen%ci'
lg5 = log -5 --color --graph --pretty=format:'%Cred%h%Creset%C(yellow)%d%Creset %s %Cgreen%ci'
lg10 = log -10 --color --graph --pretty=format:'%Cred%h%Creset%C(yellow)%d%Creset %s %Cgreen%ci %C(bold blue)<%an>'
[color "branch"]
current = magenta
local = yellow
remote = cyan
# git status 的顏色配置
[color "status"]
changed = green # 已變更的文件
added = yellow # 已暫存的文件 git add .
untracked = cyan # 未跟蹤的文件 新增的文件
[color "diff"]
meta = yellow
frag = magenta bold
commit = yellow bold
old = red bold
new = green bold
whitespace = red reverse
[color "diff-highlight"]
oldNormal = red bold
oldHighlight = red bold 52
newNormal = green bold
newHighlight = green bold 22
[core]
excludesfile = /Users/hhdd/.gitignore_global
ignorecase = true
pager = less # 分頁器 默認(rèn)是less
[pager]
# branch, log, diff等支持paper
branch = false
[difftool "sourcetree"]
cmd = opendiff \"$LOCAL\" \"$REMOTE\"
path =
[mergetool "sourcetree"]
cmd = /Applications/Sourcetree.app/Contents/Resources/opendiff-w.sh \"$LOCAL\" \"$REMOTE\" -ancestor \"$BASE\" -merge \"$MERGED\"
trustExitCode = true