Skip to content

git-study-session-demo/case04

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 

Repository files navigation

間違えてコミットしてしまった、コミットする前時点に戻したい。

間違ったコミットを公開しているシナリオ

シナリオ3では、git resetを実行するまで、コミットをリモートにgit pushしていません。
もし、先にgit pushを実行していれば、git resetしてからの再度git pushはエラーで怒られる。
このエラーは既に公開されたコミットが改ざんされるというエラーになります。
問題がなければ、そのままgit push -fで無理やりリモートのコミット履歴を書き換えるのもできますが
シナリオ4では、公開したコミットのもう一つの「打ち消す」方法を紹介します。

ブラウザから操作

1.case03と同じ操作でcase04のリポジトリをフォークする

https://github.com/git-study-session-demo/case04 (略)

2.フォークしたリポジトリをローカルにクーロンして、その中に入る

git clone <your repository url> case04
cd case04

3.開発ブランチ01を作成する

git switch -c feature/case0401

4.サンプルファイルを追加して、そのファイルをコミットし、プッシュする

touch sample.txt
echo "hello, world" > sample.txt
git add .
git commit -m 'add sample.txt'

5.sample02.txtとconfig.txt のサンプルファイルを追加して、そのファイルをコミットする

touch sample02.txt
echo "hello, world" > sample02.txt
touch config.txt
echo "個人設定" > config.txt
git add .
git commit -m 'add sample02 file'

6.config.txt を間違ってコミットしたことをすぐに気付き、そのコミットを「打ち消す」

git logコマンドで打ち消したいコミットを確認する。

git log
git revert <打ち消したいコミットのコミットID>

そして、再度 sample02.txtのみをコミットする

touch sample02.txt
echo "hello, world" > sample02.txt
touch config.txt
echo "個人設定" > config.txt
git add sample02.txt
git commit -m 'add sample02 file'

7.開発ブランチをプッシュする

git push origin feature/case0401

8.case01と同様操作で、PRを作成し・マージを行う

(略)
mainブランチに sample.txtとsample02.txt ファイルは取り込んでいて、そして config.txt は取り込んでいないことを確認

9.ローカルの開発ブランチを削除

git branch -d feature/case0401

考える

git revertコマンドは、コミット履歴を改ざんしない形でソース変更を打ち消すことができる。
この場合、一連に変更した内容は、履歴の中に維持したままになるので、場合により大きな問題となる。
どういう場合で問題になるかを考えてみてください。

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published