git revert

Git revert is a mechanism to undo the changes in a safely manner. This method is said to be safe because it does not removes the commit from the history, instead it goes back to the previous changes and creates the new commit in the history.

This is useful when working with shared repository environment. If git reset is used which removes the commit from the history, pushing the changes give error as local repository is behind the commits.

⟩ git log --oneline --decorate --color
3a3c1b3 (HEAD -> master) something new in dir1/file
e076952 Create new file in dir2 with content good bye
fac1972 Removed "why not me" song
81c668d Added "why not me" song
b9aa371 Deleted dir2/fil
eee4a58 Added some more content to dir2/fil
1758893 Added some content to the dir2/file
e615279 deleted dir1/fie
e7b8888 setting up directories
~/Learning/Git/dir · (master)
⟩ cat dir1/file
something
hello world

~/Learning/Git/dir · (master)
⟩ echo bye world >> dir1/file

~/Learning/Git/dir · (master±)
⟩ git add dir1/file

⟩ git commit -m "bye world into dir1/file"
[master 37c834b] bye world into dir1/file
 1 file changed, 1 insertion(+)

~/Learning/Git/dir · (master)
⟩ git log --oneline --decorate --color
54f69d3 (HEAD -> master) bye world into dir1/file
3a3c1b3 something new in dir1/file
e076952 Create new file in dir2 with content good bye
fac1972 Removed "why not me" song
81c668d Added "why not me" song
b9aa371 Deleted dir2/fil
eee4a58 Added some more content to dir2/fil
1758893 Added some content to the dir2/file
e615279 deleted dir1/fie
e7b8888 setting up directories
⟩ git revert HEAD
[master 9fd4134] Revert "bye world into dir1/file"
 1 file changed, 1 deletion(-)

~/Learning/Git/dir · (master)
⟩ git log --oneline --decorate --color
9fd4134 (HEAD -> master) Revert "bye world into dir1/file"
54f69d3 bye world into dir1/file
3a3c1b3 something new in dir1/file
e076952 Create new file in dir2 with content good bye
fac1972 Removed "why not me" song
81c668d Added "why not me" song
b9aa371 Deleted dir2/fil
eee4a58 Added some more content to dir2/fil
1758893 Added some content to the dir2/file
e615279 deleted dir1/fie
e7b8888 setting up directories

References

  1. https://www.atlassian.com/git/tutorials/undoing-changes/git-revert