Difference between git checkout and git clone
This article discusses the difference between git checkout and git clone commands
in Git . If you are coming from an SVN environment, you might find yourself confused because git clone
the equivalent of in SVN is svn checkout
.
By the end of this article, you will have everything you need to navigate Git repositories using the git checkout
and commands.git clone
We'll start by defining each command and discussing the workflow. Let's jump right in.
git clone command
The git clone command has only one function. When invoked, it creates a copy of the remote repository in our local machine.
It accepts the URL of the remote repository as a parameter. Following is the general syntax of 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 an identical copy on our local machine?
In this case, we will use git clone
the command. As we mentioned, this command requires the URL of the remote repository.
If you are not sure how to get the URL, follow the steps below.
- Open your GitHub web account and navigate to the repository you want to clone.
- Click on the Code button and copy the URL according to your authentication protocol. In our case, we will use HTTPS proxy.
To clone the repository, open your terminal, navigate to the directory you wish to clone to and run the command as shown 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 command once in your project git clone
.
Let's continue using git checkout
the command.
git checkout command
In git clone
contrast to commands, git checkout
a command has more than one use and you probably use it every day while working on your projects.
Following are git checkout
three common uses of the command.
- Switching between branches
- Cancel changes
- Create a new branch
Create a branch using git checkout
We usually use git branch
the command to create a new branch. However, we can also use git checkout
the command to create a branch.
git branch
The command only creates a new branch, whereas git checkout
the command creates a new branch and switches to the newly created branch.
Let’s look at an example.
If we want to create a feature branch in git checkout
our newly cloned Sample-Repo using the command , how do we do it?
We will run the command as shown below.
$ git checkout -b feature
Switching between branches using git checkout
git checkout
Commands come in handy
when we want to switch between branches in Git . Let's say we want to switch back to the master branch, how would we go about it?
We will run git checkout
the command as shown below.
$ git checkout master
Use git checkout to cancel changes
We can also use git checkout
the command to cancel changes made to files that have not yet been committed. Let's look at an example.
We will change the run.py file in Sample-Repo . When running the git status command, we get:
We can see that Git has suggested git checkout
discarding the changes made to the run.py file using the command.
$ git checkout run.py
This will discard the changes made to the file and revert it to the last committed state.
Anyway, git checkout
unlike git clone
the command, git clone
the command will create a copy of the remote repository on our local machine, and we will most likely only use it once.
On the other hand, git checkout
commands have many uses and we use them every day.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Difference between Git Checkout --Track Origin/Branch and Git Checkout -B Branch
Publish Date:2025/04/03 Views:185 Category:Git
-
This article outlines the difference between the git checkout -b branch origin/branch and git checkout --track origin/branch commands. We use these two commands to check out remote branches in a Git repository. These two commands have the s
Git Cherry-Pick and Merge Workflow
Publish Date:2025/04/02 Views:138 Category:Git
-
This article discusses the workflow of git cherry-pick and git merge commands. We use these two commands to integrate changes from one branch to another branch in the Git version control system. However, some scenarios favor the use of one
Clone a private repository in Git
Publish Date:2025/03/29 Views:106 Category:Git
-
This article will teach you how to use Git to clone a private repository hosted on Github. Git is a version control system used to track changes in a project directory. Git uses commits for such purposes. Github provides Internet hosting fo
Various methods for removing local Git changes
Publish Date:2025/03/28 Views:194 Category:Git
-
This article discusses various methods we can use to discard local changes in Git. Assuming we have cloned a repository, we will see how to discard the following local changes. Phased changes Unstaged changes Untracked changes Committed cha
The difference between Git pull and Git clone
Publish Date:2025/03/26 Views:193 Category:Git
-
This tutorial will discuss the difference between the git clone and commands. git pull git pull Using commands in Git We use git pull the command to get updates from the remote to the local. This command will update the files in the local r
Git 拉取和 Git 克隆的区别
Publish Date:2024/02/04 Views:224 Category:Git
-
这篇简短的文章将解决 git clone 和 git pull 的区别。本教程将讨论 git clone 和 git pull 命令之间的区别。
克隆 Git 存储库的 Python 方法
Publish Date:2023/06/20 Views:277 Category:Python
-
让我们看看我们可以在 Python 环境中克隆 Git 存储库的不同方法。使用 PyCharm 克隆 Git 存储库
Git pull 和 Git clone 的区别
Publish Date:2023/04/07 Views:139 Category:Git
-
这篇简短的文章将解决 git clone 和 git pull 的区别。我们使用 git pull 命令从远程获取更新到本地。我们使用 git clone 命令从远程仓库中获取所有文件。
在 Git 中克隆一个私有仓库
Publish Date:2023/04/04 Views:462 Category:Git
-
本文将教你使用 Git 克隆托管在 Github 上的私有仓库。Git 是一个版本控制系统,用于跟踪项目目录中的更改。Git 将提交用于此类目的。