3

git リセット HEAD~1

~1 は、HEAD から開始し、1 つのリンクをたどり、HEAD タグをその新しいコミット ノードに設定するという意味であるという印象を受けました。私は期待していました

git リセット HEAD~2

2 つのリンクをたどって HEAD タグを設定します。ただし、試してみると、エラーが発生します。

$ git reflog
c83bbda HEAD@{0}: reset: moving to HEAD~1
44c3540 HEAD@{1}: commit: you will be garbage soon
c83bbda HEAD@{2}: reset: moving to HEAD~1
aee7955 HEAD@{3}: commit: back to 4 lines
c83bbda HEAD@{4}: reset: moving to HEAD~1
19ec1d5 HEAD@{5}: commit: 3 lines
c83bbda HEAD@{6}: reset: moving to HEAD~1
a049538 HEAD@{7}: commit: added new line
c83bbda HEAD@{8}: commit (initial): first commit


$ git reset --hard HEAD~2
fatal: ambiguous argument 'HEAD~2': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

どうやら私は間違っていたようですが、git reset のドキュメント ページはこれを明確にするのにあまり役に立ちません。では、~1 は何を意味し、なぜそれが必要なのですか?

4

4 に答える 4

5

HEAD~1 is "the first parent of HEAD", while HEAD~2 is "the first parent of the first parent of HEAD, and so on (so HEAD~n for some n is like HEAD followed by n ^ symbols and no numbers). Again, all the specifics are in the git-rev-parse manual page.

Be careful when mixing git reset with "revisions counting backwards from HEAD". git reset will, in general, change the value of HEAD, e.g.:

$ git checkout master    # now on tip of "master" branch
$ git branch save master # copy branch tip to another label, for safekeeping
$ git reset HEAD^        # or git reset HEAD~1

moves HEAD (and master) to its first parent. Another way to name that parent is save^, and yet another is save~1. After the move finishes, though, HEAD now names that parent revision, so HEAD^ names its parent:

$ git reset HEAD^

moves you back another step, so that master and HEAD now name the same commit that save~2 names. This is easy to see using git rev-parse, which tells you the commit-ID that some symbolic-name maps to:

$ git rev-parse save~2 master
0f5a13497dd3da8aff8e452c8f56630f83253e79
0f5a13497dd3da8aff8e452c8f56630f83253e79

At this point, you can restore master to the save-point with:

$ git reset save

which moves HEAD and master back to the saved revision, and then you can delete save safely if you like:

$ git branch -d save

Note that you could save a save-point with git tag too: the only difference between a branch and a tag is the behavior of new check-ins when "on a branch" (tags don't move, branches do) and check-outs (tags put you in "detached HEAD" = not-on-a-branch state, branch names put you in "on-a-branch" state).

于 2013-08-07T12:13:06.597 に答える
3

コミットを指定する方法の詳細については、または Git - リビジョンの選択を参照してgit help revisionsください

<rev>~<n>, e.g. master~3

~<n>リビジョン パラメータのサフィックスは<n>th 、最初の親のみに続く、名前付きコミット オブジェクトの世代の祖先であるコミット オブジェクトを意味します。つまり、 は と同等<rev>~3です。<rev>^^^<rev>^1^1^1

この構文は、ほとんどの Git コマンドで使用できるため、に固有のものではありませんgit reset

于 2013-08-07T12:39:50.207 に答える
2

git reser --hard HEAD~1このように、現在のブランチから最後の1つ(またはあなたが入力した他の数字)のコミットを
git reset HEAD 削除します...最後のコミットを
git reset HEAD~1削除します...最後の2つのコミット
を削除します...

于 2013-08-07T11:59:36.847 に答える
1

実行すると、このエラーメッセージが表示されますgit reset --hard HEAD~2

fatal: ambiguous argument 'HEAD~2': unknown revision or path not in the working tree.

作業コピーを存在しないコミットにリセットしようとしているためです。reflog によると、次のコマンドを実行すると、最初のルート コミットがチェックアウトされます。

$ git reflog
c83bbda HEAD@{0}: reset: moving to HEAD~1
# etc ...
c83bbda HEAD@{8}: commit (initial): first commit

したがって、上記のreflog によると、あなたは現在作業HEADコピーですgit reset --hard head~2。最初のコミットの前には何も存在しないため、次のようにします。

fatal: ambiguous argument 'HEAD~2': unknown revision or path not in the working tree.
于 2013-08-07T14:34:33.720 に答える