Git switch and git checkout – How to switch branches in git | refine

Introduction

When you work on a

project, you typically work on more than one branch at a time. It also changes branches frequently based on priorities. Efficient branch switching is important to safely switch from a branch and confirm changes to the desired branch. The most famous command to switch branches has always been git checkout, however, more recent versions of Git divided its features into specific commands. Today, we’ll go over different use cases and examples for using git checkout and git switch. We’ll also go over some of Git’s similar commands. After reading this article, you will have a solid knowledge of how to change branches in Git and what their complementary commands are.

Note that the git checkout command is a multi-function command

that performs multiple functions such as:

• If it is a local branch or an explicit remote branch, it will switch to it • If it is a

tracked path, reset it • If it is a remote branch, it will create a tracking branch and switch to it Let’s review some examples of changing branches through git checkout, And then we’ll address the use of the Git switch.

Steps we’ll cover:

Using git checkout to switch branches Switch to an existing branch Switch

    • to a new branch Switch to a remote branch
    • Using
  • git switch

  • vs git
    • checkoutWhy was git switch needed?
  • Difference between git checkout and git reset Difference between git checkout and git

  • restore Difference
  • between git checkout and git

  • Clone

Use git checkout to

change

branches The git

checkout command allows you to navigate between different branches created through the git branch command. When you check out a branch, it updates all the files in its working directory to match the version stored in that branch. It also informs Git to preserve all new commits in that branch.

Let’s try different versions of the git checkout command.

Switch to an existing

branch

First, get the list of branches through the git

branch

The “*” shows the currently selected branch, which is “test_branch”. Now let’s switch to BranchB.

To confirm the successful branch change, run the git branch and you will see that your current branch is now BranchB

Switch to a new branch The git checkout command also comes with a “-b” argument that creates

a new branch

and automatically switches to it. Let’s give it a try.

The previous example shows that the newly created branch is also the currently selected branch. When switching branches using git checkout, you might see an error like the following.

The above error appears when you have changed a file, and the branch you are switching to also has changes for the same file (since the last merge point). Git will not allow you to switch branches until you do one of the following:

Use stash to save your changes locally temporarily without confirmation • Force checkout, which will discard your local changes • Commit your changes and then update this commit with additional changes (you can modify commits in Git until they are pushed)

Switch to a branch

remote To retire a remote branch, you’ll need to get the contents of the branch using git fetch -all first. Then, use the same git checkout RemoteBranchName command to switch to the remote branch. You may have noticed that it is the same command used to switch to a local branch.

If you want to switch to a remote branch

that does not exist as a local branch in your local working directory, you can simply run git switch remoteBranch. When Git cannot find this branch in its local repository, it will assume that you want to retire the respective remote branch with the same name. Next, you’ll create a local branch with the same name. You’ll also set up a tracking relationship between your remote and on-premises branch so that git pull and git push work as intended.

Using git switch

vs

git checkout The git switch command replaced git checkout

in 2020, although git checkout is still a supported command. The git checkout command performs two functionalities; “Change branch” and “Restore work tree files”. To separate these two functionalities, Git introduced the git switch command, which replaces the “switch branch” function of “git checkout”.

Why was git switch needed?

Suppose you have a

file named “test.txt” and at the same time you have a branch called “test”. If you are in the master branch and want to checkout to fork “test”, you would use the command “git checkout test” but this would remove the “test” file, this is where the git switch comes in.

• Git Switch Test will switch to the “Test” branch even if it has a “Test” file

• Git Restore will discard uncommitted local changes in the “Test” file even if it has a “Test” branch.

Let’s try this command

.

The above command works in the same way as the switched branches of git checkout. Switching to a branch that does not exist will generate an error

😛 to

create a new branch and change it in one go, try the following example:

To verify, simply run the git branch command to

see if your current branch has been successfully changed to the newly created branch

.

Another interesting argument of this command is git switch -. If you have to frequently switch between two branches and typing the branch name each time is cumbersome, you can use the git switch version, which switches to the previously retired branch. Let’s give it a try.

Difference between

git checkout and git reset git reset moves the current branch reference

, while git checkout only moves the header instead of the current branch reference.reset resets the index without changing the work tree. The following example will reset the index to match HEAD, without touching the work tree:

Note that you will use reset to undo staging a modified file. Checkout is mainly used with a branch, tag or confirmation. It will reset HEAD and index to a specified commit, and perform check-out of the index in the worktree at the same time. It is mainly used to discard changes to non-preconfigured files.

If your HEAD is currently configured in the master branch, running git reset 8e3f6r5 will point the master to “9e5e6a4”. Checkout, on the other hand, changes the head itself.

Difference between

git checkout and git restore git

restore was introduced when git checkout functionality was split into two separate commands git switch and git restore. In addition to changing branches, git checkout can also restore files to a specific commit state. The latter functionality has been extracted in git restore.

Git Restore restores files in the worktree from the index or any other commit that you specify. You can also use it to restore files in the index of some other commit. Note that it does not update its branch. You would use git restore to roll back uncommitted changes. These changes can be in the form of updating in your working copy or content in your index (i.e. staging area).

The following command will restore “test.txt” in the index to match the version in HEAD. Basically, you’re telling Git to copy from HEAD to the staging area/index, which is how Git reset works. git

restore -staged test.txt If you want to restore

both the index and the worktree, you must use the following version: git restore -source=HEAD -staged -worktree test.txt

Difference between git checkout and git Clone git clone is

used to retrieve repositories that you don’t have. You will get your repositories from the remote git server. Git Checkout is a powerful command with different uses, such as changing branches in your current repository and restoring archive files from a particular revision.

Contact US