Git 初始设置及常用命令
git示意图
资料和参考
- Visual Git Cheat Sheet
- Pro Git book
- 廖雪峰的 Git教程
- 设置SSH, 参考 GitHub Help - SSH
- 设置GPG, 参考 GitHub Help - GPG
一些理解
- git的核心理解为指针即可, 包括
workspace
,index
,commitHash
repository
是基于commitHash
管理版本的.HEAD
,HEAD^1
,HEAD~3
,branch
,tags
都是commitHash
的别名, 便于人们记忆和理解.- 可以基于
git reset
来检测是否完全理解git基于指针的设计思路. HEAD^1
基于父节点,HEAD~1
基于层次. 单层结构下没有差别. 复杂多层结构建议直接用commitHash
A --------------------- A = = A^0 |
- git的命令, 常见格式为
git diff p1 p2 -- file
. 意为, 比较p1
与p2
两处指定file的区别p1
缺省指向workspace,p2
缺省指向index.--
接文件或目录, 名字无歧义时可以省去--
. (如 git checkout name 就可能有歧义, branch OR file?).
表示所有的文件, 如git add .
- 学会查看帮助, 加上
-h
即可. 如git checkout -h
[]
表示可选项,<>
表示必填项
初始安装
- 新建git仓库
# Create local repository
# put ".gitignore" to project root direct.
git init # create local repository
git add README.md # add somefiles
git commit -m "first commit" # commit to local repository
# link with remote repository
git remote add origin url # add remote repository, <url> like https://... OR ssh://...
git push -u origin master # -u, 指定默认远程主机为 origin
# Clone existing repository
git clone url # <url> like https://... OR ssh://...
# more cmd about config
git config --list # show config
git config -e # edit local config file
git config -e --global # edit global config file
- 使用命令设置全局参数. (可跳过, 建议使用
git config -e --global
)# global setting
git config --global user.name "Your Name"
git config --global user.email "email@example.com"
# alise setting
git config --global alias.a 'add'
git config --global alias.aa 'add .'
git config --global alias.b 'branch'
git config --global alias.bb 'branch -a -v'
git config --global alias.co 'checkout'
git config --global alias.cm 'commit -m'
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
git config --global alias.ll "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit -10"
git config --global alias.st 'status'
git config --global alias.diffname 'diff --name-status'
# set diff color
git config --global color.diff.old "red normal bold"
git config --global color.diff.new "green normal bold"
# solve the warning, LF will be replaced by CRLF
git config --global core.autocrlf false
git config --global core.safecrlf false
# set difftool mergetool (need p4merge)
git config --global diff.tool p4merge
git config --global difftool.p4merge.cmd '"D:\Program\Perforce\p4merge.exe" "$LOCAL" "$REMOTE"'
git config --global difftool.prompt false
git config --global merge.tool p4merge
git config --global mergetool.p4merge.cmd '"D:\Program\Perforce\p4merge.exe" "$PWD/$BASE" "$PWD/$REMOTE" "$PWD/$LOCAL" "$PWD/$MERGED"'
git config --global mergetool.p4merge.trustExitCode false
git config --global mergetool.keepBackup false
- 使用
git config -e --global
打开全局配置文件, 设置全局参数. - 安装 p4merge, 用于支持
difftool
和mergetool
- 安装位置以
D:\Program\Perforce\p4merge.exe
为例[user]
name = https://draapho.github.io/
email = draapho@gmail.com
[alias]
a = add
aa = add .
b = branch
bb = branch -a -v
co = checkout
cm = commit -m
lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
ll = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit -10
st = status
diffname = diff --name-status
[color "diff"]
old = red normal bold
new = green normal bold
[core]
autocrlf = false
safecrlf = false
[diff]
tool = p4merge
[difftool "p4merge"]
cmd = \"D:\\Program\\Perforce\\p4merge.exe\" \"$LOCAL\" \"$REMOTE\"
[difftool]
prompt = false
[merge]
tool = p4merge
[mergetool "p4merge"]
cmd = \"D:\\Program\\Perforce\\p4merge.exe\" \"$PWD/$BASE\" \"$PWD/$REMOTE\" \"$PWD/$LOCAL\" \"$PWD/$MERGED\"
trustExitCode = false
[mergetool]
keepBackup = false
常用指令
# 查看信息 |
原创于 DRA&PHO