Git 常用命令
基础使用
配置信息
# 生成秘钥
ssh-keygen -t rsa
git config --global user.name 'webben'
git config --global user.email webben@gmail.com
#忽略权限
git config --global core.filemode false
#window crlf编码 in:crlf out:lf
git config --global core.autocrlf true
# git清除密码
git config --system --unset credential.helper
查看全局变量
git config user.name
#查看项目配置
git config --list
#查看全局配置
git config --global -l
或者
vi ~/.gitconfig
从仓库克隆
git clone https://git@git.com/project.git
查看git状态
展示添加新文件、更新过的文件、暂存区次数
git status
添加新文件
git add '文件名'
查看修改过的内容
git diff
git diff 'filename'
提交文件到暂存区
#提交所有修改文件
git commit -m 'msg' -a
#提交某文件
git commit -m 'msg' 'filename'
撤销一次 commit
git reset --soft HEAD^
撤销一次 push
git reset --hard HEAD^
git push origin --force
或者参考:http://blog.csdn.net/fuchaosz/article/details/52170105
查看暂存区提交描述/说明
git cherry -v
从仓库更新,合并
git pull
提交到远程代码库
#提交到本地
git push origin master
分支使用
查看分支
#查看所有分支
git branch -a
#查看本地分支
git branch
创建远程分支
'创建v1.0分支,内容为master内容'
git push origin master:v1.0
删除远程分支
git push origin --delete v1.0
创建本地分支
git checkout -b v1.0 origin/v1.0
删除本地分支
git branch -d v1.0
# 或者强制删除
git branch -D v1.0
合并分支
git merge dev_v1.12 --no-ff
远程分支与本地分支关联
#–set-upstream-to 在新版本git中已经替代了 –set-upstream
git branch --set-upstream dev origin/dev
# 新版本
git branch --set-upstream-to origin/dev dev
其他
获取远程内容,覆盖本地
git fetch origin
获取暂存区内容,覆盖本地,删除未ad的文件
git checkout -- .
恢复仓库版本
git checkout
查看远程git地址
git remote -v
修改远程git地址
git remote rm origin
git remote -v
git remote add origin git@git.com:project.git
删除没有被track的文件
git clean -df
查看没有被track的文件
git clean -n
标签
查看标签
git tag
打标签
git tag v1.1.1
Git日志
查看在线日志
git log --pretty=oneline
查看文件每行修改纪录
git blame file_name
查看某次提交修改内容
git show <id>
Git submodule
添加submodule
git submodule add 仓库地址 路径
删除 submodule
submodule 的删除稍微麻烦点:
首先,要在“.gitmodules”文件中删除相应配置信息。
然后,执行“git rm –cached ”命令将子模块所在的文件从git中删除。
下载的工程带有submodule
git submodule update --init --recursive
Git配置
打开配置文件
vim ~/.gitconfig
配置文件内容
[user]
email = *****@126.com
name = webben
[core]
quotepath = false
autocrlf = true
#whitespace = cr-at-eol
[gui]
encoding = utf-8
[alias]
co = checkout
ci = commit
br = branch
st = status
pl = pull
plo = pull origin
ps = push
pso = push origin
df = diff
ch = cherry -v
gl = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
one = log --pretty=oneline
bind = branch --set-upstream-to
resetcommit = reset --soft HEAD^
mg = merge --squash
Git log 参数u
%H 提交对象(commit)的完整哈希字串
%h 提交对象的简短哈希字串
%T 树对象(tree)的完整哈希字串
%t 树对象的简短哈希字串
%P 父对象(parent)的完整哈希字串
%p 父对象的简短哈希字串
%an 作者(author)的名字
%ae 作者的电子邮件地址
%ad 作者修订日期(可以用 -date= 选项定制格式)
%ar 作者修订日期,按多久以前的方式显示
%cn 提交者(committer)的名字
%ce 提交者的电子邮件地址
%cd 提交日期
%cr 提交日期,按多久以前的方式显示
%s 提交说明