Why do people not switch a specific commit after switching to a branch?

时间:2016-08-31 17:51:52

标签: git

From what I have seen, when people switch to an existing branch, they don't switch to a particular commit on the branch. Why not?

When we switch to an existing branch, is it correct that we automatically switch to the latest commit on the branch?

Is it true that people who are interested in a branch are only interested in the latest commit on the branch, and not in other commits on the branch?

What command is used for switching to a commit?

3 个答案:

答案 0 :(得分:4)

The branch is a pointer to a commit. So yes, when you checkout a branch you checkout the commit which the branch is pointing to, no other commit. The other older commits you see "in the same branch" are not pointed to by the branch at all. They are pointed to by their parent commits.

Someone checking out a branch might be interested in older commits but to say these commits are on the same branch is a bit misleading. Git doesn't have that kind of branches. The older commits are part of the commit history of the checked out commit, not of a branch.

To checkout a commit, just do git checkout <sha-1>. This will make your HEAD detached, which means that you don't have a branch checked out. The full commit history of the checked out commit is available of course, that has nothing to do with the branch.

答案 1 :(得分:1)

When we switch to an existing branch, is it correct that we automatically switch to the latest commit on the branch?

Yes, that's true.

Is it true that people who are interested in a branch are only interested in the latest commit on the branch, and not in other commits on the branch?

That's not true. If you switch to a branch with the purpose of making changes and then committing them, you almost certainly mean that those changes go into that very branch. If you switch to the latest commit of that branch, then you will be in detached HEAD state, and your commits will go elsewhere (into a kind of an anonymous branch).

答案 2 :(得分:1)

It depends. If you checkout branch then you selecting last available commit, if you only switch to one that was already checkout'ed, you will be selected the one you had working on or the last one of your checkout'ed to local machine (HEAD).

In most cases people interested in last commit, because they don't want to have an old version of project (maybe there is a bug or there is no feature). People may want to get older commit if they for example wants to make some feature in other branch which doesn't have some of newer features in current branch. Or to find out what was in this older commit's code.