Reverting to a previous version,
- Viewing a previous version and returning
If you want to check the source of a previous commit, there are two ways.
- Reverting to a specific commit by viewing commit messages
git log- After entering the command, use the up and down arrow keys to find the desired version commit.
- Copy at least the first 4 digits of the hash code after the 'commit' phrase.
git checkout copied 4+ digit hash codegit checkout branch-name
- Stepping back version by version
git checkout head ~ 1- Reverts to one step before the latest commit.
- You can specify the desired number of steps by changing the '1'.
Reverting to a previous version #
Even if you want to revert permanently without the option to return, there are two methods, but
revertis generally recommended.
- Creating a new commit for the reverted version
git revert head ~ 1 혹은
git revert head 커밋해시코드
- Both
checkoutmethods explained earlier can be applied torevertin the same way. - When using the
revertcommand, a new commit is created for that specific commit version, so a commit message input window will appear.
- Deleting commits after the reverted version (Not Recommended)
git reset --hard head ~ 1 혹은
git reset --hard 커밋해시코드
- Deleting remaining added files
head #
head ~previous stephead ~ 2two commits agohead ~ 3three commits ago
Can be used in this way.
+ More detailed explanation of the difference between Revert and Reset #
git revertallows you to revert the state while preserving history.git resetdeletes history.
Use
resetif you don't need the past; userevertif you might need to keep it.
revertis more recommended.