Reset & Revert commit
The git checkout
command is like a Git Swiss army knife
. Many developers thank it is overloaded , which is what lead to add addtion of git switch
and git restore commands .
We can use checkout to create branches , switch to new branches , restore files , and undo history .
We can use git checkout <commit-hash>
to view a previous commit ,
Remember, you can use the git log command to view commit hashes . We just need the first 7 digits of a commit hash .
Don't panic when you see the following message ...
Detached HEAD
you are in 'detached HEAD' state . You can look around , make experimental changes and commit them , and you can discard any commits you make in this state without impacting any branches by switching back to a branch
Define Detached HEAD
You have a couple options :
1.Stay in detached HEAD to examine the contents of old commits.Poke around,view the files,etc. 2.Leave and go back to wherever you were befor - reattach the HEAD . Reattach the HEAD 3.Create a new branch and switch to it . You can now make and save changes , since HEAD is no longer detached
Discarding changes
Suppose yout've made some changes to a file but don't want to keep them .To revert the back to whatever it looked like when you lst commited , you can use :
git checkout HEAD<filename>
to discard any changes in that file , reverting back to the HEAD
Anthor Options
Here's another shorted option to revert a files ... Rather than typing HEAD , you can subsitute -- followed by the file(s) you want to resote .
Restore
git restore
is a branch new git command that hellps with undoing operations .
Because it is so new , nost of the existing Git tutorials and books do not mention ,it is worth knowning !
Reacall that git checkout does a million different things, which many git user find very confusing.git restore
was introduced alongside git switch as alternatives to some of user for checkout
Options 1: Git restore <filename?
specific file you want to restore .
Options 2 : Git restore --source HEAD~1 <filename>
specific file you want to restore and at time you want to restore.
Unstaging files with restore
if you have accidentally added a file to your staging area with git add and you don't wish to include it the next commit , you can use git restore to remove it from staging .
Use the --staged option like this :
git restore --staged app.js
Git Reset
Suppose you've just made a couple of commits on the master branch , but you actually to make them on a separate branch . to undo those commits ,you can use git reset
`git reset <commit-hash>`
it will reset the repo back to a specific commit . the commit gone
`git reset --hard <commit-hash>`
it detele commit in repo
Git revert
Git revert similar to git reset in that they both "undo" changes , but they acccomplish in different ways.
Git reset acctually moves the branch pointer backward , eliminating commit .
git revert instead create a branch new commit which reverse/undos the changes from a commit. Because result in a new commit , you will prompted to enter a commit message
Last updated