Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialprateekparekh
12,895 PointsWhat's the difference between git reset and git checkout?
As per the Git Acquainted workshop, it is clear that 1) Use git checkout to discard changes to an unstaged file
git checkout js/script.js
2) Use git reset to undo staging of a modified file
git reset HEAD js/script.js
HEAD is a reference to the last commit HEAD^ is a reference to the previous commit (as opposed to last commit)
Now, if I have modified a file, staged it (git add file1), committed it (git commit -m "Edit file1"), and want to unstage the file1
git checkout HEAD^ js/script.js
Any reason why I couldn't use reset above? git reset HEAD^ js/script.js
Finally, if I accidentally deleted all my files in my local repository, and want to revert back to my old state, I do the following:
git reset --hard HEAD^
I'm trying to understand when exactly to use checkout or reset?
1 Answer
Will Berlanga
18,825 PointsIt's important to remember that with git you are managing between what files are in the origin git repository (server) and what you have locally to your machine. Checkout is used to copy the files from the server to your local machine. Reset is used to get your local file back to the state of the what is on the server. These two ideas differ because of how Git manages state of your repository.