关于git:Git-常用命令参考手册

1次阅读

共计 5389 个字符,预计需要花费 14 分钟才能阅读完成。

尽管每天都在应用Git,然而有些命令太久不应用,还是会遗记,所以这篇笔记的目标就是整顿那些Git 常用命令。

根底配置

Git 的设置文件为.gitconfig,它能够在用户主目录下(全局配置),也能够在我的项目目录下(我的项目配置)。

# 查看全局配置列表
$ git config --global --list
# 查看部分配置列表
$ git config --local --list

# 设置全局用户名 / 邮箱
$ git config --global user.name "yourName"
$ git config --global user.email "example@example.com"

# 设置本地当前工作区仓库用户名 / 邮箱
$ git config --local user.name "yourName"
$ git config --local user.email "example@example.com"

# 将默认文本编辑器设置为 emacs/vim
$ git config --global core.editor emacs/vim

# 编辑以后仓库的配置文件
$ git config -e  # 等价与 vim .git/config
# 编辑全局配置文件
$ git config --global -e

命令别名配置

# 增加别名 git st = git status
$ git config --global alias.st status

# 删除 st 别名
$ git config --global --unset alias.st

# 执行外部命令, 只有在后面加 ! 即可
$ git config --global alias.st '!echo hello';

代理配置

如果想晓得对于 Git 配置代理的更多信息,能够查阅这篇笔记。

# 配置 HTTP/HTTPS 代理
$ git config --global https.proxy  http://127.0.0.1:1087
$ git config --global http.proxy  http://127.0.0.1:1087

# 查看
$ git config --global --get http.proxy
$ git config --global --get https.proxy

# 勾销代理
$ git config --global --unset http.proxy
$ git config --global --unset https.proxy

生成 SSHKey

对于如何配置ssh config 能够查阅这篇笔记。

# 将 ssh key 生成在默认下,也就是 `~/.ssh/id_rsa`。$ ssh-keygen -t rsa -C "youremail"

# 将 ssh key 生成在指定门路下的指定文件名中
$ ssh-keygen -t rsa  -f ~/.ssh/id_rsa_bitbucket -C "youremail"

筹备工作

# 在当前目录新建一个 Git 代码库
$ git init

# 新建一个目录,将其初始化为 Git 代码库
$ git init [project-name]

# 下载一个我的项目和它的整个代码历史
$ git clone [url] [project-name]

# 浅克隆, 历史记录只克隆最初一条, 缩小克隆工夫
$ git clone --depth=1 https://github.com/0xAiKang/Note.git

根底操作

根底操作中的命令都是日常应用频率十分高的。

文件状态

# 查看工作区状态
$ git status

# 列出没有被 .gitignore 疏忽的文件列表
$ git status --ignored

# 列出没有被 .gitignore 疏忽的文件列表
$ git ls-files

文件操作

# 暂存所有
$ git add -A

# 暂存某个文件
$ git add ./README.md

# 增加当前目录的所有文件到暂存区    
$ git add .

# 暂存一系列文件
$ git add 1.txt 2.txt ...

# 从暂存区中删除文件(git add 的反向操作)$ git rm [file] 

# 暂存区、工作区一起删除
$ git rm -f [file]

# 进行追踪指定文件,但该文件会保留在工作区
$ git rm --cached [file]

查看文件改变

# 查看所有文件改变
$ git diff

# 查看具体文件的改变
$ git diff README.md

# 查看指定 commit-id 改变内容
$ git diff [commit-id]

# 比照工作区和版本库里的最新版本有什么区别
$ git diff HEAD --[file-name]

# 查看某个文件的历史批改记录
$ git log README.md
$ git show [commit-id] README.md

撤销与回滚

# 复原暂存区的指定文件到工作区
$ git checkout [file]

# 复原暂存区的所有文件到工作区
$ git checkout .

# 重置暂存区与工作区,与上一次 commit 保持一致
$ git reset --hard

# 回滚上一个版本
$ git reset --hard HEAD^

# 回退到指定版本(会重置暂存区与工作区)$ git reset --hard [commit-id]

# 回退到指定版本(不会重置暂存区与工作区,会回到该版本的暂存状态)$ git reset --soft [commit-id]

提交

# 提交暂存区到本地仓库
$ git commit -m [message]

# 提交暂存区的指定文件到本地仓库
git commit README.md -m [message]

# 提交并显示 diff 变动
git commit -v

# 重写上一次的提交
# 如果代码没有任何新变动,则用来改写上一次 commit 的提交信息
$ git commit --amend -m [message]

日志

# 查看残缺历史提交记录
$ git log

# 查看前 n 条记录
$ git log -n

# 以图形形式查看残缺历史提交记录
$ git log --graph --pretty=oneline --abbrev-commit

# 通过 commit log 进行搜寻
$ git log -i --grep="fire bug"

# 列出提交者奉献数量, 只会打印作者和奉献数量
$ git shortlog -sn

# 以提交奉献数量排序并打印出信息
$ git shortlog -n

# 采纳邮箱格式化的形式进行查看贡献度
$ git shortlog -e

分支

# 查看本地分支
git branch

# 查看所有分支
git branch -a

# 查看本地分支所关联的近程分支
git branch -vv

# 查看本地 master 分支创立工夫
git reflog show --date=iso master

# 新建一个分支,但仍然停留在以后分支
$ git branch [branch-name]

# 新建一个分支,并切换到该分支
$ git checkout -b [branch]

# 新建一个分支,指向指定 commit
$ git branch [branch] [commit-id]

# 新建一个分支,与指定的近程分支建设追踪关系
$ git branch --track [branch] [remote-branch]

# 切换到指定分支,并更新工作区
$ git checkout [branch-name]

# 建设追踪关系,在现有分支与指定的近程分支之间
$ git branch --set-upstream [branch] [remote-branch]

# 合并指定分支到以后分支
$ git merge [branch]

# 抉择一个 commit,合并进以后分支
$ git cherry-pick [commit-id]

# 删除指定分支
$ git branch -d [branch-name]

# 强制删除指定分支
$ git branch -D [branch-name]

# 删除近程分支
$ git push origin --delete [branch-name]
$ git branch -dr [remote/branch]

近程仓库治理

# 查看近程仓库(默认是 origin,这是 git 会应用的默认名称)$ git remote 

# 指定 -v, 查看所有近程仓库地址
$ git remote -v

# 增加一个新的近程仓库
$ git remote add [origin-name] https://github.com/0xAiKang/Note.git

# 查看指定近程仓库的详情信息
$ git remote show [origin-name]

# 重命名近程仓库
$ git remote rename [old-name] [new-name]

# 移除近程仓库
$ git remote remove [origin-name]

Push

# 默认推送以后分支
$ git push

# 推送内容到主分支,并建设追踪关系
$ git push -u origin master

# 将本地分支推送到指定近程分支,(本地分支: 近程分支)$ git push origin [branch]:[branch]

# 强行推送以后分支到近程仓库,即便有抵触
$ git push -f

Pull

# 取回默认近程仓库的变动,并主动与本地分支合并
$ git pull

# 取回指定近程仓库的变动,并主动与本地指定分支合并(近程分支名: 本地分支名)$ git pull [remote] [branch]:[branch]

# 取回指定近程仓库的变动,并主动与本地以后分支合并
$ git pull origin master

# 取回近程仓库的所有变动,然而不会主动与本地以后分支合并
$ git fetch

进阶操作

进阶操作中的命令是一些很实用,但可能不常应用,所以把它们独自拎进去。

cherry-pick

# 抉择一个 commit,合并进以后分支
$ git cherry-pick [commit-id]

# 保留原有作者信息进行提交
$ git cherry-pick -x [commit-id]

Stash

# 将以后的工作区暗藏
$ git stash

# 复原暗藏的工作区,并将此次暗藏记录从暗藏列表中移出
$ git stash pop

# 复原暗藏的工作区,保留此次暗藏记录
$ git stash apply

# 查看以后暗藏列表
$ git stash list

Blame

git blame 用于查看某个文件的批改历史记录是哪个作者进行了改变。

# 查看 README.md 文件的批改历史记录,包含工夫、作者以及内容
$ git blame README.md

# 查看谁改变了 README.md 文件的 11 行 -12 行
$ git blame -L 11,12 README.md

# 查看谁改变了 README.md 文件 11 行当前
$ git blame -L 11 README.md

标签

# 列出本地所有标签
git tag

# 新建一个 tag 在以后 commit
$ git tag [tag]

# 新建一个 tag 在指定 commit
$ git tag [tag] [commit]

# 删除本地 tag
$ git tag -d [tag]

# 删除近程 tag
$ git push origin :refs/tags/[tagName]

# 列出近程所有标签
$ git ls-remote --tags origin

# 创立带有附注标签
$ git tag -a v1.1.0 -m "标签形容"

# 查看本地 tag 信息
$ git show [tag]

# 提交指定 tag
$ git push [remote] [tag]

# 提交所有 tag
$ git push [remote] --tags

# 新建一个分支,指向某个 tag
$ git checkout -b [branch] [tag]

Git ProTips

Git ProTips 则是整顿的一些 Git 的奇技淫巧。

# 通过应用别名,优化 git log 输入,这里另外提供几种模式, 能够抉择喜爱的一种进行别名配置
$ git config --global alias.lg "log --graph --pretty=format:'%Cred%h - %Cgreen[%an]%Creset -%C(yellow)%d%Creset %s %C(yellow)<%cr>%Creset'--abbrev-commit --date=relative"

$ git config --global alias.his "log --graph --decorate --oneline --pretty=format:'%Creset %s %C(magenta)in %Cred%h %C(magenta)commited by %Cgreen%cn %C(magenta)on %C(yellow) %cd %C(magenta)from %Creset %C(yellow)%d'--abbrev-commit --date=format:'%Y-%m-%d %H:%M:%S'"$ git config --global alias.hist"log --graph --decorate --oneline --pretty=format:'%Cred%h - %C(bold white) %s %Creset %C(yellow)%d  %C(cyan) <%cd> %Creset %Cgreen(%cn)' --abbrev-commit --date=format:'%Y-%m-%d %H:%M:%S'"

$ 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.lg "log --pretty=format:'%h - %an, %ar : %s' "

参考链接

  • Git 常用命令整顿
  • 罕用 Git 命令清单
正文完
 0