Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+14 votes

Do git checkout/switch [branch] and git checkout [commit-hash] have different functions? How do you know which one to use and what are their functions?

asked in CSC 305 Fall 2024 by (1.4k points)

3 Answers

+6 votes
 
Best answer

git checkout or git switch [branch] moves you to the latest commit of the specified branch, allowing you to continue working on it. git checkout [commit-hash] checks out a specific commit, so that you are in a "detached HEAD" state where you're not on any branch. I think the detached Head state is only used to see or inspect you previous commits but anything you change here wont be saved

answered by (2.1k points)
selected by
+5 votes

I think it is just git checkout or git switch... and not actually any syntax of git checkout/switch. I believe they function basically the same (at least for our purposes).

answered by (4.5k points)
0 votes

git checkout [branch] or git switch [branch] moves to a specific branch, making it the active branch for new commits.
git checkout [commit-hash] checks out a specific commit in a detached HEAD state, allowing you to view or work on that commit without affecting branches.
Use switch for branches as it's more explicit and avoids detached HEAD confusion.

answered by (2.1k points)
...