165

gitで特定のコミットを元に戻したい。残念ながら、私たちの組織はまだCVSを標準として使用しているため、CVSにコミットし直すと、複数のgitコミットが1つにまとめられます。この場合、元のgit commitを選び出したいのですが、それは不可能です。

git add --patchコミットのどの部分を元に戻すかを決定するために差分を選択的に編集できるようにするのと同様のアプローチはありますか?

4

6 に答える 6

259

--no-commit-n)オプションを使用してgit revert、変更のステージングを解除してから、 git add --patch:を使用します。

$ git revert -n $bad_commit    # Revert the commit, but don't commit the changes
$ git reset HEAD .             # Unstage the changes
$ git add --patch .            # Add whatever changes you want
$ git commit                   # Commit those changes

注:git add --patchを使用して追加するファイルは、保持するファイルではなく、元に戻すファイルです。

于 2011-01-25T16:30:38.763 に答える
45

私は以下をうまく使いました。

最初に完全なコミットを元に戻します(インデックスに入れます)が、コミットしないでください。

git revert -n <sha1>  # -n is short for --no-commit

次に、元に戻されたGOOD変更をインデックスからインタラクティブに削除します

git reset -p          # -p is short for --patch  

次に、悪い変更の逆差分をコミットします

git commit -m "Partially revert <sha1>..."

最後に、元に戻されたGOOD変更(resetコマンドによってステージングされていない)はまだ作業ツリーにあります。それらはクリーンアップする必要があります。作業ツリーに他のコミットされていない変更が残っていない場合、これは次の方法で実行できます。

git reset --hard
于 2012-12-19T13:32:57.337 に答える
5

解決:

git revert --no-commit <commit hash>
git reset -p        # every time choose 'y' if you want keep the change, otherwise choose 'n'
git commit -m "Revert ..."
git checkout -- .   # Don't forget to use it.
于 2014-08-11T12:57:20.130 に答える
5

個人的には、自動生成されたコミットメッセージを再利用し、最終的にコミットする前に「部分的に」という単語を編集して貼り付ける機会をユーザーに提供するこのバージョンが好きです。

# generate a revert commit
# note the hash printed to console on success
git revert --no-edit <hash to revert>

# undo that commit, but not its changes to the working tree
# (reset index to commit-before-last; that is, one graph entry up from HEAD)
git reset HEAD~1

# interactively add reversions
git add -p

# commit with pre-filled message
git commit -c <hash from revert commit, printed to console after first command>

# reset the rest of the current directory's working tree to match git
# this will reapply the excluded parts of the reversion to the working tree
# you may need to change the paths to be checked out
# be careful not to accidentally overwrite unsaved work
git checkout -- .
于 2014-10-08T16:39:33.197 に答える
3

別の方法(ファイルの現在のバージョンが元に戻そうとしているバージョンからそれほど遠くない場合)は、部分的に元に戻したいものの直前にコミットgit logのハッシュを取得することです(から)。次に、コマンドは次のようになります。

$ git checkout -p <hash_preceding_commit_to_revert> -- file/you/want/to/fix.ext

これにより、作業ツリー内のファイルは変更されますが、コミットは作成されないため、本当に詰め込んだ場合は、からやり直すことができますgit reset --hard -- file/you/want/to/fix.ext

于 2018-03-13T00:44:22.017 に答える
1

git-revert -nを使用してから、add--patchを使用してハンクを選択できます。

于 2011-01-25T16:28:50.653 に答える