常用 Git 命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 添加远程仓库
git remote add upstream https://github.com/apache/shardingsphere.git

# 查看远程仓库信息
git remote -v
# origin https://github.com/strongduanmu/shardingsphere.git (fetch)
# origin https://github.com/strongduanmu/shardingsphere.git (push)
# upstream https://github.com/apache/shardingsphere.git (fetch)
# upstream https://github.com/apache/shardingsphere.git (push)

# 拉取远程库 PR 代码到 dev-0705 分支
git fetch upstream pull/11150/head:dev-0705
# From https://github.com/apache/shardingsphere
# * [new ref] refs/pull/11150/head -> dev-0705

# 拉取远程库 TAG 代码到 5.3.0-test 分支
git fetch upstream refs/tags/5.3.0:5.3.0-test

# 和上游 master 分支同步(先拉取 upstream)
git fetch upstream
git rebase upstream/master

# 修改 commit 信息
git commit --amend

# 根据指定 commitId 创建新分支
git checkout commitId -b branchName

# 批量删除 dev* 分支
git branch -a | grep "^ dev*" | xargs git branch -D

# 恢复误删除分支
## 查看被删除分支对应的 commit id
git log -g
## 根据 commit id 创建 recover_branch 分支
git branch recover_branch 34fd566205a34a2842111331449b47c39ef7fa6e

Git 合并本地多次提交

本地开发时,可能会存在多次提交的情况,为了保证 Git log 的整洁,需要对多次提交进行合并,通过 git rebase 命令可以快速完成这个目标。参考 Git 合并多个 commit 文章,可以执行如下命令进行合并:

1
2
3
4
# 从 HEAD 版本开始,合并过去的 3 个版本
git rebase -i HEAD~3
# 合并 3a4226b 之前的版本,3a4226b 不参与合并
git rebase -i 3a4226b

执行 rebase 后,会出现如下的窗口,展示了需要合并的 commit 记录,根据下面的命令提示,我们可以将 pick 修改为 squashssquash 命令会将当前提交合并到前一次提交,并允许修改提交信息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
pick c144c143930 Add DatabaseConnector interface, and move execute logic to StandardDatabaseConnector
pick 26eb5180e88 fix unit test

# Rebase 8e684ae6bdf..26eb5180e88 onto 8e684ae6bdf (2 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup [-C | -c] <commit> = like "squash" but keep only the previous
# commit's log message, unless -C is used, in which case
# keep only this commit's message; -c is same as -C but
# opens the editor
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# create a merge commit using the original merge commit's
# message (or the oneline, if no original merge commit was
# specified); use -c <commit> to reword the commit message
# u, update-ref <ref> = track a placeholder for the <ref> to be updated
# to this position in the new commits. The <ref> is
# updated at the end of the rebase
#
# These lines can be re-ordered; they are executed from top to bottom.

修改后交互式信息显示如下,然后保存退出,此时如果有冲突则需要修改,修改的时候需要注意,保留最新的历史,否则我们的修改就会丢失。

1
2
3
pick c144c143930 Add DatabaseConnector interface, and move execute logic to StandardDatabaseConnector
s 26eb5180e88 fix unit test
s 967ddd22efb fix unit test

修改完成后,需要执行以下命令,将修改添加进来,并继续 rebase 流程,如果处理不了冲突,则可以中断 rebase 流程。

1
2
3
4
5
git add .
# 继续 rebase
git rebase --continue
# 中断 rebase
git rebase --abort

如果没有冲突,或者已经解决了冲突,则会显示如下的内容,内容中包含了合并提交的 commit 信息,不需要的信息可以使用 # 注释,也可以修改最终保留的 commit 信息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# This is a combination of 3 commits.
# This is the 1st commit message:

Add DatabaseConnector interface, and move execute logic to StandardDatabaseConnector

# This is the commit message #2:

fix unit test

# This is the commit message #3:

fix unit test

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Wed Nov 13 10:57:57 2024 +0800
#
# interactive rebase in progress; onto 8e684ae6bdf
# Last commands done (3 commands done):
# squash 26eb5180e88 fix unit test
# squash 967ddd22efb fix unit test
# No commands remaining.
# You are currently rebasing branch 'dev-1113' on '8e684ae6bdf'.
#
# Changes to be committed:
# modified: proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/connector/DatabaseConnector.java

此处我们将 fix unit test 都注释掉,并修改第一个 commit 信息,增加 modify test,修改完成后退出保存。

1
2
3
4
5
6
7
8
9
10
11
12
# This is a combination of 3 commits.
# This is the 1st commit message:

Add DatabaseConnector interface, and move execute logic to StandardDatabaseConnector modify test

# This is the commit message #2:

# fix unit test

# This is the commit message #3:

# fix unit test

此时会提示 rebase 成功,并显示了新的 HEAD commit 信息。

1
2
3
4
5
6
[detached HEAD 552f1459fd4] Add DatabaseConnector interface, and move execute logic to StandardDatabaseConnector modify test
Date: Wed Nov 13 10:57:57 2024 +0800
6 files changed, 433 insertions(+), 392 deletions(-)
create mode 100644 proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/connector/StandardDatabaseConnector.java
rename proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/{DatabaseConnectorTest.java => StandardDatabaseConnectorTest.java} (91%)
Successfully rebased and updated refs/heads/dev-1113.

Github 配置 HTTPS

最近使用 SSH 访问 Github 仓库,经常出现 push 卡顿的情况,使用 ssh -T -p 443 git@ssh.github.com 测试可用性时,无法正常获取响应结果,ssh 服务可用时,应当返回如下结果。

1
2
3
4
5
6
7
8
duanzhengqiang@duanzhengqiang-ubuntu:~/blog$ ssh -T -p 443 git@ssh.github.com
The authenticity of host '[ssh.github.com]:443 ([20.205.243.160]:443)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
This host key is known by the following other names/addresses:
~/.ssh/known_hosts:1: [hashed name]
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '[ssh.github.com]:443' (ED25519) to the list of known hosts.
Hi strongduanmu! You've successfully authenticated, but GitHub does not provide shell access.

为了不影响工作效率,尝试将 SSH 替换为 HTTPS,参考 Managing your personal access tokens,点击个人头像下的 Settings,然后选择 Developer settings -> Personal access tokens -> Fine-grained personal access tokens Beta -> Generate new token,生成 token 如下图所示。

生成 Token

生成完成后,执行 git fetch 等命令时,输入用户名和密码(生成的 token)。

1
2
Username for 'https://github.com': strongduanmu
Password for 'https://strongduanmu@github.com': {token}

此外,为了避免频繁输入用户名和密码,可以执行 git config --global credential.helper store,将认证信息存储下来,这样后续执行就无需重复输入了。

Git 提交超大文件

当 PR 中包含超大文件时(超过 50 MB),此时无法将这些文件提交到仓库中,需要通过大文件提交方式进行处理。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 ~/IdeaProjects/dbplus-engine-honor │ dev-5.5.1-fix-0104 *34 +10 !9  git push                                                                                                                                                                      PIPE ✘ │ 5m 52s 
Uploading LFS objects: 100% (2/2), 439 MB | 0 B/s, done.
Enumerating objects: 762, done.
Counting objects: 100% (541/541), done.
Delta compression using up to 12 threads
Compressing objects: 100% (142/142), done.
Writing objects: 100% (193/193), 38.50 MiB | 1.89 MiB/s, done.
Total 193 (delta 77), reused 0 (delta 0), pack-reused 0 (from 0)
remote: Resolving deltas: 100% (77/77), completed with 32 local objects.
remote: warning: File test/e2e/sql/src/test/resources/env/scenario/sphereex_db_tbl_sql_federation_honor/data/expected/init-sql/mysql/02-expected-prps_asc_stock_keep_list_t-init.sql is 80.21 MB; this is larger than GitHub's recommended maximum file size of 50.00 MB
remote: error: Trace: 28a0369f24fb235ec54356e77d75564ec23819e687079bf342ad247ff6bd5ca2
remote: error: See https://gh.io/lfs for more information.
remote: error: File test/e2e/sql/src/test/resources/env/scenario/sphereex_db_tbl_sql_federation_honor/data/expected/init-sql/mysql/02-expected-rps_actual_material_consume_rate_t-init.sql is 337.76 MB; this exceeds GitHub's file size limit of 100.00 MB
remote: error: GH001: Large files detected. You may want to try Git Large File Storage - https://git-lfs.github.com.
To github.com:strongduanmu/dbplus-engine-honor.git
! [remote rejected] dev-5.5.1-fix-0104 -> dev-5.5.1-fix-0104 (pre-receive hook declined)
error: failed to push some refs to 'github.com:strongduanmu/dbplus-engine-honor.git'

首先,安装 git-lfs 工具,并在代码仓库目录下执行 git lfs install 进行初始化。

1
2
brew install git-lfs
git lfs install

然后使用如下的命令追踪大文件:

1
2
git lfs track "test/e2e/sql/src/test/resources/env/scenario/sphereex_db_tbl_sql_federation_honor/data/expected/init-sql/mysql/02-expected-prps_asc_stock_keep_list_t-init.sql"
git lfs track "test/e2e/sql/src/test/resources/env/scenario/sphereex_db_tbl_sql_federation_honor/data/expected/init-sql/mysql/02-expected-rps_actual_material_consume_rate_t-init.sql"

git lfs track 命令会自动生成 / 修改 .gitattributes 文件,我们需要将这个文件提交到仓库:

1
git add .gitattributes

最后,我们需要撤销上一次提交,因为大文件已经被纳入了之前的提交记录,执行如下的命令先撤销提交,再重新处理并 push

1
2
3
4
5
git reset --soft HEAD~1
# 修改成你要提交的具体文件
git add .
git commit -m "commit message"
git push

大文件 Push 成功