shari-sushi / 0015Laboratory

My SandBox, PoC
0 stars 0 forks source link

git reset で履歴を残さずコミットを消す #13

Open shari-sushi opened 1 year ago

shari-sushi commented 1 year ago

git filterよりお手軽に消す方法?の動作確認。 傷が浅いときのみ有効。

  1. main.goに書き込みをする→コミット、プッシュ。を繰り返す。
  2. githubで状況確認
  3. git reset $ git reset --hard [タグ?] $ git push -f origin [branch]

git reset --オプション

shari-sushi commented 1 year ago

issue/13-git_reset

1.

\0015Docker> git checkout -b "issue/13-git_reset"
Switched to a new branch 'issue/13-git_reset'

\0015Docker> git add .

\0015Docker> git commit -m "1st"
[issue/13-git_reset 6106b2b] 1st
 1 file changed, 1 insertion(+)

\0015Docker> git push origin issue/13-git_reset
Enumerating objects: 9, done.
Counting objects: 100% (9/9), done.
Delta compression using up to 4 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (5/5), 507 bytes | 507.00 KiB/s, done.
Total 5 (delta 2), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
remote:
remote: Create a pull request for 'issue/13-git_reset' on GitHub by visiting:
remote:      https://github.com/sharin-sushi/0015docker/pull/new/issue/13-git_reset
remote:
To https://github.com/sharin-sushi/0015docker.git
 * [new branch]      issue/13-git_reset -> issue/13-git_reset

\0015Docker> git add .

\0015Docker> git commit -m "2nd"
On branch issue/13-git_reset
nothing to commit, working tree clean

\0015Docker> git push origin issue/13-git_reset
Everything up-to-date

\0015Docker> git add .

\0015Docker> git commit -m "3rd"
On branch issue/13-git_reset
nothing to commit, working tree clean
\0015Docker> git push origin issue/13-git_reset
Everything up-to-date

※2ndミスった

\0015Docker> git add .

\0015Docker> git commit -m "3rd"
[issue/13-git_reset 56ce0ef] 3rd
 1 file changed, 2 insertions(+)

\0015Docker> git push origin issue/13-git_reset
Enumerating objects: 9, done.
Counting objects: 100% (9/9), done.
Delta compression using up to 4 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (5/5), 479 bytes | 479.00 KiB/s, done.
Total 5 (delta 2), reused 0 (delta 0), pack-reused 0        
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To https://github.com/sharin-sushi/0015docker.git
   6106b2b..56ce0ef  issue/13-git_reset -> issue/13-git_reset
shari-sushi commented 1 year ago

2.

コミット履歴 image

shari-sushi commented 1 year ago
  1. 
    \0015Docker>  git reset --hard d4fe9b65280bcf557f39c85968a39d9c812ab9b1
    HEAD is now at d4fe9b6 .gitignoreのtest0015Nextの.env2箇所記載

\0015Docker> git push -f origin issue/13-git_reset Total 0 (delta 0), reused 0 (delta 0), pack-reused 0 To https://github.com/sharin-sushi/0015docker.git

  1. 結果

コミット履歴 image

image

git log

\0015Docker> git log
commit d4fe9b65280bcf557f39c85968a39d9c812ab9b1 (HEAD -> issue/13-git_reset, origin/issue/13-git_reset)
Author: sharin-sushi <susi.imo.niku@gmail.com>
Date:   Tue Nov 21 00:40:36 2023 +0900

    .gitignoreのtest0015Nextの.env2箇所記載

commit 254e9f45f6c8efd4633cb31e00ada52c942d5193
Author: sharin-sushi <susi.imo.niku@gmail.com>
Date:   Tue Nov 21 00:39:32 2023 +0900

    .gitignoreのtest0015Next/.envに内容記載

結論

shari-sushi commented 1 year ago

追記

〇疑問点 マージしてた場合、  ・両方のブランチから消えるのか(多分消えない?そのまま残りそう?)  ・プルリクはどうなるのか(残りそう) .gitignoreで追跡から外されている場合  ・一時的に追跡できる状態にした方が良い?

shari-sushi commented 1 year ago

ーーーーーーーーーーーーーーーーーーーーー 追加実験 ・マージしてた場合

  1. 再度main.goに追記し、マージまで
  2. GitHubとgit logで状況確認
  3. git reset  git reset --hard [タグ?]  git push -f origin [branch]
  4. GitHubとgit logで状況確認
shari-sushi commented 1 year ago
  1. \0015Docker> git add .
    \0015Docker> git commit -m "mergeしてgit reset 試す"
    [issue/13-git_reset f948119] mergeしてgit reset 試す
    1 file changed, 1 insertion(+)
    \0015Docker> git push origin issue/13-git_reset
    Enumerating objects: 9, done.
    Counting objects: 100% (9/9), done.
    Delta compression using up to 4 threads
    Compressing objects: 100% (4/4), done.
    Writing objects: 100% (5/5), 526 bytes | 526.00 KiB/s, done.
    Total 5 (delta 2), reused 0 (delta 0), pack-reused 0
    remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
    To https://github.com/sharin-sushi/0015docker.git
    d4fe9b6..f948119  issue/13-git_reset -> issue/13-git_reset

    mainにマージした 2 parents 05e8224 + f948119 commit 3bb3044 url欄からコピーした

image

shari-sushi commented 1 year ago

2. mainにも専用branchにもある image image

プルリクから辿れた image

git logに記載有

\0015Docker> git log
commit f948119559260f1f32a71a7ebbffa3584f1eff22 (HEAD -> issue/13-git_reset, origin/issue/13-git_reset)
Author: sharin-sushi <susi.imo.niku@gmail.com>
Date:   Wed Nov 22 19:06:52 2023 +0900

    mergeしてgit reset 試す

commit d4fe9b65280bcf557f39c85968a39d9c812ab9b1
Author: sharin-sushi <susi.imo.niku@gmail.com>
Date:   Tue Nov 21 00:40:36 2023 +0900

    .gitignoreのtest0015Nextの.env2箇所記載

検索にはヒットしなかった image

shari-sushi commented 1 year ago
  1. 実施
    E\0015Docker> git reset --hard d4fe9b65280bcf557f39c85968a39d9c812ab9b1
    HEAD is now at d4fe9b6 .gitignoreのtest0015Nextの.env2箇所記載
    \0015Docker>  git push -f origin issue/13-git_reset
    Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
    To https://github.com/sharin-sushi/0015docker.git
    + f948119...d4fe9b6 issue/13-git_reset -> issue/13-git_reset (forced update)
shari-sushi commented 1 year ago
  1. 確認

git logにはない

\0015Docker> git log
commit d4fe9b65280bcf557f39c85968a39d9c812ab9b1 (HEAD -> issue/13-git_reset, origin/issue/13-git_reset)
Author: sharin-sushi <susi.imo.niku@gmail.com>
Date:   Tue Nov 21 00:40:36 2023 +0900

    .gitignoreのtest0015Nextの.env2箇所記載

プルリクにもある image

なら、mainでもgit resetやってみたら…?

\0015Docker> git checkout main
Switched to branch 'main'

checkout時点でVSCode上には無かった image

ということはorigin上のmainには既にない? ならばpushするだけで済むはず…

\0015Docker>  git push -f origin main
Enumerating objects: 11, done.
Counting objects: 100% (11/11), done.
Delta compression using up to 4 threads
Compressing objects: 100% (5/5), done.
Writing objects: 100% (6/6), 627 bytes | 627.00 KiB/s, done.
Total 6 (delta 3), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (3/3), completed with 3 local objects.
To https://github.com/sharin-sushi/0015docker.git
 + 3bb3044...05e8224 main -> main (forced update)

GitHub上の コードからも、histryではコミットが履歴すら無く。

コード image

image image

プルリクからは辿れる image

〇結論