Javatpoint Logo
Javatpoint Logo

Git Checkout

In Git, the term checkout is used for the act of switching between different versions of a target entity. The git checkout command is used to switch between branches in a repository. Be careful with your staged files and commits when switching between branches.

Git Checkout

The git checkout command operates upon three different entities which are files, commits, and branches. Sometimes this command can be dangerous because there is no undo option available on this command.

It checks the branches and updates the files in the working directory to match the version already available in that branch, and it forwards the updates to Git to save all new commit in that branch.

Operations on Git Checkout

We can perform many operations by git checkout command like the switch to a specific branch, create a new branch, checkout a remote branch, and more. The git branch and git checkout commands can be integrated.

Checkout Branch

You can demonstrate how to view a list of available branches by executing the git branch command and switch to a specified branch.

To demonstrate available branches in repository, use the below command:

Now, you have the list of available branches. To switch between branches, use the below command.

Syntax:

Output:

Git Checkout

As you can see in the given output that master branch has switched to TestBranch.

Create and Switch Branch

The git checkout commands let you create and switch to a new branch. You can not only create a new branch but also switch it simultaneously by a single command. The git checkout -b option is a convenience flag that performs run git branch <new-branch>operation before running git checkout <new-branch>.

Syntax:

Output:

Git Checkout

As you can see in the given output, branch3 is created and switched from the master branch.

Checkout Remote Branch

Git allows you to check out a remote branch by git checkout command. It is a way for a programmer to access the work of a colleague or collaborator for review and collaboration. Each remote repository contains its own set of branches. So, to check out a remote branch, you have first to fetch the contents of the branch.

In the latest versions of Git, you can check out the remote branch like a local branch.

Syntax:

Output:

Git Checkout

In the above output, first, the fetch command is executed to fetch the remote data; after that, the checkout command is executed to check out a remote branch.

Edited is my remote branch. Here, we have switched to edited branch from master branch by git command line.

The earlier versions of Git require the creation of a new branch based on the remote. In earlier versions, below command is used to check out the remote branch.


Next TopicGit Revert




Help Others, Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA