Git reset VS Git checkout

git reset <commit>
move the HEAD completely to that commit, just like back to that commit time. If you go back to some previous commits, and check in, it will create a new branch. 
git reset --soft OR git reset --soft HEAD #change in working directory and in stage area
git reset --mixed OR git reset --mixed HEAD OR git reset
#default behavior, change in working directory, unstaged
git reset --hard OR git reset --hard HEAD #discard all local change and reset to that commit), recommend to just reset to HEAD, if you reset to a previous commit and create a new commit, then it will branch off from origin/master and you will need to merge


### git checkout <commit> ###
this will the HEAD to that commit, but branch will remain pointing to latest commit, will create a detached HEAD situation, if commit based on HEAD and then switch to master branch, the commit will be lost.
common use: to test something or look at the code for some previous commit, after looking, then use git checkout master to go back to original place

### git reset HEAD [file] or git reset [file]
This will unstaged a file. git add [file] will add the file into the staged area, this command will unstage the file and put back into working directory
common use: if accidentally add a file to the staging area, now want to reset that file.
### git checkout -- [file] or git checkout HEAD [file]
Checkout the file from latest commit, it will overwrite and discard all the local changes.
common use: make some change of a file, but want to discard the changes now

ORIG_HEAD contain the previous HEAD when running some dangerous operation
git reset [commit] <-- this will set the ORIG_HEAD to be the HEAD before this operation
git reset ORIG_HEAD will go back to original state

results matching ""

    No results matching ""