回答:
git reflogあなたの友だちです。そのリストで使用したいコミットを見つけたら、それにコミットできます(例:)git reset --hard e870e41。
(変更をコミットしなかった場合...問題が発生する可能性があります-早期にコミットし、頻繁にコミットしてください!)
git log HEAD@{1}。それが正しい一連のコミットのように見える場合は、できgit reset HEAD@{1}ます。
git fsck --lost-found。
答える前に、背景を追加して、これHEADが何であるかを説明しましょう。
First of all what is HEAD?HEAD現在のブランチ上の現在のコミット(最新)への参照です。
常に1つしか存在できませんHEAD(を除くgit worktree)。
の内容HEADは内部に格納され.git/HEAD、現在のコミットの40バイトのSHA-1が含まれています。
detached HEAD最新のコミットを行っていない場合、つまりHEAD、履歴内の以前のコミットを指している場合は、と呼ばれdetached HEADます。
コマンドラインでは、次のようになります- HEADは現在のブランチの先端を指していないため、ブランチ名の代わりにSHA-1 :
git checkoutgit checkout <commit_id>
git checkout -b <new branch> <commit_id>
git checkout HEAD~X // x is the number of commits t go back
これにより、目的のコミットを指す新しいブランチがチェックアウトされます。
このコマンドは、特定のコミットにチェックアウトします。
この時点で、ブランチを作成して、この時点から作業を開始できます。
# Checkout a given commit.
# Doing so will result in a `detached HEAD` which mean that the `HEAD`
# is not pointing to the latest so you will need to checkout branch
# in order to be able to update the code.
git checkout <commit-id>
# Create a new branch forked to the given commit
git checkout -b <branch name>
git reflogいつでも使用できreflogます。
git reflog を更新した変更が表示さHEADれ、目的のreflogエントリをチェックアウトすると、HEADこのコミットに戻ります。
HEADが変更されるたびに、新しいエントリが reflog
git reflog
git checkout HEAD@{...}
これにより、目的のコミットに戻ります
git reset --hard <commit_id>HEADを目的のコミットに「移動」します。
# This will destroy any local modifications.
# Don't do it if you have uncommitted work you want to keep.
git reset --hard 0d1d7fc32
# Alternatively, if there's work to keep:
git stash
git reset --hard 0d1d7fc32
git stash pop
# This saves the modifications, then reapplies that patch after resetting.
# You could get merge conflicts if you've modified things which were
# changed since the commit you reset to.
git rebase --no-autostashます。git revert <sha-1>指定されたコミットまたはコミット範囲を「元に戻す」。
リセットコマンドは、指定されたコミットで行われた変更を「取り消し」ます。
元に戻すコミットを含む新しいコミットはコミットされますが、元のコミットも履歴に残ります。
# Add a new commit with the undo of the original one.
# The <sha-1> can be any commit(s) or commit range
git revert <sha-1>
このスキーマは、どのコマンドが何を実行するかを示しています。
ご覧のとおり、をreset && checkout変更しHEADます。
git reset --hard <commit_id>、削除HEADはうまくいきました!グラフィカル表現の+1 !!
git reflog <branchname>1つのブランチの変更のみが表示されるため、非常に便利です。
削除されたコミットを取得する別の方法は、git fsckコマンドを使用することです。
git fsck --lost-found
これは最終行のようなものを出力します:
dangling commit xyz
reflog他の回答で提案されているのと同じコミットであることを確認できます。今、私たちはすることができますgit merge
git merge xyz
注:ダングリングコミットへの参照を削除するコマンドをすでに実行している場合は
、コミットを取り戻すことはできません。fsckgit gc