Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

What's the difference between "git reset" and "git checkout"?


Asked by Barbara Sanford on Dec 04, 2021 Git



git reset is specifically about updating the index, moving the HEAD. git checkout is about updating the working tree (to the index or the specified tree). It will update the HEAD only if you checkout a branch (if not, you end up with a detached HEAD).
Subsequently,
1 git checkout modifies your working tree, 2 git reset modifies which reference the branch you're on points to, 3 git revert adds a commit undoing changes.
Just so, If git revert is a “safe” way to undo changes, you can think of git reset as the dangerous method. There is a real risk of losing work with git reset. Git reset will never delete a commit, however, commits can become 'orphaned' which means there is no direct path from a ref to access them.
Consequently,
This means executing git reset is equivalent to executing git reset --mixed HEAD. In this form HEAD is the specified commit. Instead of HEAD any Git SHA-1 commit hash can be used. This is the most direct, DANGEROUS, and frequently used option.
Additionally,
On the commit-level, resetting is a way to move the tip of a branch to a different commit. This can be used to remove commits from the current branch. For example, the following command moves the hotfix branch backwards by two commits. git checkout hotfix git reset HEAD ~ 2