27

走在前沿的弄潮儿,怎能不会Git的那些奇技淫巧

 5 years ago
source link: https://www.jiqizhixin.com/articles/2019-04-21-7?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Git 是世界上最好用的代码版本控制工具,没有之一;那么作为走在前沿的 ML 开发者,岂能不擅长高端大气上档次的 Git?

本文介绍了七十多种常用的 Git 命令与技巧,项目作者削微寒在开源工作上也做了非常多的贡献。目前 Git 奇技淫巧项目已经获得了 6.8K 的 Star,他还构建了另一个 1.1W+ 的开源项目集锦 HellowGitHub。

Git 是一个「分布式版本管理工具」,简单的理解版本管理工具:大家在写东西的时候都用过「回撤」这个功能,但是回撤只能回撤几步,假如想要找回我三天之前的修改,光用「回撤」是找不回来的。而「版本管理工具」能记录每次的修改,只要提交到版本仓库,你就可以找到之前任何时刻的状态(文本状态)。

对于 ML 开发者而言,版本控制也是必不可少的。如果我们写了个初版模型,并上传至 GitHub 私人或公共仓库,那么每当我们有一些修改,都可以通过 Git 上传,它会自动控制这些历史版本。如果说后面发现前面的某些 超参数 或结构非常好,那么查看历史版本也是很好的。

对于其它开源工作的管理,Git 就更好用了。因为它是一种分布式版本控制系统,并没有一个真正的「中心服务器」,所以不同的电脑间有相同的版本库,本地版本库损坏了就直接从其它完好的版本库复制。这种 Git,才是协作开发代码的最优方法,因此基于 Git 的 GitHub 才成为开源世界中不可获取的部分。

下面的内容就是列举了常用的 Git 命令和一些小技巧,读者可以在原 GitHub 项目上进行 "页面内查找",也可以在微信文章页面上选择 "搜索页面内容"。

开卷必读

如果之前未使用过 Git,可以学习 Git 小白教程入门: http://rogerdudler.github.io/git-guide/index.zh.html

1. 一定要先测试命令的效果后,再用于工作环境中,以防造成不能弥补的后果!

2. 所有的命令都在 git version 2.7.4 (Apple Git-66) 下测试通过

3. 统一概念:

  • 工作区:改动(增删文件和内容)

  • 暂存区:输入命令:git add 改动的文件名,此次改动就放到了 『暂存区』

  • 本地仓库 (简称:本地):输入命令:git commit 此次修改的描述,此次改动就放到了 』本地仓库』,每个 commit,我叫它为一个 『版本』。

  • 远程仓库 (简称:远程):输入命令:git push 远程仓库,此次改动就放到了 『远程仓库』(GitHub 等)

  • commit-id:输出命令:git log,最上面那行 commit xxxxxx,后面的字符串就是 commit-id

4. 如果喜欢这个项目,欢迎 Star、提交 Pr、反馈问题

先给一张各种命令的详解图,更详细的内容可查阅后文的命令解释:

VnyQrae.jpg!web

为了方便查阅,这里先以目录的形式列举各种操作的列表:

  • 展示帮助信息

  • 回到远程仓库的状态

  • 重设第一个 commit

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

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

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

  • 快速切换到上一个分支

  • 删除已经合并到 master 的分支

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

  • 关联远程分支

  • 列出所有远程分支

  • 列出本地和远程分支

  • 创建并切换到本地分支

  • 从远程分支中创建并切换到本地分支

  • 删除本地分支

  • 删除远程分支

  • 重命名本地分支

  • 查看标签

  • 查看标签详细信息

  • 本地创建标签

  • 推送标签到远程仓库

  • 删除本地标签

  • 删除远程标签

  • 切回到某个标签

  • 放弃工作区的修改

  • 恢复删除的文件

  • 以新增一个 commit 的方式还原某一个 commit 的修改

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

  • 修改上一个 commit 的描述

  • 查看 commit 历史

  • 显示本地更新过 HEAD 的 git 命令记录

  • 修改作者名

  • 修改远程仓库的 url

  • 增加远程仓库

  • 列出所有远程仓库

  • 查看两个星期内的改动

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

  • 给 git 命令起别名

  • 存储当前的修改,但不用提交 commit

  • 保存当前状态,包括 untracked 的文件

  • 展示所有 stashes

  • 回到某个 stash 的状态

  • 回到最后一个 stash 的状态,并删除这个 stash

  • 删除所有的 stash

  • 从 stash 中拿出某个文件的修改

  • 展示所有 tracked 的文件

  • 展示所有 untracked 的文件

  • 展示所有忽略的文件

  • 强制删除 untracked 的文件

  • 强制删除 untracked 的目录

  • 展示简化的 commit 历史

  • 查看某段代码是谁写的

  • 把某一个分支到导出成一个文件

  • 从包中导入分支

  • 执行 rebase 之前自动 stash

  • 从远程仓库根据 ID,拉下某一状态,到本地分支

  • 详细展示一行中的修改

  • 清除 .gitignore 文件中记录的文件

  • 展示所有 alias 和 configs

  • 展示忽略的文件

  • commit 历史中显示 Branch1 有的,但是 Branch2 没有 commit

  • 在 commit log 中显示 GPG 签名

  • 删除全局设置

  • 新建并切换到新分支上,同时这个分支没有任何 commit

  • 展示任意分支某一文件的内容

  • clone 下来指定的单一分支

  • 忽略某个文件的改动

  • 忽略文件的权限变化

  • 以最后提交的顺序列出所有 Git 分支

  • 在 commit log 中查找相关内容

  • 把暂存区的指定 file 放到工作区中

  • 强制推送

详解 Git 奇技淫巧

展示帮助信息

git help -g

输出如下:

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

回到远程仓库的状态

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

git fetch --all && git reset --hard origin/master

重设第一个 commit

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

git update-ref -d HEAD

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

输出工作区和暂存区的 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 branch -vv

关联远程分支

关联之后,git branch -vv 就可以展示关联的远程分支名了,同时推送到远程仓库直接:git push,不需要指定远程仓库了。

git branch -u origin/mybranch

或者在 push 时加上 -u参数

git push origin/mybranch -u

列出所有远程分支

-r参数相当于:remote

git branch -r

列出本地和远程分支

-a参数相当于:all

git branch -a

创建并切换到本地分支

git checkout -b <branch-name>

从远程分支中创建并切换到本地分支

git checkout -b <branch-name> origin/<branch-name>

删除本地分支

git branch -d <local-branchname>

删除远程分支

git push origin --delete <remote-branchname>

或者

git push origin :<remote-branchname>

重命名本地分支

git branch -m <new-branch-name>

查看标签

git tag

展示当前分支的最近的 tag

git describe --tags --abbrev=0

查看标签详细信息

git tag -ln

本地创建标签

git tag <version-number>

默认 tag 是打在最近的一次 commit 上,如果需要指定 commit 打 tag:

$ git tag -a <version-number> -m "v1.0 发布 (描述)" <commit-id>

推送标签到远程仓库

首先要保证本地创建好了标签才可以推送标签到远程仓库:

git push origin <local-version-number>

一次性推送所有标签,同步到远程仓库:

git push origin --tags

删除本地标签

git tag -d <tag-name>

删除远程标签

删除远程标签需要先删除本地标签,再执行下面的命令:

git push origin :refs/tags/<tag-name>

切回到某个标签

一般上线之前都会打 tag,就是为了防止上线后出现问题,方便快速回退到上一版本。下面的命令是回到某一标签下的状态:

git checkout -b branch_name tag_name

放弃工作区的修改

git checkout <file-name>

放弃所有修改:

git checkout .

恢复删除的文件

<code>git rev-list -n 1 HEAD -- <file_path> # 得到 deleting_commit</code><code>git checkout <deleting_commit>^ -- <file_path> # 回到删除文件 deleting_commit 之前的状态</code>

以新增一个 commit 的方式还原某一个 commit 的修改

git revert <commit-id>

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

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

<code>git reset <commit-id> # 默认就是-mixed <mark data-type="technologies" data-id="2e982b73-88e2-41e8-a430-f7ae5a9af4bf">参数</mark>。</code><code>git reset –mixed HEAD^ # 回退至上个版本,它将重置 HEAD 到另外一个 commit, 并且重置暂存区以便和 HEAD 相匹配,但是也到此为止。工作区不会被更改。</code><code>git reset –soft HEAD~3 # 回退至三个版本之前,只回退了 commit 的信息,暂存区和工作区与回退之前保持一致。如果还要提交,直接 commit 即可</code><code>git reset –hard <commit-id> # 彻底回退到指定 commit-id 的状态,暂存区和工作区也会变为指定 commit-id 版本的内容</code>

修改上一个 commit 的描述

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

git commit --amend

查看 commit 历史

git log

查看某段代码是谁写的

blame 的意思为『责怪』,你懂的。

git blame <file-name>

显示本地更新过 HEAD 的 git 命令记录

每次更新了 HEAD 的 git 命令比如 commint、amend、cherry-pick、reset、revert 等都会被记录下来(不限分支),就像 shell 的 history 一样。这样你可以 reset 到任何一次更新了 HEAD 的操作之后,而不仅仅是回到当前分支下的某个 commit 之后的状态。

git reflog

修改作者名

git commit --amend --author='Author Name <[email protected]>'

修改远程仓库的 url

git remote set-url origin <URL>

增加远程仓库

git remote add origin <remote-url>

列出所有远程仓库

git remote

查看两个星期内的改动

git whatchanged --since='2 weeks ago'

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

这个过程需要 cherry-pick 命令,参考: http://sg552.iteye.com/blog/1300713#bc2367928

git checkout <branch-name> && git cherry-pick <commit-id>

给 git 命令起别名

简化命令

<code>git config --global alias.<handle> <command></code><code>比如:git status 改成 git st,这样可以简化命令</code><code>git config --global alias.st status</code>

存储当前的修改,但不用提交 commit

详解可以参考廖雪峰老师的 git 教程 ( http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/00137602359178794d966923e5c4134bc8bf98dfb03aea3000 )

git stash

保存当前状态,包括 untracked 的文件

untracked 文件:新建的文件

git stash -u

展示所有 stashes

git stash list

回到某个 stash 的状态

git stash apply <stash@{n}>

回到最后一个 stash 的状态,并删除这个 stash

git stash pop

删除所有的 stash

git stash clear

从 stash 中拿出某个文件的修改

git checkout <stash@{n}> -- <file-path>

展示所有 tracked 的文件

git ls-files -t

展示所有 untracked 的文件

git ls-files --others

展示所有忽略的文件

git ls-files --others -i --exclude-standard

强制删除 untracked 的文件

可以用来删除新建的文件。如果不指定文件文件名,则清空所有工作的 untracked 文件。clean 命令,注意两点:

1. clean 后,删除的文件无法找回

2. 不会影响 tracked 的文件的改动,只会删除 untracked 的文件

git clean <file-name> -f

强制删除 untracked 的目录

可以用来删除新建的目录,注意:这个命令也可以用来删除 untracked 的文件。详情见上一条。

git clean <directory-name> -df

展示简化的 commit 历史

git log --pretty=oneline --graph --decorate --all

把某一个分支导出成一个文件

git bundle create <file> <branch-name>

从包中导入分支

新建一个分支,分支内容就是上面 git bundle create 命令导出的内容

git clone repo.bundle <repo-dir> -b <branch-name>

执行 rebase 之前自动 stash

git rebase --autostash

从远程仓库根据 ID,拉下某一状态,到本地分支

git fetch origin pull/<id>/head:<branch-name>

详细展示一行中的修改

git diff --word-diff

清除 gitignore 文件中记录的文件

git clean -X -f

展示所有 alias 和 configs

注意:config 分为:当前目录(local)和全局(golbal)的 config,默认为当前目录的 config

<code>git config --local --list (当前目录)</code><code>git config --global --list (全局)</code>

展示忽略的文件

git status --ignored

commit 历史中显示 Branch1 有的,但是 Branch2 没有 commit

git log Branch1 ^Branch2

在 commit log 中显示 GPG 签名

git log --show-signature

删除全局设置

git config --global --unset <entry-name>

新建并切换到新分支上,同时这个分支没有任何 commit

相当于保存修改,但是重写 commit 历史

git checkout --orphan <branch-name>

展示任意分支某一文件的内容

git show <branch-name>:<file-name>

clone 下来指定的单一分支

git clone -b <branch-name> --single-branch https://github.com/user/repo.git

忽略某个文件的改动

关闭 track 指定文件的改动,也就是 Git 将不会在记录这个文件的改动

git update-index --assume-unchanged path/to/file

恢复 track 指定文件的改动

git update-index --no-assume-unchanged path/to/file

忽略文件的权限变化

不再将文件的权限变化视作改动

git config core.fileMode false

以最后提交的顺序列出所有 Git 分支

最新的放在最上面

git for-each-ref --sort=-committerdate --format='%(refname:short)' refs/heads/

在 commit log 中查找相关内容

通过 grep 查找,given-text:所需要查找的字段

git log --all --grep='<given-text>'

把暂存区的指定 file 放到工作区中

不添加参数,默认是 -mixed

git reset <file-name>

强制推送

git push -f <remote-name> <branch-name>

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK