191

コーディング中に、何が起こっているかを追跡するためにいくつかのファイルに print ステートメントを追加しました。

完了したら、一部のファイルの変更を元に戻して、実際に作業したファイルをコミットすることはできますか?

print in file を追加したとしますAが、 file を変更しBました。BコミットしたいものでありA、古い状態に戻したい.

4

6 に答える 6

297

これを行うには、ファイル A に行った変更に応じて、3 つの基本的な方法があります。変更をインデックスに追加したり、コミットしたりしていない場合は、checkout コマンドを使用するだけで変更できます。リポジトリと一致する作業コピーの状態:

git checkout A

すでにインデックスに追加している場合は、reset を使用します。

git reset A

コミットした場合は、revert コマンドを使用します。

# the -n means, do not commit the revert yet
git revert -n <sha1>
# now make sure we are just going to commit the revert to A
git reset B
git commit

一方、コミットしたものの、元に戻す必要のないかなり多くのファイルがコミットに含まれている場合、上記の方法には多くの「リセット B」コマンドが含まれる可能性があります。この場合、次の方法を使用できます。

# revert, but do not commit yet
git revert -n <sha1>
# clean all the changes from the index
git reset
# now just add A
git add A
git commit

別の方法でも、rebase -i コマンドを使用する必要があります。これは、編集するコミットが複数ある場合に役立ちます。

# use rebase -i to cherry pick the commit you want to edit
# specify the sha1 of the commit before the one you want to edit
# you get an editor with a file and a bunch of lines starting with "pick"
# change the one(s) you want to edit to "edit" and then save the file
git rebase -i <sha1>
# now you enter a loop, for each commit you set as "edit", you get to basically redo that commit from scratch
# assume we just picked the one commit with the erroneous A commit
git reset A
git commit --amend
# go back to the start of the loop
git rebase --continue
于 2009-06-01T02:16:14.540 に答える
7
git add B # Add it to the index
git reset A # Remove it from the index
git commit # Commit the index
于 2009-06-01T02:16:56.067 に答える
6

man git-checkout :git checkout A

于 2009-06-01T02:11:04.690 に答える
3

はい;

git commit FILE

FILE だけをコミットします。次に、使用できます

git reset --hard

他のファイルのローカル変更を元に戻す。

私が知らない他の方法もあるかもしれません...

編集: または、NicDumZ が言ったように、変更を元に戻したいファイルだけを git-checkout します (最善の解決策は、コミットするファイルまたは元に戻すファイルがあるかどうかによって異なります:-)

于 2009-06-01T02:13:49.473 に答える
1

「 gitadd <file>」(または「gitadd --interactive」、またはインタラクティブなコミットのオプションがある「git gui」)を使用して、コミットに必要な変更を単純にマークできないのはなぜですか。「gitcommit-a」の代わりに「gitcommit」を使用しますか?

あなたの状況では(あなたの例では)それは次のようになります:

prompt> git add B
prompt> git commit

ファイルBへの変更のみが許可され、ファイルAは「ダーティ」のままになります。つまり、作業領域バージョンの印刷ステートメントが残ります。これらの印刷ステートメントを削除する場合は、を使用するだけで十分です。

prompt> git reset A

また

prompt> git checkout HEAD -- A

コミットされたバージョン(HEADからのバージョン、つまり「git showHEAD:A」バージョン)に戻します。

于 2009-06-02T12:14:12.600 に答える