Difference Between Git Checkout and Git Clone
This article discusses the differences between the git checkout
and the git clone
commands in Git. If you are coming from the SVN
environment, you may find yourself confused as the equivalent of git clone
in SVN
is svn checkout
.
By the end of this article, you will have all you need to navigate a Git repository with the git checkout
and git clone
commands.
We will start by defining each command and discussing the workflow. Let’s jump right in.
the git clone
Command
The git clone
command has only one function. When invoked, it creates a copy of a remote repository in our local machine.
It accepts a remote repository’s URL as a parameter. Here is the common syntax for the command.
$ git clone <URL>
Let’s look at an example.
Assuming we have a remote Sample-Repo
repository hosted on GitHub, how can we make a copy of the same on our local machine?
We will utilize the git clone
command in such a scenario. As we mentioned, the command requires the URL for the remote repository.
If you are unsure how to get the URL, follow the steps below.
- Open your GitHub web account and navigate to the repository you wish to clone.
- Tap the
Code
button and copy the URL depending on your authentication protocol. In our case, we will use theHTTPS
proxy.
To clone the repository, open your terminal, navigate to the directory you wish to clone to and run the command, as illustrated below.
$ git clone https://github.com/Wachira11ke/Sample-Repo.git
This will create a local copy of the remote repository on our machine. You will most likely use the git clone
command once in a project.
Let’s move on to the git checkout
command.
the git checkout
Command
As opposed to the git clone
command, the git checkout
command has more than one use, and you will probably use it daily when working on a project.
Here are three common uses for the git checkout
command.
- Switch between branches
- Cancel a change
- Create new branches
Create a Branch With git checkout
We usually use the git branch
command to create new branches. However, we can also use the git checkout
command to create a branch.
The git branch
command only creates a new branch, while the git checkout
command creates a new branch and switches to the newly created branch.
Let’s look at an example.
How do we go about it if we want to create a feature
branch in our newly cloned Sample-Repo
with the git checkout
command?
We will run the command, as shown below.
$ git checkout -b feature
Switch Between Branches With git checkout
The git checkout
command comes in handy when we want to switch between branches in Git. How would we go about assuming we want to switch back to the master
branch?
We will run the git checkout
command, as illustrated below.
$ git checkout master
Cancel a Change With git checkout
We can also use the git checkout
command to cancel changes made to a file that is not yet committed. Let’s look at an example.
We will change the run.py
file in our Sample-Repo
. On running the git status
command, we get:
We can see that Git has suggested using the git checkout
command to discard the changes made to the run.py
file.
$ git checkout run.py
This will discard the changes made to the file and revert it to its last committed state.
In conclusion, the git checkout
and git clone
commands differ. The git clone
command will make a copy of a remote repository in our local machines, and we will most likely use it only once.
On the other hand, the git checkout
command has several uses, and we will utilize it daily.
John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.
LinkedInRelated Article - Git Checkout
- Git Checkout VS Pull
- How to Checkout a Remote Git Branch
- How to Ignore Local File Changes in Git
- How to Rollback to an Old Commit in a Public Git Repository
- Difference Between Git Checkout --Track Origin/Branch and Git Checkout -B Branch Origin/Branch