跳转至

git

git

高级

  • 从一个节点产生多个分支,能否 rebase 新的 cm 到该节点?

reset 后恢复 reflog

git reflog

reset --soft, --mixed, --hard

version control - What's the difference between git reset --mixed, --soft, and --hard? - Stack Overflow

修改的不同阶段:

  • in working directory:
  • index / stagged:通过 git add
  • commited:进入版本控制,commited 的修改总能找回来。
- A - B - C (master)

--soft B:将 C 的修改恢复到 index 中 --mixed B:将 C 的修改恢复到 working directory 中 --hard B:index 被修改为 B,working directory 清空

git co 和 git reset --mixed 效果类似,都能保留当前 working directory,同时还能保留 git log。

tag

#显示所有tag
git tag
#显示具体tag信息
git show v1.2
#添加tag
git tag v1.2 9fceb02 -a
git tag v1.2 9fceb02 -a -m "my tag"
#删除tag
git tag -d v1.2
#推送tag到远程
git push origin --tags

小技巧

shallow clone

避免 clone 的仓库太大,将 git commit 记录截断。

git clone --depth 1 git://gcc.gnu.org/git/gcc.git

man git-clone Create a shallow clone with a history truncated to the specified number of commits. Implies --single-branch unless --no-single-branch is given to fetch the histories near the tips of all branches. If you want to clone submodules shallowly, also pass shallow-submodules.

代理

查看详细信息

  • GIT_TRACE=1
  • git --verbose <cmd>

http 和 https 设置代理


ssh 代理

windows

windows 下没有 nc 命令,需要 connect.exe(可以下载.c 文件编译成 exe 文件)

SSH in git behind proxy on windows 7 - Stack Overflow

Host github.com
  HostName github.com
  ...
  ProxyCommand connect -H 127.0.0.1:11223 %h %p

遇到的问题

unsafe directory

git - Fatal error "unsafe repository ('/home/repon' is owned by someone else)" - Stack Overflow

git version > Git 2.36

git config --global --add safe.directory '*'

curl 92 HTTP/2 stream 0 was not closed cleanly: CANCEL (err 8)

➜  repo git clone https://github.com/YaoFANGUK/video-subtitle-generator
Cloning into 'video-subtitle-generator'...
remote: Enumerating objects: 434, done.
remote: Counting objects: 100% (95/95), done.
remote: Compressing objects: 100% (53/53), done.
error: RPC failed; curl 92 HTTP/2 stream 0 was not closed cleanly: CANCEL (err 8)
error: 503 bytes of body are still expected
fetch-pack: unexpected disconnect while reading sideband packet
fatal: early EOF
fatal: fetch-pack: invalid index-pack output

git - error: RPC failed; curl 92 HTTP/2 stream 0 was not closed cleanly: PROTOCOL_ERROR (err 1) - Stack Overflow

git config --global http.version HTTP/1.1

成功

➜  repo git clone https://github.com/YaoFANGUK/video-subtitle-generator
Cloning into 'video-subtitle-generator'...
remote: Enumerating objects: 434, done.
remote: Counting objects: 100% (95/95), done.
remote: Compressing objects: 100% (53/53), done.
remote: Total 434 (delta 46), reused 87 (delta 42), pack-reused 339
Receiving objects: 100% (434/434), 5.92 GiB | 21.39 MiB/s, done.
Resolving deltas: 100% (149/149), done.
Updating files: 100% (151/151), done.

貌似修改 buffer 也可以:GIT 推送错误 error: RPC failed;curl 92 HTTP/2 stream 0 was not closed cleanly: CANCEL (err 8) - 郭小睿 - 博客园 (cnblogs.com)

git config --global http.postBuffer 524288000

patch 补丁

Git 打补丁-- patch 和 diff 的使用(详细) - 掘金 (juejin.cn)

Git 提供了两种补丁方案,一是用 git diff 生成的 UNIX 标准补丁.diff 文件,二是 git format-patch 生成的 Git 专用.patch 文件。 .diff 文件只是记录文件改变的内容,不带有 commit 记录信息,多个 commit 可以合并成一个 diff 文件。 .patch 文件带有记录文件改变的内容,也带有 commit 记录信息,每个 commit 对应一个 patch 文件。

git format-patch HEAD^  # 最新的一次 commit(上一次到现在的 commit)
git format-patch 【commit sha1 id】..【commit sha1 id】 # 某两次提交之间的所有patch
  • -o: If -o is specified, output files are created in dir. Otherwise they are created in the current working directory
  • -n: -n, --numbered Name output in [PATCH n/m] format, even with a single patch.
git am 【path/to/xxx.patch】

git diff 相关

git diff 【commit sha1 id】 【commit sha1 id】 > 【diff文件名】
git diff --no-index file1 file2 # 非git repo下,生成两个文件diff
git apply [--check] 【path/to/xxx.patch
git apply --check 【path/to/xxx.diff】

format-patch

Git - git-format-patch Documentation (git-scm.com)

Prepare each non-merge commit with its "patch" in one "message" per commit, formatted to resemble a UNIX mailbox. The output of this command is convenient for e-mail submission or for use with git am.

git diff [<options>] --no-index [--] <path> <path>

This form is to compare the given two paths on the filesystem. You can omit the --no-index option when running the command in a working tree controlled by Git and at least one of the paths points outside the working tree, or when running the command outside a working tree controlled by Git.

submodule

可以本地添加 submodule

git submodule add ./third-party/nvbit_release third-party/nvbit_release

添加 submodule 后会生成.gitmodules文件

[submodule "third-party/gpu-rodinia"]
        path = third-party/gpu-rodinia
        url = ./third-party/gpu-rodinia
[submodule "third-party/nvbit_release"]
        path = third-party/nvbit_release
        url = ./third-party/nvbit_release
[submodule "third-party/cuda-samples"]
        path = third-party/cuda-samples
        url = ./third-party/cuda-samples
[submodule "third-party/gpu-parboil"]
        path = third-party/gpu-parboil
        url = ./third-party/gpu-parboil

clone submodule

clone 时默认是不会 clone 子模块的,需要添加 recursive。-j8 表示使用多线程下载。

git clone --recursive -j8 gpu-analysis-model gpu-analysis-model-70

会从.gitmodules中指定的 url 中 clone 仓库,然后 checkout 到对应 commit。如果是本地 commit,没有推送到远程,则会因为没有对应 commit 而报错。解决方法是改用本地仓库作为 url。

file not allowed 问题

Submodule 'third-party/cuda-samples' (/staff/fyyuan/repo/gpu-analysis-model/third-party/cuda-samples) registered for path 'third-party/cuda-samples'

Cloning into '/staff/fyyuan/repo/gpu-analysis-model-70/third-party/cuda-samples'...
fatal: transport 'file' not allowed

解决方法

git config --global protocol.file.allow always