Git 常用命令

img
  • Workspace:工作区。编写代码的区域。
  • Repository:仓库区(或本地仓库)。用来保存 commit,一个 commit,就是工作区的一个历史版本。
  • Index / Stage:暂存区。用来暂存生成 commit 所需的信息,可看作临时的 commit,git add将工作区的指定内容加入暂存区,git commit依照暂存区信息生成 commit,并写入仓库区。
  • Remote:远程仓库。托管到服务器的本地仓库,就成了远程仓库。

一、新建代码库

# 初始化当前目录为 Git 仓库
$ git init

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

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

二、配置

# 查看当前 Git 配置
$ git config -l

# 查看当前 Git 配置,同时显示配置来源
$ git config --show-origin -l

# 查看系统配置(作用于所有用户)
$ git config --system -l

# 查看全局配置(作用于当前用户)
$ git config --global -l

# 查看本地配置(作用于指定仓库)
$ git config --local -l

# 编辑 Git 配置文件
$ git config -e [--global]

# 设置提交代码时的用户信息
$ git config [--global] user.name "[name]"
$ git config [--global] user.email "[email]"

三、增加 / 删除文件

# 添加指定文件到暂存区
$ git add [file]

# 添加指定目录到暂存区,包括子目录
$ git add [dir]

# 添加当前目录的所有更新到暂存区
$ git add .

# 删除工作区文件,并且将这次删除放入暂存区(取消跟踪该文件)
$ git rm [file]

# (递归地)删除指定文件夹及其内容,并且将这次删除放入暂存区
$ git rm -r [dir]

# 将指定文件从暂存区移除(取消跟踪该文件),但该文件会保留在工作区
$ git rm --cached [file]

# (递归地)将指定文件夹及其内容从暂存区移除,但该文件夹及其内容会保留在工作区
$ git rm -r --cached [dir]

# 将所有内容从暂存区移除,但内容会保留在工作区
$ git rm -r --cached .

# 改名文件/文件夹,并且将这次改名放入暂存区
$ git mv [old-name] [new-name]

四、代码提交

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

# 提交暂存区的指定文件到仓库区
$ git commit [file] -m "[message]"

# 把跟踪中的文件暂存,并提交到仓库区
$ git commit -a

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

五、分支

# 列出所有本地分支
$ git branch

# 列出所有远程跟踪分支
$ git branch -r

# 列出所有本地分支和远程跟踪分支
$ git branch -a

# 列出所有本地分支,并显示每个分支的最后一次提交
$ git branch -v

# 新建一个分支,指向当前 commit(HEAD),但依然停留在当前分支
$ git branch [branch-name]

# 新建一个分支,指向当前 commit,并切换到该分支
$ git checkout -b [branch-name]

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

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

# 切换到上一个分支
$ git checkout -

# 合并指定分支到当前分支
$ git merge [branch-name]

# 选择一个 commit,合并进当前分支
$ git cherry-pick [commit]

# 删除本地分支
$ git branch -d [branch-name]

# 删除远程分支
$ git push [remote] --delete [branch-name]
$ git push [remote] :[branch-name]

# 本地分支重命名
$ git branch -m [old-branch-name] [new-branch-name]

# 查看分支跟踪状态
$ git branch -vv

# 新建一个本地分支,与指定的远程跟踪分支建立追踪关系,并切换到该分支
$ git checkout -b [local-branch] [remote]/[remote-branch]

# 新建一个本地分支,与指定的远程跟踪分支建立追踪关系,并切换到该分支,本地分支与远程分支同名
$ git checkout --track [remote]/[branch-name]

# 设置本地指定分支与远程跟踪分支建立追踪关系
$ git branch -u [remote]/[remote-branch] [local-branch]

# 设置本地当前分支与远程跟踪分支建立追踪关系
$ git branch -u [remote]/[remote-branch]

六、标签

# 列出所有 tag
$ git tag

# 匹配指定 tag 并列出
$ git tag -l "v1.8.5*"

# 在当前 commit 新建一个轻量 tag
$ git tag [tag-name]

# 在指定 commit 新建一个轻量 tag
$ git tag [tag-name] [commit]

# 在当前 commit 新建一个附注 tag
$ git tag -a [tag-name] -m "[message]"

# 在指定 commit 新建一个附注 tag
$ git tag -a [tag-name] [commit] -m "[message]"

# 查看 tag 信息
$ git show [tag-name]

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

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

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

# 删除远程 tag
$ git push [remote] --delete [tag-name]
$ git push [remote] :refs/tags/[tag-name]

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

七、查看信息

# 查看状态
$ git status

# 查看简短状态
$ git status -s

# 显示当前分支的版本历史
$ git log

# 显示某个 commit 之前(包括该 commit)的版本历史
$ git log [commit]

# 显示指定文件每一次提交所引入的差异
$ git log -p [file]

# 显示每次 commit 的统计信息
$ git log --stat

# 将每个提交放在一行显示
$ git log --pretty=oneline

# 将每个提交放在一行显示,且仅显示校验和中的前几个字符
$ git log --oneline

# 绘制分支图
$ git log --oneline --graph

# 绘制分支图,且显示各个分支的指向
$ git log --oneline --decorate --graph --all

# 显示过去 5 次提交
$ git log -5

# 显示指定时间之后的提交
$ git log --after="2022-07-01"

# 显示指定时间之前的提交
$ git log --before="2022-07-01"

# 显示指定作者的提交
$ git log --author="HL"

# 仅显示提交说明中包含指定关键字的提交
$ git log --grep [keyword]

# 显示添加或删除了指定关键词的提交
$ git log -S [keyword]

# 显示某个文件的版本历史,包括文件改名
$ git log --follow [file]
$ git whatchanged [file]

# 显示所有提交过的用户,按提交次数排序
$ git shortlog -sn

# 显示指定文件是什么人在什么时间修改过
$ git blame [file]

# 显示两次提交之间的差异
$ git diff [first-commit]...[second-commit]

# 显示不同分支之间的差异
$ git diff [first-branch] [second-branch]

# 显示不同分支之间差异的统计信息
$ git diff --stat [first-branch] [second-branch]

# 显示不同分支之间指定文件的详细差异
git diff [first-branch] [second-branch] [file]

# 显示某次提交的元数据和内容变化
$ git show [commit]

# 显示某次提交发生变化的文件
$ git show --name-only [commit]

# 显示某次提交时,某个文件的内容
$ git show [commit]:[filename]

# 显示引用日志
$ git reflog

八、远程同步

# 显示所有远程仓库
$ git remote -v

# 显示某个远程仓库的信息
$ git remote show [remote]

# 增加一个新的远程仓库,并命名
$ git remote add [name] [url]

# 下载远程仓库的所有变动到本地仓库
$ git fetch [remote]

# 下载特定分支的更新
$ git fetch [remote] [branch]

# 下载远程指定分支的变化,并与本地指定分支合并
$ git pull [remote] [remote-branch]:[local-branch]

# 下载远程分支的变化,并与本地当前分支合并
$ git pull [remote] [branch]

# 上传本地指定分支到远程仓库,不建立跟踪关系
$ git push [remote] [branch]

# 上传本地指定分支到远程仓库,并建立跟踪关系
$ git push -u [remote] [remote-branch]

# 推送所有分支到远程仓库
$ git push [remote] --all

九、撤销

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

# 恢复某个 commit 的指定文件到暂存区和工作区
$ git checkout [commit] [file]

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

# 重置暂存区的内容,与 HEAD 保持一致,但工作区不变
# --mixed、HEAD 可省
$ git reset [--mixed] [HEAD]

# 重置当前分支的指针为指定 commit,同时重置暂存区,但工作区不变
# --mixed 可省;commit 省略时默认为 HEAD;file 省略时重置暂存区所有内容
$ git reset [--mixed] [file] [commit]

# 重置暂存区与工作区,与 HEAD 保持一致
$ git reset --hard [HEAD]

# 重置当前分支的 HEAD 为指定 commit,同时重置暂存区和工作区,与指定 commit 一致
$ git reset --hard [commit]

# 重置当前 HEAD 为指定 commit,但保持暂存区和工作区不变
$ git reset --soft [commit]

# 通过新建一个 commit 来回到指定 commit,但工作区不变
$ git revert [commit]

# 暂时将未提交的变化移除,稍后再移入
$ git stash
$ git stash pop

ref: 常用 Git 命令清单、《Pro Git》

posted @ 2022-03-27 20:44  Higurashi-kagome  阅读(15)  评论(0编辑  收藏  举报