What is the git checkout command?
We use the git checkout command to navigate between branches in a git repository. This command instructs Git on the branch where you want to apply the changes. We can also use this to restore the working tree files.
Note: A branch is a pointer to a commit.
Syntax
The syntax of the git checkout command is as follows:
git checkout [options] branch
branch: The branch name to which Git will be switched.
Options
The options we can use with git checkout command include:
-q: Quietly switch the branch without showing any output.--progress: Report the progress status.-B: Create a new branch if the branch does not exist.-b: Create a new branch.-f: Force the branch switch even if even if the working directory differs from theHEAD.--track: Set up the upstream configuration.--no-track: Do not set up the upstream configuration.--orphan: Create an orphan branch and switch to it. The first commit on this branch will be the root of a new history and will have no parent.
git clone vs git checkout
The git clone command is used to get the code from the remote repository to the local repository. On the other hand, the git checkout command is used to switch between code already on the local repository.
Code
Example 1
$> git branch
branch1
branch2
branch3
branch4
$> git checkout branch2
We use the git branch command to display a list of available branches. We then use the git checkout branch to switch to the branch named branch2.
Example 2
$> git checkout -b branch1
A branch branch1 is created, and git switches to it.
Free Resources