Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
shari-sushi opened this issue Nov 22, 2023 · 9 comments
Open

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

shari-sushi opened this issue Nov 22, 2023 · 9 comments

Comments

@shari-sushi
Copy link
Owner

shari-sushi commented Nov 22, 2023

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

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

git reset --オプション

@shari-sushi
Copy link
Owner Author

shari-sushi commented Nov 22, 2023

issue/13-git_reset

\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
Copy link
Owner Author

コミット履歴
image

@shari-sushi
Copy link
Owner Author

shari-sushi commented Nov 22, 2023

\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
 + 56ce0ef...d4fe9b6 issue/13-git_reset -> issue/13-git_reset (forced update)
  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
Copy link
Owner Author

追記

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

@shari-sushi
Copy link
Owner Author

shari-sushi commented Nov 22, 2023

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

  1. 再度main.goに追記し、マージまで
  2. GitHubとgit logで状況確認
  3. git reset
     git reset --hard [タグ?]
     git push -f origin [branch]
  4. GitHubとgit logで状況確認

@shari-sushi
Copy link
Owner Author

\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
Copy link
Owner Author

shari-sushi commented Nov 22, 2023

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
Copy link
Owner Author

  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
Copy link
Owner Author

  1. 確認
  • 専用ブランチ
    GitHubのコードにはない
    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箇所記載
  • main branch
    ある
    image

プルリクにもある
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

〇結論

  • マージした場合、プルリクは残る。
  • 1つのブランチでgit resetし、各ブランチでpushすればコミット履歴等は消える(--allでも良い説有る?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant