JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM > Git >

Difference between git checkout and git clone

Author:JIYIK Last Updated:2025/04/03 Views:

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 clonethe 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 checkoutand 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 clonethe 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.

  1. Open your GitHub web account and navigate to the repository you want to clone.
  2. 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 checkoutthe command.


git checkout command

In git clonecontrast to commands, git checkouta command has more than one use and you probably use it every day while working on your projects.

Following are git checkoutthree common uses of the command.

  1. Switching between branches
  2. Cancel changes
  3. Create a new branch

Create a branch using git checkout

We usually use git branchthe command to create a new branch. However, we can also use git checkoutthe command to create a branch.

git branchThe command only creates a new branch, whereas git checkoutthe 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 checkoutour newly cloned Sample-Repo using the command , how do we do it?

We will run the command as shown below.

$ git checkout -b feature

Feature branches

Switching between branches using git checkout

git checkoutCommands 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 checkoutthe command as shown below.

$ git checkout master

Use git checkout to cancel changes

We can also use git checkoutthe 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:

Working Tree

We can see that Git has suggested git checkoutdiscarding 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 checkoutunlike git clonethe command, git clonethe 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 checkoutcommands 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.

Article URL:

Related Articles

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 将提交用于此类目的。

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial