Git Branching

In this exercise,we use “git checkout” for switch to the new branch , but in backtracking lesson we use “git checkout” for restore the file in your working directory to look exactly as it did when you last made a commit.How can I distinguish these commands?
Exercises link:

  1. https://www.codecademy.com/paths/web-development/tracks/learn-git/modules/learn-git-git-branching-u/lessons/git-branching/exercises/git-checkout
  2. https://www.codecademy.com/paths/web-development/tracks/learn-git/modules/learn-git-git-backtracking-u/lessons/git-backtracking/exercises/git-checkout-1
1 Like

I’m only going through some of these myself at the minute so take this all with a pinch of salt (any advice may sit somewhere between simply inaccurate to wildly incorrect :wink:). From what I understand checkout is used to alter the state your current working directory. This can be switching to a new branch or reverting to your last commit (also allows changes on a smaller file by file basis).

You can also view previous commits on a branch but it gets a bit more complex (detached HEAD state if you want to look into it) because you can’t just start working from this point (or at least you’d no longer be making changes to an active branch). You’d need to either shift the branch to this commit (something like git reset) or you’d want a new branch at this point to start developing on.

Basic docs are linked below but a number of online queries delve into the detached HEAD state if you wanted more guidance-
https://git-scm.com/docs/git-checkout

3 Likes

Hey, when I went through the git module it didn’t sink in immediately. Not too long afterwards I ran into this CS50 video from Harvard (https://youtu.be/1u2qu-EmIRc) that I thought did a really nice job at concisely explaining it (with good visuals).

3 Likes