git checkout

git checkout another_branch

Performance under various conditions:

  • Local branches exist

Switch branch

  • The branch does not exist locally, but the remote repository does

The equivalent of

git checkout -b another_branch origin/another_branch
Copy the code

and

git branch -u origin/another_branch
Copy the code

Pull the latest branch from the remote to the local and switch to the branch.

  • Neither the local nor remote warehouse is available

An error.

git checkout origin/another_branch

Performance under various conditions:

  • There is no local branch but there is a remote repository

A commit that points to a floating HEAD, not to any branch, is not accessed under any branch.

HEAD points to the latest commit, but there is no branch to hold the commit, so there is a wandering state.

  • Errors will be reported in other cases.

git checkout origin master

This statement will report an error in most cases. If Origin is a revised version and another_branch is a file, it will check out the revised version of the file, but probably not what we expect. Origin is used primarily in Git Fetch, Git pull, and Git Push as an alias for a URL pointing to a remote repository.

git switch

  • Local branches exist
git switch foo
Copy the code
  • The local branch does not exist but the remote repository does
git switch -c foo origin/foo
# or simply
git switch foo
Copy the code
  • Under normal circumstances, iffooThe branch does not exist, try to create a new branch from an existing branch /commit and switch like this:
git switch -c foo <ref>
git switch -c foo <commit>
Copy the code

If we store a REPO in multiple remote libraries at the same time, we will have multiple local remote library aliases (such as GitHub and GitLab). In this case the same branches exist on different remote libraries, such as github_origin/foo and gitlab_Origin /foo. In this case, execute the command

git switch foo 
Copy the code

Git doesn’t know which remote library we’re pulling branches from. We need to specify which remote library to pull the branch from, and we need to pass

git switch -c foo origin/foo
git switch -c foo github/foo
Copy the code

To specify the remote library. Of course, if pull code is required from the same branch of two remote Repositories, it is a good idea to mark which branch of the remote repository. Such as:

git switch -c gitlab_foo origin/foo
git switch -c github_foo github/foo
Copy the code
  • If foo exists, recreate/force-create foo (or reset foo) from a known branch or commit, and then switch to foo:
git switch -C foo <ref>
git switch -C foo <commit>
Copy the code

The equivalent of

git switch foo
git reset [<ref>|<commit>] --hard
Copy the code
  • Switch to a free HEAD that is known to reference or commit
git switch -d <ref>
git switch -d <commit>
Copy the code
  • If you just want to create a branch but don’t want to switch to that branch, use branch. To create a new branch based on an existing branch:
git branch foo <ref>
git branch foo <commit>
Copy the code