如何从GitHub拉取我的项目?

148

我之前在Github上有一个项目,但是后来我清空了电脑。现在我想知道应该使用哪个git命令以我的用户名作为基础重新检出我的项目,以便我可以在我的账户下推送最新的更改到Github。

7个回答

176

为什么我总是看到像/path/repo.git这样的东西,但当我克隆本地repo时,我需要做/path/repo/.git。我做错了什么吗? - CatShoes

41

首先,您需要让git知道你是谁。从您的设置页面获取您的用户名和令牌。

然后运行:

git config --global github.user YOUR_USERNAME
git config --global github.token YOURTOKEN

如果你没有密钥的备份,你需要生成一个新密钥

然后,你应该可以运行:

git clone git@github.com:YOUR_USERNAME/YOUR_PROJECT.git

19

执行以下命令:

cd /pathToYourLocalProjectFolder

git pull origin master

9
楼主说:“然而,我把电脑清空了,/pathToYourLocalProjectFolder不再存在。” - Shahbaz
你应该将 'pathToYourLocalProjectFolder' 替换为你实际的本地项目文件夹路径。 - Udy Warnasuriya

8

如果您已经清除了计算机并想要重新检查您的项目,您可以通过进行以下初始设置来开始:

git config --global user.name "Your Name"
git config --global user.email youremail@domain.com

登录你的 Github 帐户,进入你要克隆的仓库,并复制“使用 HTTPS 克隆”的 URL。

即使上次设置了 SSH,你也可以使用 HTTPS 克隆远程仓库:

git clone https://github.com/username/repo-name.git

注意:

如果您之前为远程仓库设置了SSH,那么您需要将该密钥添加到您的PC上已知主机的ssh文件中;如果您没有这样做,并尝试执行git clone git@github.com:username/repo-name.git, 您将会看到类似下面的错误:

Cloning into 'repo-name'...
The authenticity of host 'github.com (192.30.255.112)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXDoJWGl7E1IGOCspZomTxdCARLviMw6E5SY8.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com,192.30.255.112' (RSA) to the list of known hosts.
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

在这种情况下,使用HTTPS比SSH更容易。


5

您可以通过两种方式来实现,

1. 将远程仓库克隆到本地主机

git clone https://github.com/user-name/repository.git

2. 将远程仓库拉到本地

首先,您需要通过以下方式创建本地 git 仓库,

git init or git init repo-name

然后,
git pull https://github.com/user-name/repository.git

现在,你的本地仓库中已经包含了远程仓库中的所有提交和分支。

愉快地编码,加油 -:)


3
以下是在Windows操作系统上生成SSH key的步骤:
  1. 打开Git Bash并生成ssh key

    粘贴以下文本,将其中的"your_email@example.com"替换为您的GitHub电子邮件地址。

    ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

    这将创建一个新的ssh key,并使用提供的电子邮件地址作为标签。

    Generating public/private rsa key pair.

    当提示输入“Enter a file in which to save the key”时,请按回车键。这将接受默认的文件位置。

    Enter a file in which to save the key (/c/Users/you/.ssh/id_rsa):[Press enter]

    在提示符下,输入一个安全的密码。有关更多信息,请参见“使用SSH密钥口令”。

    Enter passphrase (empty for no passphrase): [Type a passphrase] Enter same passphrase again: [Type passphrase again]

  2. 将key添加到SSH Agent中

    在Git Bash中键入以下内容(99999只是一个示例)以查看代理正在运行。

    eval $(ssh-agent -s) Agent pid 99999

    然后键入此命令。

    ssh-add ~/.ssh/id_rsa

    然后使用以下命令将SSH key复制到剪贴板。

    clip < ~/.ssh/id_rsa.pub

  3. 将SSH Key添加到Git账户中

    在GitHub网站上,单击右上角的图像,然后选择“Settings”。在随后的页面中,单击SSH和GPG keys选项。这将打开SSH key页面。单击“New SSH key”。在“Title”字段中,为新密钥添加一个描述性标签。将您的key粘贴到“Key”字段中。

  4. 克隆存储库

    打开VS Code(或任何具有命令提示符等的IDE / CLI)。使用cd命令进入要克隆的目录,并键入以下行。

    git config --global github.user yourGitUserName git config --global user.email your_email git clone git@github.com:yourGitUserName/YourRepoName.git

参考链接:https://help.github.com/articles/adding-a-new-ssh-key-to-your-github-account/

0
请注意,对于一个特定的分支,git克隆通常只使用一次(除非您想将项目复制到其他文件夹/分支中)。
在您的问题中,“拉取”这个词很重要,因为它也是一个git命令(git pull),它将提取远程存储库中所做的更改。
这个精度只是为了避免克隆和拉取之间的任何混淆。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接