How to switch branch git

How to switch branch git

How to switch branch git

Switch to a specified branch. The working tree and the index are updated to match the branch. All new commits will be added to the tip of this branch.

THIS COMMAND IS EXPERIMENTAL. THE BEHAVIOR MAY CHANGE.

OPTIONS

Branch to switch to.

Name for the new branch.

Create a new branch named starting at before switching to the branch. This is a convenient shortcut for:

Switch to a commit for inspection and discardable experiments. See the «DETACHED HEAD» section in git-checkout[1] for details.

If
is not found but there does exist a tracking branch in exactly one remote (call it ) with a matching name, treat as equivalent to

If the branch exists in multiple remotes and one of them is named by the checkout.defaultRemote configuration variable, we’ll use that one for the purposes of disambiguation, even if the
isn’t unique across all remotes. Set it to e.g. checkout.defaultRemote=origin to always checkout remote branches from there if
is ambiguous but exists on the origin remote. See also checkout.defaultRemote in git-config[1].

The default behavior can be set via the checkout.guess configuration variable.

If you have local modifications to one or more files that are different between the current branch and the branch to which you are switching, the command refuses to switch branches in order to preserve your modifications in context. However, with this option, a three-way merge between the current branch, your working tree contents, and the new branch is done, and you will be on the new branch.

When a merge conflict happens, the index entries for conflicting paths are left unmerged, and you need to resolve the conflicts and mark the resolved paths with git add (or git rm if the merge should result in deletion of the path).

Quiet, suppress feedback messages.

Do not set up «upstream» configuration, even if the branch.autoSetupMerge configuration variable is true.

git switch refuses when the wanted ref is already checked out by another worktree. This option makes it check the ref out anyway. In other words, the ref can be held by more than one worktree.

EXAMPLES

The following command switches to the «master» branch:

Git Switch Branch – How to Change the Branch in Git

How to switch branch git. Смотреть фото How to switch branch git. Смотреть картинку How to switch branch git. Картинка про How to switch branch git. Фото How to switch branch git

Switching branches is something you’ll need to do often in Git.

To do this, you can use the git checkout command.

How to create a new branch in Git

This will create a new branch off of the current branch. The new branch’s history will start at the current place of the branch you «branched off of.»

Assuming you are currently on a branch called master :

How to switch to an existing branch in Git

How to checkout a specific commit

To checkout or switch to a specific commit, you can also use git checkout and pass the SHA of the commit instead of a branch name.

After all, branches are really just pointers and trackers of specific commits in the Git history.

How to find a commit SHA

One way to find the SHA of a commit is to view the Git log.

You can view the log by using the git log command:

On the first line of each commit after the word commit is a long string of characters and numbers: 94ab1fe28727.

This is called the SHA. A SHA is a unique identifier that is generated for each commit.

To checkout a specific commit, you just need to pass the commit’s SHA as the parameter to git checkout :

Note: You generally only need to use the first few characters of the SHA—as the first four or five characters of the string are most likely unique across the project.

What is a detached HEAD state?

The result of checking out a specific commit puts you in a «detached HEAD state.»

[a detached HEAD state] means simply that HEAD refers to a specific commit, as opposed to referring to a named branch

Basically, the HEAD (one of Git’s internal pointers that tracks where you are in the Git history) has diverted from the known branches, and so changes from this point would form a new pathway in the Git history.

Git wants to make sure that that is what you are intending, so it gives you a «free space» of sorts to experiment—as described by the output:

From this position you have two options:

Conclusion

The git checkout command is a useful and multi-purpose command.

You can use it to create new branches, checkout a branch, checkout specific commits, and more.

If you liked this tutorial, I also talk about topics like this on Twitter, and write about them on my site.

A simple web developer who likes helping others learn how to program.

If you read this far, tweet to the author to show them you care. Tweet a thanks

Learn to code for free. freeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers. Get started

freeCodeCamp is a donor-supported tax-exempt 501(c)(3) nonprofit organization (United States Federal Tax Identification Number: 82-0779546)

Donations to freeCodeCamp go toward our education initiatives, and help pay for servers, services, and staff.

How to switch branch git

Switch to a specified branch. The working tree and the index are updated to match the branch. All new commits will be added to the tip of this branch.

THIS COMMAND IS EXPERIMENTAL. THE BEHAVIOR MAY CHANGE.

OPTIONS

Branch to switch to.

Name for the new branch.

Create a new branch named starting at before switching to the branch. This is a convenient shortcut for:

Switch to a commit for inspection and discardable experiments. See the «DETACHED HEAD» section in git-checkout[1] for details.

If
is not found but there does exist a tracking branch in exactly one remote (call it ) with a matching name, treat as equivalent to

If the branch exists in multiple remotes and one of them is named by the checkout.defaultRemote configuration variable, we’ll use that one for the purposes of disambiguation, even if the
isn’t unique across all remotes. Set it to e.g. checkout.defaultRemote=origin to always checkout remote branches from there if
is ambiguous but exists on the origin remote. See also checkout.defaultRemote in git-config[1].

If you have local modifications to one or more files that are different between the current branch and the branch to which you are switching, the command refuses to switch branches in order to preserve your modifications in context. However, with this option, a three-way merge between the current branch, your working tree contents, and the new branch is done, and you will be on the new branch.

When a merge conflict happens, the index entries for conflicting paths are left unmerged, and you need to resolve the conflicts and mark the resolved paths with git add (or git rm if the merge should result in deletion of the path).

Quiet, suppress feedback messages.

Do not set up «upstream» configuration, even if the branch.autoSetupMerge configuration variable is true.

git switch refuses when the wanted ref is already checked out by another worktree. This option makes it check the ref out anyway. In other words, the ref can be held by more than one worktree.

EXAMPLES

The following command switches to the «master» branch:

How to switch branch git

Switch to a specified branch. The working tree and the index are updated to match the branch. All new commits will be added to the tip of this branch.

THIS COMMAND IS EXPERIMENTAL. THE BEHAVIOR MAY CHANGE.

OPTIONS

Branch to switch to.

Name for the new branch.

Create a new branch named starting at before switching to the branch. This is a convenient shortcut for:

Switch to a commit for inspection and discardable experiments. See the «DETACHED HEAD» section in git-checkout[1] for details.

If
is not found but there does exist a tracking branch in exactly one remote (call it ) with a matching name, treat as equivalent to

If the branch exists in multiple remotes and one of them is named by the checkout.defaultRemote configuration variable, we’ll use that one for the purposes of disambiguation, even if the
isn’t unique across all remotes. Set it to e.g. checkout.defaultRemote=origin to always checkout remote branches from there if
is ambiguous but exists on the origin remote. See also checkout.defaultRemote in git-config[1].

The default behavior can be set via the checkout.guess configuration variable.

If you have local modifications to one or more files that are different between the current branch and the branch to which you are switching, the command refuses to switch branches in order to preserve your modifications in context. However, with this option, a three-way merge between the current branch, your working tree contents, and the new branch is done, and you will be on the new branch.

When a merge conflict happens, the index entries for conflicting paths are left unmerged, and you need to resolve the conflicts and mark the resolved paths with git add (or git rm if the merge should result in deletion of the path).

Quiet, suppress feedback messages.

Do not set up «upstream» configuration, even if the branch.autoSetupMerge configuration variable is true.

git switch refuses when the wanted ref is already checked out by another worktree. This option makes it check the ref out anyway. In other words, the ref can be held by more than one worktree.

EXAMPLES

The following command switches to the «master» branch:

How can I switch to another branch in git?

Which one of these lines is correct?

And what is the difference between them?

How to switch branch git. Смотреть фото How to switch branch git. Смотреть картинку How to switch branch git. Картинка про How to switch branch git. Фото How to switch branch git

How to switch branch git. Смотреть фото How to switch branch git. Смотреть картинку How to switch branch git. Картинка про How to switch branch git. Фото How to switch branch git

14 Answers 14

Trending sort

Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.

It falls back to sorting by highest score if no posts are trending.

Switch to Trending sort

If another_branch already exists locally and you are not on this branch, then git checkout another_branch switches to the branch.

If neither exists, git checkout another_branch returns error.

git checkout origin/another_branch succeeds if origin/another_branch exists. It leads to be in detached HEAD state, not on any branch. If you make new commits, the new commits are not reachable from any existing branches and none of the branches will be updated.

UPDATE:

As 2.23.0 has been released, with it we can also use git switch to create and switch branches.

If foo exists, try to switch to foo :

If foo does not exist and origin/foo exists, try to create foo from origin/foo and then switch to foo :

More generally, if foo does not exist, try to create foo from a known ref or commit and then switch to foo :

If foo exists, try to recreate/force-create foo from (or reset foo to) a known ref or commit and then switch to foo :

which are equivalent to:

Try to switch to a detached HEAD of a known ref or commit:

If you just want to create a branch but not switch to it, use git branch instead. Try to create a branch from a known ref or commit:

Источники информации:

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *