The git reset command is a command that moves the commit position of the current branch.
git reset commit ID
Depending on how you use it, the git reset command will assume that you haven't changed the current working directory, so I'll write about this first.
--hard option undo all working directory changes
The "--hard" option of the git reset command undoes any changes made to the working directory.
# Revert all changes made to the working directory # Change commit position git reset --hard commit ID
So, when using the git reset command, first think carefully about whether you want to move only the commit position or revert the changes you made to your working directory.
I want to undo all the changes I made to the working directory
If you have changed the source code and undoed the changes you made to the working directory, specify the "--hard" option. If the commit ID is omitted, it reverts to the latest commit position.
#I want to undo all the changes I made to the working directory git reset --hard
I want to keep the changes I made to the working directory and revert to the previous commit
If you accidentally commit, you may want to keep the changes you made to your working directory and revert to your previous commit. In such a case, specify the previous commit ID without specifying the "--hard" option.
#I want to keep the changes I made to the working directory and revert to the previous commit git reset Previous commit ID
I want to restore a certain state in the past
There are times when you want to return to a certain state in the past, such as wanting to refer to an implementation that was deleted in the past again. In such a case, use the git branch command to create a branch for the past, and then use "git reset --hard" to change to the past state. It's safe to put it back.
#create an old branch git branch old Switch to the #old branch git checkout old # Make sure the current branch is old git branch # Confirm that the output is as follows main * old #Return to past state git reset --hard Commit ID at some point in the past
The "--grep" option of the git log command, which allows you to search for commit messages, is useful for finding where you want to go back.
git log --grep'fix foo'
If you found the commit ID and want to return to that state, use that commit ID. Also, you may want to go back one step before that. In that case, you can use "^" to express the previous commit.
#Revert to found commit ID git reset --hard commit ID #Revert to just before the found commit ID git reset --hard commit ID ^