git的奇特知识点

展示帮助信息

复制代码
git help -g


The common Git guides are:
   attributes          Defining attributes per path
   cli                 Git command-line interface and conventions
   core-tutorial       A Git core tutorial for developers
   cvs-migration       Git for CVS users
   diffcore            Tweaking diff output
   everyday            A useful minimum set of commands for Everyday Git
   glossary            A Git Glossary
   hooks               Hooks used by Git
   ignore              Specifies intentionally untracked files to ignore
   modules             Defining submodule properties
   namespaces          Git namespaces
   repository-layout    Git Repository Layout
   revisions           Specifying revisions and ranges for Git
   tutorial            A tutorial introduction to Git
   tutorial-2          A tutorial introduction to Git: part two
   workflows           An overview of recommended workflows with Git

'git help -a' and 'git help -g' list available subcommands and some concept guides. See 'git help <command>' or 'git help <concept>' to read about a specific subcommand or concept.

回到远程仓库的状态

抛弃本地所有的修改,回到远程仓库的状态。

复制代码
git fetch --all && git reset --hard origin/master

重设第一个 commit

也就是把所有的改动都重新放回工作区,并清空所有的 commit,这样就可以重新提交第一个 commit 了

复制代码
git update-ref -d HEAD

查看冲突文件列表

展示工作区的冲突文件列表

复制代码
git diff --name-only --diff-filter=U

展示工作区和暂存区的不同

输出工作区和暂存区的 different (不同)。

复制代码
git diff

还可以展示本地仓库中任意两个 commit 之间的文件变动:

复制代码
git diff <commit-id> <commit-id>

展示暂存区和最近版本的不同

输出暂存区和本地最近的版本 (commit) 的 different (不同)。

复制代码
git diff --cached

展示暂存区、工作区和最近版本的不同

输出工作区、暂存区 和本地最近的版本 (commit) 的 different (不同)。

复制代码
git diff HEAD

快速切换到上一个分支

复制代码
git checkout -

删除已经合并到 master 的分支

复制代码
git branch --merged master | grep -v '^\*\|  master' | xargs -n 1 git branch -d

清除无效分支

复制代码
git fetch -p

展示本地分支关联远程仓库的情况

复制代码
git branch -vv

重命名本地分支

复制代码
git branch -m <new-branch-name>

回到某个 commit 的状态,并删除后面的 commit

和 revert 的区别:reset 命令会抹去某个 commit id 之后的所有 commit

复制代码
git reset <commit-id>  #默认就是-mixed参数。

git reset --mixed HEAD^  #回退至上个版本,它将重置HEAD到另外一个commit,并且重置暂存区以便和HEAD相匹配,但是也到此为止。工作区不会被更改。

git reset --soft HEAD~3  #回退至三个版本之前,只回退了commit的信息,暂存区和工作区与回退之前保持一致。如果还要提交,直接commit即可  

git reset --hard <commit-id>  #彻底回退到指定commit-id的状态,暂存区和工作区也会变为指定commit-id版本的内容

修改上一个 commit 的描述

如果暂存区有改动,同时也会将暂存区的改动提交到上一个 commit

复制代码
git commit --amend

切换到新的分支不带历史记录

复制代码
git checkout --orphan new_branch

统计每个人增删行数

复制代码
git log --format='%aN' | sort -u | while read name; do echo -en "$name\t"; git log --author="$name" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -; done

查看仓库提交者排名前 5

复制代码
git log --pretty='%aN' | sort | uniq -c | sort -k1 -n -r | head -n 5

贡献值统计

复制代码
git log --pretty='%aN' | sort -u | wc -l

提交数统计

复制代码
git log --oneline | wc -l

添加或修改的代码行数

复制代码
git log --stat|perl -ne 'END { print $c } $c += $1 if /(\d+) insertions/'

查看某段代码是谁写的

复制代码
git blame <file-name>

查看两个星期内的改动

复制代码
git whatchanged --since='2 weeks ago'

把 A 分支的某一个 commit,放到 B 分支上

复制代码
git checkout <branch-name> && git cherry-pick <commit-id>

添加子模块

复制代码
git submodule add <url> <path>

首次克隆仓库与子模块

复制代码
git clone --recursive <url>

更新子模块

复制代码
git submodule update
相关推荐
chhanz5 小时前
git/github入门基操(终端版)
git·github
林晓lx6 小时前
使用Git钩子+ husky + lint语法检查提高前端项目代码质量
前端·git·gitlab·源代码管理
诸葛思颖7 小时前
一个本地 Git 仓库关联多个远程仓库
git
StarShip10 小时前
git commit --amend 之后,想要修改为git commit , 如何处理?
git
dnpao15 小时前
在服务器已有目录中部署 Git 仓库
运维·服务器·git
__Witheart__15 小时前
Git 如何从某个 commit 新建分支
git
Klaus_Wei1 天前
git 高级命令模式典型应用--“同步云端 + 清理垃圾分支”的一键命令
git·git高级用法·git同步云端·git清理垃圾分支
妙娲种子1 天前
配置git/创建第一个智能相册保存快照
git
what_20181 天前
git一个账号在两台电脑登录 出现不同用户名
git
困鲲鲲1 天前
ROS2系列 (13) : 常用Git指令入门(本地Git)
git·ros2