Skip to the content.

Git, GitHubとは? - 巻き戻す

🇺🇸 English version: 03_Revert_EN.md

このセクションでは git revert を紹介し, Git でファイルを元に戻すやり方について説明します

タイムトラベルの準備

Local Git

前のセクションでは, develop ブランチを作成, マージ, 削除しました.

main ブランチだけにしましょう.
develop ブランチがまだある場合削除してください.

  1. learning_git フォルダーに移動します

    cd ~/learning_git
    
  2. Gitの現状を確認

    git status
    
    On branch develop
    Your branch is up to date with 'origin/develop'.
    nothing to commit, working tree clean
    
  3. リポジトリのブランチを確認する

    git branch
    
    - develop
      main
    
  4. develop ブランチにいるので, mainに切り替えます

    git checkout main
    
    Switched to branch 'main'
    Your branch is up to date with 'origin/main'.
    
  5. 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).
    
  6. develop_file.md ファイルも削除しましょう.

    rm develop_file.md
    

GitHub

  1. Github の learning_git リポジトリに移動します.
    • github.com/UserName/learning_git
  2. ブランチが1つだけかどうかを確認する.
    • github.com/UserName/learning_git/branches
  3. develop ブランチがあればブランチを削除
    • 03_Revert_DeleteBranch

サンプル ファイル と ブランチ を作成する

タイムトラベルためにファイルを作成する

  1. main ブランチに切り替える

     git checkout main
    
  2. timeline ブランチを作成します.

     git checkout -b timeline
    
     Switched to a new branch 'timeline'
    
  3. 次のファイルを作成して別々にコミットします.

    • 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 push origin timeline

git push origin timeline

GitHub - timeline branch

03_Revert_timelineBranch

GitHub - timeline’s commits

03_Revert_timelineBranch_Commits

過去を訪ねる

git log --oneline

git checkout [commit hash]

1コミット文過去に戻る

git revert HEAD

コミットハッシュで過去に戻る

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

03_Revert_10.png

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]

Reset vs Revert

git reset [commit] git revert [commit]
元に戻す取り消し操作の削除 前進する取り消し操作
過去の [commit] に戻り, それまでのすべてのコミットを削除します. 過去の[ commit ]で新しいコミットを作成します.
これはすべてをクリーンアップします. コミットは削除されません.
ただし, 削除されたコミットの履歴は失われます. 公開/共有リポジトリに使用

03_Revert_GitRevert.png

次のセクション

Git CLI カンニングペーパー - 04_CheatSheet.md

講義ガイド一覧

README.md ⚙️