根据官方文档整理,以及相关示例,

官方文档: https://git-scm.com/book/en/v2

 

 

Git 命令 – 设置与配置

Git config命令

设置用户名,邮箱

$ git config –global user.name “John Doe”

$ git config –global user.email johndoe@example.com

列出所有配置

$ git config –list

 

别名

$ git config –global alias.co checkout

$ git config –global alias.br branch

$ git config –global alias.ci commit

$ git config –global alias.st status

$ git config –global alias.unstage ‘reset HEAD –‘

$ git config –global alias.last ‘log -1 HEAD’

$ 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”

pull默认使用选项 –rebase

$ git config –global pull.rebase true

 

凭证存储

$ git config –global credential.helper cache

$ git config –global credential.helper store –file ~/.my-credentials

 

配置默认编辑器

$ git config –global core.editor emacs

配置commit默认信息

$ git config –global commit.template ~/.gitmessage.txt

默认分页器(more,【less】)

$ git config –global core.pager ”

windows 转换回车换行符CR,LF —> LF

$ git config –global core.autocrlf true false(取消)

 

Git 命令 – 获取与创建项目

git init 命令

创建一个初始化仓库

$ git init  默认分支 master

创建裸仓库(服务器没有工作目录)

$ git init –bare –shared #shared 自动修改仓库目录权限为可写

初始化仓库目录

description 文件仅供 GitWeb 程序使用

config 文件包含项目特有的配置选项

info 目录包含一个全局性排除(global exclude)文件

.gitignore 文件中的忽略模式(ignored patterns)

hooks 目录包含客户端或服务端的钩子脚本

HEAD 文件、(尚待创建的)index 文件,和 objects 目录、refs 目录。

objects 目录存储所有数据内容refs 目录存储指向数据(分支)的提交对象的指针;

HEAD 文件指示目前被检出的分支;index 文件保存暂存区信息

 

git clone命令

克隆现有仓库

$ git clone https://github.com/libgit2/libgit2 mylibgit

解包

$ git clone repo.bundle repo

克隆初始化更新含子模块的仓库

$ git clone –recursive https://github.com/chaconinc/MainProject

 

Git 命令 – 快照基础

git add 命令

跟踪新文件

$ git add README

交互式暂存

$ git add -i

$ git add –interactive

 

git status 命令

检查当前文件状态

$ git status

状态简览

$ git status -s

 M README –>

MM Rakefile –>右M修改未暂存左M 修改并暂存

A  lib/git.rb –>新添加到暂存区中

M  lib/simplegit.rb  –>

?? LICENSE.txt  –>新添加未跟踪

 

git diff 命令

查看修改(比较的是工作目录中当前文件和暂存区域快照之间的差异,

也就是修改之后还没有暂存起来的变化内容。)

$ git diff

查看已暂存将要提交

$ git diff –cached

$ git diff –staged

提交准侧(检查空白错误)

$ git diff –check

三点语法比较分支

$ git diff master…contrib

显示合并后状态

$ git rerere diff

 

git reset 命令

取消暂存的文件

$ git reset HEAD CONTRIBUTING.md

https://git-scm.com/book/zh/v2/Git-%E5%B7%A5%E5%85%B7-%E9%87%8D%E7%BD%AE%E6%8F%AD%E5%AF%86#r_git_reset

撤销合并

$ git reset –hard HEAD~

 

git rm 命令

移除文件

$ git rm PROJECTS.md

仓库中删除不继续跟踪,磁盘保留

$ git rm –cached README

 

git mv 命令

移动文件改名

$ git mv file_from file_to

 

git clean 命令

清除工作目录演戏

默认情况下,git clean 命令 只会移除没有忽略的未跟踪文件。

$ git clean -d -n

强制清除

$ git clean -f -d

 

 

Git 命令 – 分支与合并

git branch 命令

创建分支

$ git branch testing

查看分支

$ git branch

查看每个分支的最后一次提交

$ git branch -v

查看已合并到当前分支

$ git branch –merged

查看未合并分支

$ git branch –no-merged

删除分支(未合并分支删除会失效)

$ git branch -d testing

强制删除分支

$ git branch -D testing

修改跟踪分支

$ git branch -u origin/serverfix

查看所有跟踪分支

$ git branch -vv

 

git checkout 命令

分支切换

$ git checkout testing

跟踪远程分支

$ git checkout –track origin/serverfix

跟踪远程分支并设置本地名称

$ git checkout -b sf origin/serverfix

 

git merge  命令

合并分支

$ git merge <branch>

退出合并

$ git merge –abort

合并忽略空白

$ git merge -Xignore-space-change whitespace

 

git mergetool 命令外部的合并帮助工具。

 

git log 命令只会移除没有忽略的未跟踪文件。

常用

$ git log –pretty=oneline –graph

$ git log –graph –oneline –decorate –all

https://git-scm.com/book/zh/v2/Git-%E5%9F%BA%E7%A1%80-%E6%9F%A5%E7%9C%8B%E6%8F%90%E4%BA%A4%E5%8E%86%E5%8F%B2#r_viewing_history.

分支所在对象

$ git log –oneline –decorate

在 experiment 分支中而不在 master 分支中的提交”

$ git log master..experiment

$ git log refA..refB

$ git log ^refA refB

$ git log refB –not refA

 

 

git stash 命令

储藏

$ git stash

查看

$ git stash list

恢复

$ git stash apply

 

https://git-scm.com/book/zh/v2/Git-%E5%B7%A5%E5%85%B7-%E5%82%A8%E8%97%8F%E4%B8%8E%E6%B8%85%E7%90%86#r_git_stashing

 

git tag 命令

打标签

$ git tag -a v1.4 -m ‘my version 1.4’

查看

$ git show v1.4

后期根据提交打标签

$ git tag -a v1.2 9fceb02

推送标签

$ git push origin v1.5

全部推送

$ git push origin –tags

删除标签

$ git tag -d v1.4

 

 

Git 命令 – 项目分享与更新

git fetch 命令

从远程仓库拉取

$ git fetch [remote-name]

 

git pull 命令

git pull 命令基本上就是 git fetch 和 git merge 命令的组合体

$ git pull –rebase  变基合并

 

git push 命令

推送到远程仓库

$ git push [remote-name] [branch-name]

删除远程分支

$ git push origin –delete serverfix

 

git remote 命令

查看远程仓库

$ git remote -v

$ git remote show origin

添加远程仓库

$ git remote add pb https://github.com/paulboone/ticgit

远程仓库重命名

$ git remote rename pb paul

移除远程仓库

$ git remote rm paul

 

git archive 命令

创建快照归档文件

$ git archive master –prefix=’project/’ | gzip > `git describe master`.tar.gz

 

git submodule 命令

添加子模块

$ git submodule add https://github.com/chaconinc/DbConnector

 

 

Git 命令 – 检查与比较

git show 命令

查看标签信息

$ git show v1.4

查看一次提交

$ git show 1c002d

$ git show test

 

git shortlog 命令

制作提交简报

$ git shortlog –no-merges master –not v1.0.1

 

git describe 命令

生成一个构建号

$ git describe master

 

Git 命令 – 调试

git bisect 命令

https://git-scm.com/book/zh/v2/Git-%E5%B7%A5%E5%85%B7-%E4%BD%BF%E7%94%A8-Git-%E8%B0%83%E8%AF%95#r_binary_search

git blame 命令 (文件标注)

-L 选项来限制输出范围在第12至22行:

$ git blame -L 12,22 simplegit.rb

 

git grep 命令

https://git-scm.com/book/zh/v2/Git-%E5%B7%A5%E5%85%B7-%E6%90%9C%E7%B4%A2#r_git_grep

 

 

Git 命令 – 补丁

git cherry-pick 命令

变基与拣选工作流

$ git cherry-pick e43a6fd3

 

git rebase 命令

变基

$ git rebase master

截取特性分支上的另一个特性分支,然后变基到其他分支

$ git rebase –onto master server client

以上命令的意思是:“取出 client 分支,找出处于 client 分支和 server 分支的共同祖先之后的修改,

然后把它们在 master 分支上重放一遍”

变基

$ git rebase –onto 622e88 9c68fdc

 

git revert 命令

还原提交

$ git revert -m 1 HEAD

 

Git邮件(不常用)

$ git apply  应用补丁

$ git am  应用补丁

$ git format-patch  生成补丁

$ git imap-send 上传发送补丁

$ git send-email  邮件发送补丁

$ git request-pull  生成推送消息

https://git-scm.com/book/zh/v2/%E9%99%84%E5%BD%95-C%3A-Git-%E5%91%BD%E4%BB%A4-%E9%82%AE%E4%BB%B6

 

 

Git 命令 – 外部系统

git svn 命令

克隆subvers 仓库

$ git svn clone file:///tmp/test-svn -T trunk -b branches -t tags

$ git svn clone file:///tmp/test-svn -s

提交到subversion 服务器

$ git svn dcommit

https://git-scm.com/book/zh/v2/Git-%E4%B8%8E%E5%85%B6%E4%BB%96%E7%B3%BB%E7%BB%9F-%E4%BD%9C%E4%B8%BA%E5%AE%A2%E6%88%B7%E7%AB%AF%E7%9A%84-Git#r_git_svn

 

git fast-import  命令

从其他版本控制系统导入

 

Git 命令 – 管理

https://git-scm.com/book/zh/v2/%E9%99%84%E5%BD%95-C%3A-Git-%E5%91%BD%E4%BB%A4-%E7%AE%A1%E7%90%86

 

Git 命令 – 底层命令

https://git-scm.com/book/zh/v2/%E9%99%84%E5%BD%95-C%3A-Git-%E5%91%BD%E4%BB%A4-%E5%BA%95%E5%B1%82%E5%91%BD%E4%BB%A4

posted on 2019-06-12 14:22 王峰i 阅读() 评论() 编辑 收藏
版权声明:本文为wf-linux原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/wf-linux/p/11009396.html