UP | HOME

Git Commands

Table of Contents

在Linux环境下,可以通过git –help、git reset –help来查询git命令的具体参数和说明,更详细的还可以适用man git、man git reset 来查询。切忌,在Linux下一定要多适用–help和man命令。

创建

Command Description
git init 将当前目录构建未一个git代码库
git init <projectname> 新建一个目录,并将其初始化为git代码库
git clone <url> 下载一个项目以及真个代码历史记录

配置

git的设置文件为.gitconfig,它既可以在用户主目录下(作为该用户的全局配置),也可以在项目目录下作为项目配置。

Command Description
git config –list 显示当前git的配置
git config -e [–global] 编辑git配置文件(默认是使用$EDITOR)
git config [–global] user.name "[name]" 设置提交代码时用户名
git config [–global] user.name "[email address]" 设置提交代码时用户邮箱

增加/删除文件

Command Description
git status 显示工作路径下全部已修改的文件
git diff 显示与上次提交版本文件的不同
git add . 把当前所有修改添加到下次提交中
git add -p <file> 添加每个变化前都需要确定,对于同一个文件的所处变化,可以实现分次提交
git add <file1> <file2> 指定多个文件的修改添加到暂存区
git add <dirname> 指定目录添加到暂存区,包括子目录
git rm <file1> <file2> 删除工作区文件,并将这次删除放入暂存区
git rm –cached <file> 停止追踪指定文件,但该文件会保留在工作区
git mv <file-origional> <file-renameed> 改名文件,并将这个改名放入暂存区

代码提交

Command Description
git commit -m <message> 提交暂存区到仓库区
git commit <file1> <file2> … -m <message> 提交暂存区的指定文件到仓库区
git commit -a 提交工作区自上次commit之后的所有变化,直接到仓库区
git commit -v 提交时显示所有的diff信息
git commit –amend -m <message> 适用一次新的commit,替代上次提交,如果代码没有任何变化,则用来改写上次提交的message
git commit –amend <file1> <file2> … 重做上次提交,并包括指定文件的新变化

提交历史

Command Description
git log 从最新提交开始显示所有的提交记录
git log –stat 显示所有提交历史,以及每次提交发生变更的文件
git log -S <keyword> 搜索提交历史,根据关键词
git log <tag> HEAD –pretty=format:%s 显示某个提交之后的所有变动,每个提交占据一行
git log <tag> HEAD –grep feature 显示某个提交之后的所有变动,其”提交说明“必须符合搜索提交
git log –follow <file> 显示某个文件的版本历史,包括文件改名
git whatchanged <file> 同上
git log -p <file> 显示指定文件相关的每一次提交
git log -5 –pretty –oneline 显示过去5次提交
git shortlog -sn 系那是所有提交过的用户,按提交次数排序
git blame <file> 谁,在什么时间,修改了什么
git diff 显示暂存区和工作区的差异
git diff –cached <file> 显示暂存区和上一次提交的差异
git diff HEAD 显示工作区与当前分支最新提交之间的差异
git diff <first-commmit> <second-commit> 显示两次提交之间的差异
git dff –shortstat "@{0 day ago}" 显示今天你写了多少代码
git show commit 显示某次提交的元数据和内容变化
git show –name-only <commit> 显示某次提交发生变化的文件
git show <commmit>:<filename> 显示某次提交时,某个文件的内容
git reflog 显示当前分支的最近几次提交

分支

Command Description
git branch 列出本地所有的分支
git branch -r 列出远程所有分支
git branch -a 列出本地与远程的所有分支
git branch <branch-name> 新建一个分支,但依旧停留在当前分支
git checkout -b <branch-name> 新建一个分支并切换到该分支
git branch <branch> <commit> 新建一个分支,指向指定commit
git branch –track <branch> <remote-branch> 新建一个分支,与指定的远程分支建立追踪关系
git checkout <branch-name> 切换到指定分支,并更新工作区
git checkout - 切换到上一个分支
git branch –set-upstream <branch> <remote-branch> 建立追踪关系,在现有分支与指定的远程分支之间
git merge <branch> 合并指定分支当前分支
git branch -d <branch-name> 删除分支
git cherry-pick <commit> 选择一个commit,合并进当前分支
git push origin –delect <branch-name> 删除远程分支
git branch -dr <remote/branch> 删除远程分支

标签

Command Description
git tag 列出所有标签
git tag <tag> 新建一个tag在当前commit
git tag <tag> <commit> 在指定的commit上创建新标签
git tag -d <tag> 删除本地tag
git push origin :refs/tags/<tagname> 删除远程tag
git show <tag> 查看tag信息
git push <remote> <tag> 提交指定标签
git push <remote> –tags 提交所有tag
git checkout -b <branch> <tag> 新建一个分支,指定某个tag

更新与发布

Command Description
git remote -v 列出当前配置的远程端
git remote show <remote> 显示远程端信息
git remote add <shortname> <ur> 添加新的远程端
git fetch <remote> 下载远程端的所有改动到本地(不会自动合并到当前)
git pull <remote> <branch> 下载远程端的所有改动到本地(自动合并到当前)
git push <remote> <branch> 将本地版本发布到远程端
git branch -dr <remote/branch> 删除远程端分支
git push –tags 发布标签

合并与重置

Command Description
git merge <branch> 将分支合并到当前
git rebase <branch> 将当前版本重置到分支中(请勿重置已发布的提交)
git rebase –abort 推出重置
git rebase –continue 解决冲突后继续重置
git mergetool 适用配置好的合并工具去解决冲突
git add <resolved-file> 在编辑器中手动解决冲突后标记文件为已解决冲突
git rm <resolved-file>  

撤销

Command Description
git checkout <file> 恢复暂存区的指定文件到工作区
git checkout <commit> <file> 恢复某个提交的指定文件到暂存区和工作区
git chekcout . 恢复暂存区的所有文件到工作区
git reset <file> 重置暂存区的指定文件,与上一次提交保持一致,但工作区不变
git reset –hard 重置暂存区与工作区与上次提交保持一致
git reset <commit> 重置当前分支的指针为指定提交,同时重置暂存区,但工作区不变
git reset –hard <commit> 重置当前分支的HEAD为指定提交,同时重置暂存区和工作区,与指定提交保存一致
git reset –keep <commit> 将HEAD重置到上一次提交的版本并保留未提交的本地修改
git revert <commit> 重置一个提交(通过创建一个截然不同的新提交)
git stash 暂时将未提交的变化移除,稍后移入

Created: 2018-01-15 一 16:38

Validate