git checkout
Git checkout (Git结账)
Definition
Definition (定义)
The git checkout command switches branches or restores working tree files. It operates on files, commits, and branches. The git checkout command allows switching between multiple features in just a single repository.
Checking out branches
Checking out branches (签出分支)
The git checkout command works with the git branch command. It updates the files in the working directory to match the version stored in that branch telling Git to record all the new commits. The git checkout command can be associated with git clone. The git clone command operates to fetch code from a remote repository whereas git checkout operates to switch code versions on the local system.
Checking out existing branches
Checking out existing branches (查看现有分支机构)
Consider there is a repository that contains pre-existing branches. If you want to switch between these branches you can invoke git checkout. Executing git branch will display the list of available branches and the current branch name.
git branch
master
test_branch
feature_branch
git checkout feature_branch
Checking out new branches
Checking out new branches (查看新分支机构)
The git branch command is executed to create a new branch. However, git checkout -b argument creates a new branch and directly switch to it:
git checkout -b <new_branch>
In the given example, the -b flag tells Git to execute git branch <new_branch> before invoking git checkout <new_branch>.
git checkout -b <new_branch> <existing_branch>
The git checkout -b bases the new_branch off the current HEAD. In the given example, <existing_branch> will base new_branch off instead of the current HEAD.
Switching branches
Switching branches (切换分支)
Invoking the following will point HEAD to the tip of <branchname>:
git checkout <branchname>
The git reflog command can be executed to view the history. Read more about it on our git reflog.
Checking out remote branches
Checking out remote branches (签出远程分支机构)
It is common to use remote repositories when working with a team. Each remote repository can contain its own branches. So as to checkout a remote branch git fetch should be executed to fetch the contents of it:
git fetch --all
The modern versions of Git allow checking out the remote branch like a local branch:
git checkout <remote_branch>
While in the older versions of Git, you should create a new branch based on the remote one:
git checkout <remote_branch> origin/<remote_branch>
Then checkout a new local branch and reset it to the remote branches last commit with the git reset command:
git checkout -b <branchname>
git reset --hard origin/<branchname>
Detached HEADS
Detached HEADS (分离的头部)
“Detached HEAD” state allows you to check out commits and examine the repository’s older state without creating a local branch. The HEAD command updates the git checkout to point the specified branch or commit. No problem will occur, when HEAD points to a branch, but when it points to a commit, it moves to a “detached HEAD” state.
“Detached HEAD” state is a warning which informs that your activity is “detached” from the project’s development. Developing a feature in a “detached HEAD” state, no branch will allow you to get back to it. When checking out another branch you cannot reference your feature:
The development must take place on a branch not being on “detached HEAD” thus ensuring to have a reference to the new commits. But when you want to look at an old commit being on “detached HEAD” state does not really matters. (开发必须在不在“分离头”上的分支上进行,从而确保引用新提交。但是,当您想查看处于“分离头”状态的旧提交时,这并不重要。)