Reputation: 113
What will happen if we checkout to a commit name instead of branch name?
Upvotes: 2
Views: 5019
Reputation: 1463
I disagree with the answers which say things like "they are the same" or "nothing bad will happen".
They are distinctly different in an important way. antak described it correctly in a post, but I want to emphasize the potential hazard, which is that you could lose track of new commits.
If you checkout by branch, then the branch pointer will point to the commit, and the next time you commit, the new commit will be added, and the branch pointer will move along with it. This is usually what people want to happen.
If you just checkout by commit, and then make a new commit, you do not have a branch pointing to that new commit. Only HEAD points to it.
If you were to checkout some other branch at this point, you could lose track of that new commit. (You would lose track of it if you don't know about special things like the ref-log).
As antak pointed out, you could make the new commit, and then create a branch at that point, but if you don't know the answer to the original question then you probably don't know how to do that, right?
Nutshell: you usually want to checkout the branch, unless you have a specific reason to do otherwise.
Upvotes: 6
Reputation: 207
Simply git checkout branch_name takes you to the lastest commit of that branch wheres as takes you to a commit you mention its name.
Upvotes: 0
Reputation: 20849
Both checkout <commit>
and checkout <branch>
are same in that they will make any necessary changes to files and directories in your working tree so that it ends up looking the same as the state of files recorded by the commit specified by the <commit>
or <branch>
argument.
In addition to this, the latter (<branch>
) also sets whatever branch you specified as the active branch. By doing so, it completes the concept of "being on a branch".
A branch is just a movable label given to a commit that automatically moves to any newer commits that you commit. These labels make commits more manageable and remember-able.
checkout <commit>
may put you in detached state. In this state, you don't get your custom label. But you can create commits and create a new branch at that position afterwards. Other than the fact that commits not labelled by a branch (or tag) will eventually, after many days, get garbage collected, whether or not work in detached state is a matter of personal preference and/or convenience.
Upvotes: 1
Reputation: 315
In short, nothing bad will happen. Checking out a branch
is easier and keeps you up to date (i.e. latest commit
). Checking out by commit
might or might not put you behind the latest state of the branch.
git checkout <branch>
will checkout <branch>
at it's latest commit, i.e. HEAD
will point to the last commit of that branch.
git checkout <commit>
is a bit different. If checking out <commit>
you could be pointing to the latest commit and thus you will be in sync with HEAD
. But you can also checkout
a previous commit as well, which would put you in a detached HEAD
state, i.e. HEAD
points to a different commit in your git history (most likely the latest). You can reset HEAD
too, to point to the current commit you have checked out.
You should generally checkout by <branch>
as you will always know you have the latest version of the branch (unless there were upstream changes, then you should git pull
).
Here is the documentation for git checkout
if you'd like to learn more.
Upvotes: 1