Git克隆分支–如何克隆特定分支

本文介绍了Git的克隆和分支工作流程,重点讲解如何根据需求克隆Git的特定分支。文章首先概述了Git和GitHub的基本知识,接着详细说明了在不同操作系统上安装Git的方法。然后,作者解释了Git克隆和分支的概念,并提供了两种克隆特定分支的方法:一是克隆整个仓库后切换到所需分支,二是直接克隆单一分支。
摘要由CSDN通过智能技术生成

Unlike older centralized version control systems such as SVN and CVS, Git is distributed. Every developer has the full history and control of their code locally or remotely. They can also access or manipulate several parts of the code as they deem fit from different locations.

与SVN和CVS等较旧的集中版本控制系统不同,Git是分布式的。 每个开发人员都具有本地或远程代码的完整历史记录和控制权。 他们还可以视情况从不同位置访问或操纵代码的多个部分。

Since Linus Torvalds (the famous creator of the Linux operating system kernel) created Git in 2005 for Linux kernel development, it has become the most widely used modern version control system in the world.

自从Linus Torvalds(Linux操作系统内核的著名创建者)于2005年为Linux内核开发创建Git以来,它已成为世界上使用最广泛的现代版本控制系统。

In this article, I'll introduce you to the Git clone and Git branch workflows and I'll show you how you can clone a specific branch based on your needs. Let's begin! 😬

在本文中,我将向您介绍Git clone和Git分支工作流程,并向您展示如何根据需要克隆特定分支。 让我们开始! 😬

先决条件 (Prerequisites)

  • Basic knowledge of the terminal

    终端基础知识
  • Ability to type commands in the terminal

    能够在终端中键入命令
  • Git installed (I'll still show you how)

    安装了Git(我仍然向您展示如何)
  • A GitHub account

    GitHub帐户
  • A smile on your face (Put up that smile friend 😅)

    脸上露出笑容(把那个笑容friend起来)

Git和GitHub快速入门 (Quick Introduction to Git and GitHub)

According to Wikipedia,

根据维基百科

Git is a distributed version control system designed to track changes to a project (code) in software development. It is intended to enforce coordination, collaboration, speed, and efficiency among developers.

Git是一个分布式版本控制系统,旨在跟踪软件开发中对项目(代码)的更改。 它旨在在开发人员之间加强协调,协作,速度和效率。

GitHub, on the other hand, is a web-based hosting service for version control using Git. It offers all of the distributed version control and source code management functionality of Git as well as adding more features for computer code.

另一方面, GitHub是基于Web的托管服务,用于使用Git进行版本控制。 它提供了Git的所有分布式版本控制和源代码管理功能,并为计算机代码添加了更多功能。

如何在Windows上安装Git (How to Install Git on Windows)

Download and install the latest Git for Windows Installer here.

在此处下载并安装最新的Git for Windows Installer。

如何在Linux上安装Git (How to Install Git on Linux )

Here are the commands based on your Linux distro:

以下是基于您的Linux发行版的命令:

Debian或Ubuntu (Debian or Ubuntu)

sudo apt-get update
sudo apt-get install git

软呢帽 (Fedora)

sudo dnf install git

CentOS的 (CentOS)

sudo yum install git

Arch Linux (Arch Linux)

sudo pacman -Sy git

Gentoo (Gentoo)

sudo emerge --ask --verbose dev-vcs/git

如何在Mac上安装Git (How to Install Git on a Mac)

Download and install the latest Git for Mac installer here.

在此处下载并安装最新的Mac版Git安装程序

Or you can type this command:

或者,您可以键入以下命令:

brew install git

Now that we've got Git installed, let's move on to the tutorial.

现在我们已经安装了Git,让我们继续本教程。

Git Clone简介 (Introduction to Git Clone)

Git allows you to manage and version your project(s) in a "repository". This repository is stored on a web-based hosting service for version control, like GitHub.

Git允许您在“存储库”中管理和版本化项目。 该存储库存储在基于Web的托管服务上,用于版本控制,例如GitHub。

You can then clone this repository to your local machine and have all the files and branches locally (I'll explain more about branches soon).

然后,您可以将此存储库克隆到本地计算机上,并在本地拥有所有文件和分支(稍后我将详细介绍分支)。

For example, you can clone freeCodeCamp's repository with SSH like so:

例如,您可以使用SSH克隆freeCodeCamp的存储库,如下所示:

git clone git@github.com:freeCodeCamp/freeCodeCamp.git

Git分支介绍 (Introduction to Git Branches)

When working on a project, you will likely have different features. And multiple contributors will be working on this project and its features.

在处理项目时,您可能会具有不同的功能。 多个贡献者将致力于该项目及其功能。

Branches allow you to create a "playground" with the same files in the master branch. You can use this branch to build independent features, test new features, make breaking changes, create fixes, write docs or try out ideas without breaking or affecting the production code. When you're done, you merge the branch into the production master branch.

分支允许您使用master分支中的相同文件创建一个“操场”。 您可以使用此分支来构建独立功能,测试新功能,进行重大更改,创建修订,编写文档或尝试一些想法,而不会破坏或影响生产代码。 完成后,将分支合并到生产master分支中。

Branching is a core concept in Git which is also used in GitHub to manage workflows of different versions of one project. The master branch is always the default branch in a repository that is most often considered "production and deployable code". New branches like passwordless-auth or refactor-signup-ux can be created from the master branch.

分支是Git中的核心概念,它在GitHub中也用于管理一个项目的不同版本的工作流程。 master分支始终是存储库中的默认分支,该存储库最常被视为“生产和可部署代码”。 可以从master分支中创建新的分支,例如passwordless-authrefactor-signup-ux

如何克隆Git分支 (How to Clone Git Branches)

While you can clone repositories with the git clone command, keep in mind that this clones the branch and the remote HEAD. This is usually master by default and includes all other branches in the repository.

虽然可以使用git clone命令克隆存储库,但请记住,这会克隆分支和远程HEAD 。 默认情况下,这通常是master ,并且包括存储库中的所有其他分支。

So when you clone a repository, you clone the master and all other branches. This means you will have to checkout another branch yourself.

因此,当您克隆存储库时,您将克隆master和所有其他分支。 这意味着您将不得不自己签出另一个分支。

Let's say your task on a project is to work on a feature to add passwordless authentication to a user dashboard. And this feature is in the passwordless-auth branch.

假设您在项目上的任务是使用一项功能,以向用户仪表板添加无密码身份验证。 此功能位于无passwordless-auth分支中。

You really don't need the master branch since your "feature branch" will be merged into master afterward. How then do you clone this passwordless-auth branch without fetching all other branches with "a bunch of files you don't need"?

您真的不需要master分支,因为您的“功能分支”随后将合并到master 。 然后,如何克隆此无passwordless-auth分支而不用“不需要的一堆文件”来获取所有其他分支?

I created this sample repository to explain this. This repository holds a simple blog built with Nextjs and has four dummy branches:

我创建了此样本存储库以对此进行解释。 该存储库拥有一个使用Nextjs构建的简单博客,并具有四个虚拟分支:

  • master

  • dev

    开发者
  • staging

    分期
  • passwordless-auth

    无密码验证

In Nextjs, any file inside the folder pages/api is mapped to the /api/* path and will be treated as an API endpoint instead of a page. In our repository, I have created different dummy APIs in this directory to make each branch different.

在Nextjs中,文件夹pages/api内的任何文件都映射到/api/*路径,并将被视为API端点而不是page 。 在我们的存储库中,我在此目录中创建了不同的虚拟API 以使每个分支都不同。

The master branch holds the file pages/api/hello.js while passwordless-auth holds the file pages/api/auth.js. Each file just returns a dummy text response. See master's hello API response here (with a special message for you 😉).

master分支保存文件pages / api / hello.js,passwordless-auth保存文件pages / api / auth.js。 每个文件仅返回一个虚拟文本响应。 在此处查看master的hello API响应(带有一条特殊消息😉)。

Let's clone the repository:

让我们克隆存储库:

git clone git@github.com:BolajiAyodeji/nextjs-blog.git

This gives us access to all branches in this repository and you can easily toggle between each to see each version and its files.

这使我们可以访问该存储库中的所有分支,您可以轻松地在每个分支之间切换以查看每个版本及其文件。

git branch -a

Wondering where the remotes/origin/.. branches came from?When you clone a repository, you pull data from a repository on the internet or an internal server known as the remote. The word origin is an alias created by your Git to replace the remote URL (you can change or specify another alias if you want).

想知道remotes / origin / ..分支的来源是什么?克隆存储库时,您是从Internet或内部服务器(称为remote)上的存储库中提取数据的。 单词origin是Git为替换远程URL创建的别名(您可以根据需要更改或指定其他别名)。

These remotes/origin/.. branches point you back to the origin repository you cloned from the internet so you can still perform pull/push from the origin.

这些remotes / origin / ..分支将您指向从Internet克隆的原始存储库,因此您仍然可以从原始位置执行拉/推。

So when you clone master onto your machine, remotes/origin/master is the original master branch on the internet, and master is on your local machine. So you will pull/push from and to the remotes/origin/master.In summary Remote is the URL that points you to the repository on the internet while Origin is an alias for this remote URL.

因此,当将master克隆到计算机上时, remotes/origin/master是Internet上原始的master分支,而master在本地计算机上。 因此,您将在remotes/origin/master拉/推操作。总之, Remote是指向您指向Internet上存储库的URL,而Origin是此远程URL的别名。

如何克隆特定分支 (How to Clone a Specific Branch)

Now let's clone a specific branch from our demo repository. There are two ways to clone a specific branch. You can either:

现在,让我们从演示存储库中克隆特定分支。 有两种克隆特定分支的方法。 您可以:

  • Clone the repository, fetch all branches, and checkout to a specific branch immediately.

    克隆存储库,获取所有分支,并立即签出到特定分支。
  • Clone the repository and fetch only a single branch.

    克隆存储库,仅获取单个分支。

选项一 (Option One)

git clone --branch <branchname> <remote-repo-url>

or

要么

With this, you fetch all the branches in the repository, checkout to the one you specified, and the specific branch becomes the configured local branch for git push and git pull . But you still fetched all files from each branch. This might not be what you want right? 🌚

这样,您将获取存储库中的所有分支,签出到指定的分支,特定分支成为git pushgit pull的已配置本地分支。 但是您仍然从每个分支获取所有文件。 这可能不是您想要的吗? 🌚

Let's test it:

让我们测试一下:

git clone -b passwordless-auth git@github.com:BolajiAyodeji/nextjs-blog.git

This automatically configures passwordless-auth as the local branch but still tracks other branches.

这会自动将无passwordless-auth配置为本地分支,但仍会跟踪其他分支。

选项二 (Option Two)

git clone --branch <branchname> --single-branch <remote-repo-url>

or

要么

This performs the same action as option one, except that the --single-branch option was introduced in Git version 1.7.10 and later. It allows you to only fetch files from the specified branch without fetching other branches.

除了在--single-branch 1.7.10及更高版本中引入了--single-branch选项外,此功能与选项一相同。 它仅允许您从指定的分支获取文件,而无需获取其他分支。

Let's test it:

让我们测试一下:

git clone -b passwordless-auth --single-branch git@github.com:BolajiAyodeji/nextjs-blog.git

This automatically configures passwordless-auth as the local branch and only tracks this branch.

这会自动将passwordless-auth配置为本地分支,并且仅跟踪该分支。

If you run cd pages/api you'll find the auth.js file in the passwordless-auth branch as expected from the previous setup.

如果运行cd pages/api您将在无passwordless-auth分支中找到auth.js文件,这与以前的设置相同。

结论 (Conclusion)

You might be running out of internet or storage space but you need to work on a task in a specific branch. Or you might want to clone a specific branch with limited files for various reasons. Fortunately, Git provides you the flexibility to do this. Flex your muscles and try it out, there's much more "Git" to learn.One at a time, yeah? ✌🏾

您可能没有足够的Internet或存储空间,但需要在特定分支中处理任务。 或者,出于各种原因,您可能希望使用有限的文件克隆特定分支。 幸运的是,Git为您提供了执行此操作的灵活性。 弯曲肌肉并尝试一下,还有更多的“ Git”需要学习。一次,是吗? ✌🏾

翻译自: https://www.freecodecamp.org/news/git-clone-branch-how-to-clone-a-specific-branch/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值