Skip to the content.

What is Git & GitHub? – Revert

🇯🇵 日本語版:03_Revert.md

This section introduces git revert and explains how to undo files with Git.

Outline

Preparing for Time Travel

Local Git

In the previous section, you created, merged, and deleted the develop branch.

Let’s get down to just the main branch. If you still have the develop branch, please delete it.

  1. Move to the learning_git folder.

    cd ~/learning_git
    
  2. Check the current Git status.

    git status
    
    On branch develop
    Your branch is up to date with 'origin/develop'.
    nothing to commit, working tree clean
    
  3. Check the branches in the repository.

    git branch
    
    - develop
      main
    
  4. Since you are on the develop branch, switch to main.

    git checkout main
    
    Switched to branch 'main'
    Your branch is up to date with 'origin/main'.
    
  5. Delete the develop branch.

    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. Let’s also delete the develop_file.md file.

    rm develop_file.md
    

GitHub

  1. Go to the learning_git repository on GitHub.
    • github.com/UserName/learning_git
  2. Check whether there is only one branch.
    • github.com/UserName/learning_git/branches
  3. If there is a develop branch, delete the branch.
    • 03_Revert_DeleteBranch

Create Sample Files and Branches

Create files for time travel.

  1. Switch to the main branch.

    git checkout main
    
  2. Create a timeline branch.

    git checkout -b timeline
    
    Switched to a new branch 'timeline'
    
  3. Create the following files and commit them separately.

    • 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"
    

Revert: Rewind the Clock

View the History

You should have the following four files in the repository.

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

Visit the Past

git log --oneline

git checkout [commit hash]

Go Back One Commit

git revert HEAD

Go Back by Commit Hash

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

Notice the 7a5bbf4 (origin/timeline) Year 3 line. It means the Revert "Year 3" commit has not been applied on GitHub (origin) yet.

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]
A backward-moving undo that deletes commits. A forward-moving undo.
Goes back to a past [commit] and removes every commit made after it. Creates a new commit at a past [commit].
This cleans everything up. No commits are deleted.
However, the history of the deleted commits is lost. Use it for public or shared repositories.

03_Revert_GitRevert.png

Next Section

Git Cheat Sheet - 04_CheatSheet_EN.md

List of Lecture Guides

README_EN.md ⚙️