Git 高阶命令

[toc]

git 原理图

git command

git 命令大集合

git add

1
git add -h

查看更详细的帮助内容

1
git add . 

不加参数默认为将修改操作的文件和未跟踪新添加的文件添加到 git 系统的暂存区,注意不包括删除文件。

1
git add -u .

-u == --update,表示将已跟踪文件中的修改和删除的文件添加到暂存区,注意不包括新增加的文件。

1
git add -A .

-A == --all,表示将所有的已跟踪的文件的修改与删除和新增的未跟踪的文件都添加到暂存区。

stash local changes

  • If you want remove all local changes from your working copy, simply stash them.

    1
    git stash save --keep-index  

  • If you don't need them anymore, you now can drop that stash.

    1
    git stash drop

git pull

1
git pull origin target_branch_name

下载指定分支代码合并到当前分支:相当于指定分支的代码与当前分支的代码合并到了一起)。

每次使用该命令前,需要保证本地工作区是没有任何修改代码的,也就是说需要将本地工作区编辑过的文件添加到暂存区 (git add .),或提交到本地仓库中 (git commit),才可以使用该命令拉取指定分支的代码合并到当前分支中。

建议:每次在操作完 git commit 命令后,必须拉取一下 master 分支代码,保持本地正在开发功能逻辑的代码分支代码是最新的,避免后续在提交时冲突过多或覆盖掉其他人的代码的问题出现。

git brach

1
git branch -a

加上 - a 参数可以查看远程分支,远程分支会用红色表示出来

1
git log --graph

查看分支合并图

git log

1
git log --oneline --author=git_name

git log 中根据 author 过滤

1
git log --pretty=one

显示 git 提交记录的第一行,方便检索和管理提交记录

1
git log --oneline --after="2020-05-21 00:00:00"  | grep "region"

查询某段时间的提交记录关键字

git cherry-pick

1
2
3
4
5
nothing to commit, working directory clean
The previous cherry-pick is now empty, possibly due to conflict resolution.
If you wish to commit it anyway, use:

git commit –allow-empty

It’s exactly what it says: the changes you’re trying to cherry-pick are already wholly already integrated/cherry-picked in the branch you’re on. I.e. the result of the cherry-pick is no changes. You can create an empty commit with the --allow-empty flag to indicate that you attempted to cherry-pick, but there were no changes to pull in.

git shortlog

查找 commit description 包含某些字符的 log 日志

1
2
3
4
5
git shortlog --format='%H|%cn|%s' | grep 'your_search_info'

%H: commit hash
%cn: committer name
%s: subject

git commit

修改 commit 信息

用一条新的 commit 信息修订现有的 commit,这会覆盖原始 commit。请勿在已推送的 commit 中使用它。

1
git commit --amend -m "<new commit message>"

恢复指定 diff

1
2
// You can specifically apply an out-of-date diff or a diff which was never attached to a revision by using this flag.
arc patch --diff diff_id --nobranch

删除多个本地 branches

1
2
# 执行前需要切换到master分支执行
git branch | grep -v "master" | xargs git branch -D

如何把分支比较结果输入到指定文件

  • Linux 输出重定向
1
2
> 输出重定向:会将原来的文件内容覆盖。
>> 追加:不会覆盖原来文件的内容,而是追加到文件的尾部。
1
2
3
4
# 使用 Linux 重定向命令
git diff [branchA] [branchB] >>d:/diff/exportname.diff # 追加

git diff [branchA] [branchB] >d:/diff/exportname.diff # 覆盖

git patch

有时候,我们需要进行代码迁移,比如把 A 开发机上的代码迁移到 B 开发机上,这时候就可以使用补丁。

git 提供两种补丁方案: - 通过 git diff 生成 .diff 文件。 * 可以指定文件生成 .diff 文件,也可以指定单个或多个 commit 生成。 * 生成的文件不含有 commit 信息。 - 通过 git format-patch 生成 .patch 文件。 * 一个 commit 对应一个 patch 文件。 * 生成的文件含有 commmit 信息。

git format-patch

1
2
3
4
5
6
7
8
# 当前分支所有超前 master 的提交
git format-patch -M master

# 某次提交以后的所有 patch。[commit id] 指的是 commit name,可以通过 git log 查看。
git format-patch [commit id]

# 某两次提交之间的所有patch:
git format-patch [commit sha1 id..[commit sha2 id]

git format-patch 命令中,一个 commit 对应一个 patch 文件,所以生成的补丁文件默认从 1 开始顺序编号,并使用对应提交信息中的第一行作为文件名。

apply patch

1
2
3
4
5
6
7
8
# 先检查 patch 文件
git apply --stat xxx.patch

# 检查能否应用成功
git apply --check xxx.patch

# 打补丁。使用 -s 或 –-signoff 选项,可以 commit 信息中加入 signed-off-by 信息。
git am --signoff < xxx.patch
  • branch 中修改文件所在目录,git 是可以跟踪这个变化的。

git submodule

有种情况我们经常会遇到:某个工作中的项目需要包含并使用另一个项目。 也许是第三方库,或者你独立开发的,用于多个父项目的库。 现在问题来了:你想要把它们当做两个独立的项目,同时又想在一个项目中使用另一个。

Git 通过子模块来解决这个问题。 子模块允许你将一个 Git 仓库作为另一个 Git 仓库的子目录。 它能让你将另一个仓库克隆到自己的项目中,同时还保持提交的独立。

在配置网站的 gitHun Actions 中,主题 Next 是一个 submodule。我将 Next submodule 转化为普通文件夹,步骤如下所示。

1

如何把 Git Submodule 变成普通文件夹

References