克隆到非空 Git 目录

本文将介绍如何将 Git 仓库克隆到非空文件夹。当你要将远程仓库中的文件与当前本地仓库中的文件合并时,此操作会派上用场。

在 Git 中克隆到非空 Git 目录

克隆远程仓库很容易。我们使用下面的命令。

git clone <repository-url> <directory>

这会将远程仓库克隆到指定目录。但是,该目录应该是空的。

如果你尝试在非空仓库中克隆,你将收到致命警告消息,如下所示。

pc@JOHN MINGW64 ~/Git (main)
$ git clone https://github.com/Wachira11ke/Delftscopetech.git
fatal: destination path 'Delftscopetech' already exists and is not an empty directory.

由于目录 Delftscopetech 已经存在并包含一些文件,我们不能使用 git clone 命令来克隆我们的仓库。

如果你不需要目录中的文件,你可以删除它们,但如果你想合并两个仓库中的文件,请使用下面的方法。

  1. 打开你要将远程仓库克隆到的目录。

    “bash
    cd / Delftscopetech1

  2. 使用此命令设置新仓库。

    git init
    
  3. 添加远程仓库

    git remote add origin https://github.com/Wachira11ke/Delftscopetech.git
    
  4. 拉取合并

    git pull origin main --allow-unrelated-histories
    

例子:

pc@JOHN MINGW64 ~/Git (main)
$ cd \Delftscopetech1
pc@JOHN MINGW64 ~/Git/Delftscopetech1 (main)
$ git init
Initialized empty Git repository in C:/Users/pc/Git/Delftscopetech1/.git/
pc@JOHN MINGW64 ~/Git/Delftscopetech1 (master)
$ git remote add origin https://github.com/Wachira11ke/Delftscopetech.git
pc@JOHN MINGW64 ~/Git/Delftscopetech1 (master)
$ git pull origin master --allow-unrelated-histories
fatal: couldn't find remote ref master
pc@JOHN MINGW64 ~/Git/Delftscopetech1 (master)
$ git pull origin main --allow-unrelated-histories
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 610 bytes | 3.00 KiB/s, done.
From https://github.com/Wachira11ke/Delftscopetech
 * branch            main       -> FETCH_HEAD
 * [new branch]      main       -> origin/main

我们已经成功地将我们的远程仓库克隆到了我们的本地仓库中,并使用了一个非空目录。