[Git] Git Checkout & Restore & Reset & Revert

1 minute read

Git Checkout & Restore & Reset & Revert

  • Git Checkout
  • Git Restore
  • Git Reset
  • Git Revert

Git Checkout

  • 1) go back to a previous commit
    • you are in 'detached HEAD' state message is shown
  • 2) discard changes made to the file
# git checkout {commit_hash}
# needs at least 7 digis of hash
git checkout 486f904

# git checkout HEAD~{number}
# move [number] of commits behind the current HEAD
git checkout HEAD~2

# cf. move to the previous branch
git switch -

# git checkout HEAD {filename}
# git checkout -- {filename}
git checkout HEAD dog.txt
git checkout -- dog.txt

Git Restore

  • helps with undoing operations
    • 1) unmodify file
    • 2) unstage file
  • introduced alongside with git switch as alternatives to some of the uses for git checkout
# git restore {file_name}
# this command is not undoable(cmd + z)
# same as git checkout HEAD dog.txt or git checkout -- dog.txt
git restore dog.txt

# git restore --source HEAD~{number} | {commit_hash} {file_name}
# restore the contents of {file_name} to its state form the commit prior to HEAD~{number}
git restore --source HEAD~1 dog.txt

# git restore --staged {file_name}
# same am git rm --cached dog.txt
git restore --staged dog.txt

Git Reset

  • undo commits
  • three options
  –soft –mixed –hard
HEAD to the certain commit to the certain commit to the certain commit
Staging Area Change X to the certain commit to the certain commit
Working Directory Change X Change X to the certain commit
Usage change to certain branch(commit on wrong branch) undo commit but keep changes undo all changes
# git reset {commit_hash}, default is --mixed
# same as git reset --mixed 4661ab9
git reset 4661ab9

Git Revert

  • undo commits
  • unlike git reset which actually moves the branch pointer backwards(eliminates commits), git revert creates a new commit which reverses/undoes the change from a commit
  • useful when other people already have the commits which should be undone
git revert

Tags:

Categories:

Updated: