git revert vs. git reset
I previously wrote about two different methods to rollback a database migration file. In this entry, I will explain two different methods to undo commits made to your application.
Groundwork:
$ git log --oneline
- This will show an abbreviated list of all recent commits made. The unique ID in front (the "hash" or "SHA") will be needed to undo any commits.
Situation 1: The commit has already been uploaded with $ git push.
$ git revert <SHA>;
- This will create a new commit that reverses all the additions and deletions done in that particular <SHA> commit.
Example:
1
2
3
4
5
6
7
My-Macbook:railsapp HomeUser$ git log --oneline
684ba64 edit README file
8bcd79c add skills, portfolio, and about sections
af0a023 navbar creation
bd1b6a4 add responsive intro screen
42d6aa8 first commit
My-Macbook:railsapp HomeUser$ git revert 684ba64
1
2
3
4
5
6
7
8
9
10
11
12
13
Revert "edit README file"
This reverts commit 684ba647629bdfd2e0c31216b13fb51673a0c0af.
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Changes to be committed:
# modified: README.rdoc
#
~
~
"~/Desktop/railsapp/.git/COMMIT_EDITMSG" 10L, 304C
The second code block shows the VIM editor in which the message on the new commit can be modified. Simply type “a” to append the message, edit the message, push “esc” when done, and then type “:exit” to save the changes and to commit the changes. More about VIM Editor commands here.
Afterwards, run $ git push
to update the new commit, keeping the mistaken commit in the history.
Situation 2: Removing local commits that have not been pushed.
$ git reset --hard <SHA>;
- This will undo any changes made to the working directory and rewind your application back to the specific <SHA>. It will also delete any commits made after the specific <SHA>.
Example:
1
2
3
4
5
6
7
8
9
10
11
12
My-Macbook:railsapp HomeUser$ git log --oneline
684ba64 edit README file
8bcd79c add skills, portfolio, and about sections
af0a023 navbar creation
bd1b6a4 add responsive intro screen
42d6aa8 first commit
My-Macbook:railsapp HomeUser$ git reset --hard af0a023
HEAD is now at af0a023 navbar creation
My-Macbook:railsapp HomeUser$ git log --oneline
af0a023 navbar creation
bd1b6a4 add responsive intro screen
42d6aa8 first commit
More information on undoing changes to your repository can be found at the GitHub website, including how to redo your undos.