孤立提交会发生什么?

Ban*_*San 12 git git-reset git-commit

我有一个有四个提交的回购:

$ git log --oneline --decorate
6c35831 (HEAD, master) C4
974073b C3
e27b22c C2
9f2d694 C1
Run Code Online (Sandbox Code Playgroud)

reset -- softC2提交,现在我有一个像这样的回购:

$ git reset e27b22c --soft

$ git log --oneline --decorate
e27b22c (HEAD, master) C2
9f2d694 C1
Run Code Online (Sandbox Code Playgroud)

现在我添加一个额外的提交,所以日志看起来像这样:

$ git log --oneline --decorate
545fa99 (HEAD, master) C5
e27b22c C2
9f2d694 C1
Run Code Online (Sandbox Code Playgroud)

发生了什么事提交C3C4?我没有删除它们,所以我认为它们仍在那里,C3我的父母仍在C2.

Dan*_*ruz 13

简短的回答:提交C3,并C4会留在Git的对象数据库它们是垃圾回收,直至.

答案很长:垃圾收集将通过不同的Git瓷器命令或明确的垃圾收集自动进行.有许多场景可能会触发自动垃圾收集; 看看gc.*配置设置,以获得一个想法.您可以使用git gcbuiltin命令显式gabage collect .让我们看一个例子来看看会发生什么.

首先,让我们设置我们的环境(我正在使用Linux;根据您的环境进行必要的更改),因此我们希望在不同的Git存储库中获得相同的对象哈希.

export GIT_AUTHOR_NAME='Wile E. Coyote'
export GIT_AUTHOR_EMAIL=coyote@acme.com
export GIT_AUTHOR_DATE=2015-01-01T12:00:00
export GIT_COMMITTER_NAME='Roadrunner'
export GIT_COMMITTER_EMAIL=roadrunner@acme.com
export GIT_COMMITTER_DATE=2015-01-01T12:00:00
Run Code Online (Sandbox Code Playgroud)

由于使用此信息生成提交对象哈希,如果我们使用相同的作者和提交者值,我们现在都应该获得相同的哈希值.

现在,让我们初始化函数中使用记录对象的信息git log,git reflog,git count-objects,git rev-listgit fsck.

function git_log_objects () {
    echo 'Log ...'
    git log --oneline --decorate
    echo 'Reflog ...'
    git reflog show --all
    echo 'Count ...'
    git count-objects -v
    echo 'Hashes ...'
    # See: https://stackoverflow.com/a/7350019/649852
    {
        git rev-list --objects --all --reflog
        git rev-list --objects -g --no-walk --all
        git rev-list --objects --no-walk $(
            git fsck --unreachable 2>/dev/null \
                | grep '^unreachable commit' \
                | cut -d' ' -f3
        )
    } | sort | uniq
}
Run Code Online (Sandbox Code Playgroud)

现在让我们初始化一个Git存储库.

git --version
git init
git_log_objects
Run Code Online (Sandbox Code Playgroud)

对我来说,输出:

git version 2.4.0
Initialized empty Git repository in /tmp/test/.git/
Log ...
fatal: bad default revision 'HEAD'
Reflog ...
fatal: bad default revision 'HEAD'
Count ...
count: 0
size: 0
in-pack: 0
packs: 0
size-pack: 0
prune-packable: 0
garbage: 0
size-garbage: 0
Hashes ...
Run Code Online (Sandbox Code Playgroud)

正如所料,我们有一个初始化的存储库,其中没有对象.让我们做一些提交并看看对象.

git commit --allow-empty -m C1
git commit --allow-empty -m C2
git tag T1
git commit --allow-empty -m C3
git commit --allow-empty -m C4
git commit --allow-empty -m C5
git_log_objects
Run Code Online (Sandbox Code Playgroud)

这给了我以下输出:

[master (root-commit) c11e156] C1
 Author: Wile E. Coyote <coyote@acme.com>
[master 10bfa58] C2
 Author: Wile E. Coyote <coyote@acme.com>
[master 8aa22b5] C3
 Author: Wile E. Coyote <coyote@acme.com>
[master 1abb34f] C4
 Author: Wile E. Coyote <coyote@acme.com>
[master d1efc10] C5
 Author: Wile E. Coyote <coyote@acme.com>
Log ...
d1efc10 (HEAD -> master) C5
1abb34f C4
8aa22b5 C3
10bfa58 (tag: T1) C2
c11e156 C1
Reflog ...
d1efc10 refs/heads/master@{0}: commit: C5
1abb34f refs/heads/master@{1}: commit: C4
8aa22b5 refs/heads/master@{2}: commit: C3
10bfa58 refs/heads/master@{3}: commit: C2
c11e156 refs/heads/master@{4}: commit (initial): C1
Count ...
count: 6
size: 24
in-pack: 0
packs: 0
size-pack: 0
prune-packable: 0
garbage: 0
size-garbage: 0
Hashes ...
10bfa58a7bcbadfc6c9af616da89e4139c15fbb9
1abb34f82523039920fc629a68d3f82bc79acbd0
4b825dc642cb6eb9a060e54bf8d69288fbee4904 
8aa22b5f0fed338dd13c16537c1c54b3496e3224
c11e1562835fe1e9c25bf293279bff0cf778b6e0
d1efc109115b00bac9d4e3d374a05a3df9754551
Run Code Online (Sandbox Code Playgroud)

现在我们在存储库中有六个对象:五个提交和一个空树.我们可以看到Git对所有五个提交对象都有分支,标记和/或reflog引用.只要Git引用一个对象,该对象就不会被垃圾回收.显式运行gabage集合将导致没有对象从存储库中删除.(我将离开验证这是一项练习,让你完成.)

现在让我们删除对Git的引用C3,C4C5提交.

git reset --soft T1
git reflog expire --expire=all --all
git_log_objects
Run Code Online (Sandbox Code Playgroud)

哪个输出:

Log ...
10bfa58 (HEAD -> master, tag: T1) C2
c11e156 C1
Reflog ...
Count ...
count: 6
size: 24
in-pack: 0
packs: 0
size-pack: 0
prune-packable: 0
garbage: 0
size-garbage: 0
Hashes ...
10bfa58a7bcbadfc6c9af616da89e4139c15fbb9
1abb34f82523039920fc629a68d3f82bc79acbd0
4b825dc642cb6eb9a060e54bf8d69288fbee4904 
8aa22b5f0fed338dd13c16537c1c54b3496e3224
c11e1562835fe1e9c25bf293279bff0cf778b6e0
d1efc109115b00bac9d4e3d374a05a3df9754551
Run Code Online (Sandbox Code Playgroud)

现在我们看到Git只引用了两个提交.但是,所有六个对象仍在存储库中.它们将保留在存储库中,直到它们被自动或明确地垃圾收集.例如,您甚至可以使用git cherry-pick或使用它来恢复未引用的提交git show.现在,让我们明确地垃圾收集未引用的对象,看看Git在幕后做了什么.

GIT_TRACE=1 git gc --aggressive --prune=now
Run Code Online (Sandbox Code Playgroud)

这将输出一些信息.

11:03:03.123194 git.c:348               trace: built-in: git 'gc' '--aggressive' '--prune=now'
11:03:03.123625 run-command.c:347       trace: run_command: 'pack-refs' '--all' '--prune'
11:03:03.124038 exec_cmd.c:129          trace: exec: 'git' 'pack-refs' '--all' '--prune'
11:03:03.126895 git.c:348               trace: built-in: git 'pack-refs' '--all' '--prune'
11:03:03.128298 run-command.c:347       trace: run_command: 'reflog' 'expire' '--all'
11:03:03.128635 exec_cmd.c:129          trace: exec: 'git' 'reflog' 'expire' '--all'
11:03:03.131322 git.c:348               trace: built-in: git 'reflog' 'expire' '--all'
11:03:03.133179 run-command.c:347       trace: run_command: 'repack' '-d' '-l' '-f' '--depth=250' '--window=250' '-a'
11:03:03.133522 exec_cmd.c:129          trace: exec: 'git' 'repack' '-d' '-l' '-f' '--depth=250' '--window=250' '-a'
11:03:03.136915 git.c:348               trace: built-in: git 'repack' '-d' '-l' '-f' '--depth=250' '--window=250' '-a'
11:03:03.137179 run-command.c:347       trace: run_command: 'pack-objects' '--keep-true-parents' '--honor-pack-keep' '--non-empty' '--all' '--reflog' '--indexed-objects' '--window=250' '--depth=250' '--no-reuse-delta' '--local' '--delta-base-offset' '.git/objects/pack/.tmp-8973-pack'
11:03:03.137686 exec_cmd.c:129          trace: exec: 'git' 'pack-objects' '--keep-true-parents' '--honor-pack-keep' '--non-empty' '--all' '--reflog' '--indexed-objects' '--window=250' '--depth=250' '--no-reuse-delta' '--local' '--delta-base-offset' '.git/objects/pack/.tmp-8973-pack'
11:03:03.140367 git.c:348               trace: built-in: git 'pack-objects' '--keep-true-parents' '--honor-pack-keep' '--non-empty' '--all' '--reflog' '--indexed-objects' '--window=250' '--depth=250' '--no-reuse-delta' '--local' '--delta-base-offset' '.git/objects/pack/.tmp-8973-pack'
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), done.
Total 3 (delta 1), reused 0 (delta 0)
11:03:03.153843 run-command.c:347       trace: run_command: 'prune' '--expire' 'now'
11:03:03.154255 exec_cmd.c:129          trace: exec: 'git' 'prune' '--expire' 'now'
11:03:03.156744 git.c:348               trace: built-in: git 'prune' '--expire' 'now'
11:03:03.159210 run-command.c:347       trace: run_command: 'rerere' 'gc'
11:03:03.159527 exec_cmd.c:129          trace: exec: 'git' 'rerere' 'gc'
11:03:03.161807 git.c:348               trace: built-in: git 'rerere' 'gc'
Run Code Online (Sandbox Code Playgroud)

最后,让我们看看对象.

git_log_objects
Run Code Online (Sandbox Code Playgroud)

哪个输出:

Log ...
10bfa58 (HEAD -> master, tag: T1) C2
c11e156 C1
Reflog ...
Count ...
count: 0
size: 0
in-pack: 3
packs: 1
size-pack: 1
prune-packable: 0
garbage: 0
size-garbage: 0
Hashes ...
10bfa58a7bcbadfc6c9af616da89e4139c15fbb9
4b825dc642cb6eb9a060e54bf8d69288fbee4904 
c11e1562835fe1e9c25bf293279bff0cf778b6e0
Run Code Online (Sandbox Code Playgroud)

现在我们看到我们只有三个对象:两个提交和一个空树.


che*_*ner 5

git show 6c35831例如,运行以查看C4仍然存在.运行git reflog master以查看(很多)master 用于引用的内容.其中一个条目(master^{1}很可能,但如果你也做了其他更改,可能还有一个条目)应该对应6c35831,并且git show master^{1}(或者无论哪个条目)应该显示git show我提到的第一个命令的相同输出.