Git, GitHubとは? - 巻き戻す
🇺🇸 English version: 03_Revert_EN.md
このセクションでは git revert
を紹介し, Git でファイルを元に戻すやり方について説明します
タイムトラベルの準備
Local Git
前のセクションでは, develop
ブランチを作成, マージ, 削除しました.
main
ブランチだけにしましょう.
develop
ブランチがまだある場合削除してください.
-
learning_git
フォルダーに移動しますcd ~/learning_git
-
Gitの現状を確認
git status
On branch develop Your branch is up to date with 'origin/develop'. nothing to commit, working tree clean
-
リポジトリのブランチを確認する
git branch
- develop main
-
develop
ブランチにいるので,main
に切り替えますgit checkout main
Switched to branch 'main' Your branch is up to date with 'origin/main'.
-
develop
ブランチを削除するgit branch --delete develop
warning: deleting branch 'develop' that has been merged to 'refs/remotes/origin/develop', but not yet merged to HEAD. Deleted branch develop (was 4f98baf).
-
develop_file.md ファイルも削除しましょう.
rm develop_file.md
GitHub
- Github の
learning_git
リポジトリに移動します.- github.com/
UserName
/learning_git
- github.com/
- ブランチが1つだけかどうかを確認する.
- github.com/
UserName
/learning_git/branches
- github.com/
develop
ブランチがあればブランチを削除
サンプル ファイル と ブランチ を作成する
タイムトラベルためにファイルを作成する
-
main
ブランチに切り替えるgit checkout main
-
timeline
ブランチを作成します.git checkout -b timeline
Switched to a new branch 'timeline'
-
次のファイルを作成して別々にコミットします.
yr_1
,yr_2
,yr_3
touch yr_1 git add yr_1 git commit -m "Year 1"
touch yr_2 git add yr_2 git commit -m "Year 2"
touch yr_3 git add yr_3 git commit -m "Year 3"
元に戻す: 時計を巻き戻す
歴史を見る
次の 4 つのファイルがリポジトリ内にあるはずです
ls
README.md yr_1 yr_2 yr_3
git log --oneline
- ブランチ上でのコミットを一覧表示します.
-
チェックアウトおよび復帰コマンドに提供されたコミットハッシュを使用する.
git log --oneline
7a5bbf4 (HEAD -> timeline) Year 3 5215f6d Year 2 f10f791 Year 1 03098e7 (origin/main, main) README file created
git push origin timeline
- これで
timeline
ブランチは GitHub にプッシュされています. - ブラウザでリポジトリにアクセスして確認します.
git push origin timeline
GitHub - timeline branch
GitHub - timeline’s commits
過去を訪ねる
git log --oneline
- ブランチ上でのコミットを一覧表示します
-
チェックアウトおよび復帰コマンドに提供されたコミットハッシュを使用する
git log --oneline
7a5bbf4 (HEAD -> timeline, origin/timeline) Year 3 5215f6d Year 2 f10f791 Year 1 03098e7 (origin/main, main) README file created
git checkout [commit hash]
- 作業ディレクトリを [
commit
]とまったく同じ状態に変換します. - これが元に戻すコミットかどうかを確認します.
-
この状況で行われた変更は保存されません.
git checkout f10f791
Note: switching to 'f10f791'. You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by switching back to a branch. If you want to create a new branch to retain commits you create, you may do so (now or later) by using -c with the switch command. Example: git switch -c <new-branch-name> Or undo this operation with: git switch - Turn off this advice by setting config variable advice.detachedHead to false HEAD is now at f10f791 Year 1
ls
README.md yr_1
1コミット文過去に戻る
git revert HEAD
-
1コミット前に戻ります.
git checkout timeline
Previous HEAD position was f10f791 Year 1 Switched to branch 'timeline'
ls
README.md yr_1 yr_2 yr_3
git revert HEAD
Removing yr_3 [timeline 450d385] Revert "Year 3" 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 yr_3
ls
README.md yr_1 yr_2
コミットハッシュで過去に戻る
git log --oneline
2fb96f6 (HEAD -> timeline) Revert "Year 3"
7a5bbf4 (origin/timeline) Year 3
5215f6d Year 2
f10f791 Year 1
03098e7 (origin/main, main) README file created
7a5bbf4 (origin/timeline) Year 3
行に注意してください.
これは, GitHub (origin) に Revert "Year 3"
コミットが実装されていないことを意味します。
git push
git push origin timeline
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 240 bytes | 240.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
remote: This repository moved. Please use the new location:
remote: https://github.com/ahandsel/learning_git_3.git
To https://github.com/ahandsel/learning_git_3.git
f0c82a0..53a0f3e timeline -> timeline
git log --oneline
727642d ( HEAD -> timeline \, origin/timeline ) Revert “Year 3”
f7cf1cb Year 3
f7fb07c Year 2
e4df7f2 Year 1
03098e7 ( origin/main \, main ) README file created
git revert f7fb07
Year 2 commit’s hash
git revert 5215f6d
Removing yr_2
[timeline 6c3367a] Revert "Year 2"
1 file changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 yr_2
git log --oneline
f3fc335 ( HEAD -> timeline ) Revert “Year 2”
727642d ( origin/timeline ) Revert “Year 3”
f7cf1cb Year 3
f7fb07c Year 2
e4df7f2 Year 1
03098e7 ( origin/main \, main ) README file created
Revert
ls
README.md yr_1
git push origin timeline
git revert [commit hash]
git revert [commit hash]
- 前進する取り消しコマンド
- 指定された[
commit
]によって加えられた変更を反転し, 新しいコミットとして結果を追加します.
Reset vs Revert
git reset [commit] |
git revert [commit] |
---|---|
元に戻す取り消し操作の削除 | 前進する取り消し操作 |
過去の [commit ] に戻り, それまでのすべてのコミットを削除します. |
過去の[ commit ]で新しいコミットを作成します. |
これはすべてをクリーンアップします. | コミットは削除されません. |
ただし, 削除されたコミットの履歴は失われます. | 公開/共有リポジトリに使用 |
次のセクション
Git CLI カンニングペーパー - 04_CheatSheet.md
講義ガイド一覧
README.md ⚙️