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
相关推荐
CC码码3 小时前
管理你的多个 Git 密钥(多平台多账号)
git·gitlab·github
CC码码3 小时前
管理你的多个 Git 密钥(单平台多账号)
git·gitlab·github
大卫小东(Sheldon)3 小时前
GIM 1.5发布了! 支持Windows系统了
git·ai·rust
flying jiang3 小时前
将大仓库拆分为多个小仓库
git
李boyang10 天前
Git(四):远程操作
git
荻野泽溪10 天前
Git新建分支并同步到远程
git
漫步企鹅10 天前
【Git】新建一个远程分支的常规操作
git·新建远程分支
潇-xiao10 天前
Linux下的版本控制器Git(15)
linux·笔记·git
@昵称不存在10 天前
Git学习
git·学习
pe7er10 天前
⛔️⛔️⛔️丢弃本地commit,强制采用远端代码
git