【Git】Git如何clone别人的repo到自己的repo中

转载自:https://www.cnblogs.com/dersu/p/13045555.html ,仅作学习记录

如何拷贝一个他人的repo到自己的repo,并做修改。本文的目标是通过这个实践介绍Git command。

首先初始化Git协议栈,Mac通常预装Git协议栈,在Terminal输入:

git --version
git config --global user.name "Dersu-git"
git config --global user.email "myemailaddress"
  • git --version 显示目前的git协议栈版本。
  • git config 配置用户名和email。

如果需要换一个Github账号,在Mac需要删除在Keychain Access存储的账号和密码。

新建一个自己的repo

git init

git init 初始化一个本地的空的Git repository。之后每增加或修改一个文档,需要staging、commit。
在这里插入图片描述
参考Git版本管理图,我们可以直觉理解为:任何文档修改都必须在Git history文档(Hidden system folder)中登记,否则Git不认。
所以,我们可以理解为什么对于错误的文档删除操作,可用git checkout。——因为没登记过,Git所保留的是最近一次正确的files,checkout就好。

但本文不讨论新建一个repo,讨论如何用Git拷贝一个他人的repo到自己的Github账号下的repo。

原理是这样的

三个角色:「我的本地folder」、「Github上他人的repo」、「Github上我的Repo」。我用本地folder中转,将他人的repo拷贝至我的repo。

  • 1)新建「Github上我的Repo」。因为我需要获得其标识:URL。如还未注册Github账号,注册。
  • 2)Clone 「Github上他人的repo」。我从Github他人的repo url 用git clone到本地folder,重命名这个folder的名字:folder name=Github repo name.
  • 3)Push到「Github上我的Repo」。Git push所有内容到Github我的repo。
  • 4)修改文件。在本地folder修改文件,本地stage、commit,并push到github我的repo。

以下是具体Git command的步骤:

1)新建「Github上我的Repo」
在Github创建一个repo:travel-site
这是一个空的repo。

2)Clone 「Github上他人的repo」
创建一个本地文件夹

git clone https://github.com/LearnWebCode/travel-site-files
  • git clone:拷贝一个github的repo到本地。可以看到本地新建了一个文件夹travel-site-files。

rename文件夹的名字和Github上我的repo同名。

3)Push到「Github上我的Repo」
现在我们要把所有内容push到Github的repo上。

3.1. 这是一个clone的文件夹,Git默认远端是原repo,即Github上他人的repo

cd travel-site
git remote -v
origin https://github.com/LearnWebCode/travel-site-files.git (fetch)
origin https://github.com/LearnWebCode/travel-site-files.git (push)

3.2. git remote 设定远端repository是Github上我的repo

git remote set-url origin https://github.com/Dersu-git/travel-site.git

3.3. origin表示远端repo,即github repo的URL,master表示只有一个master branch,此时所有都上传。并没有staging

git push origin master

4)修改文件
在本地folder修改index.html文档,修改了页尾一个年份。

git status
git add -A
git commit -m 'Modify the year in IPR line.'
git push origin master
  • git status是查看有哪些文档被修改了。
  • git add是staging,哪些文档需要要放到staging area,准备commit,即准备正式提交修改。
  • git commit是本地repo正式修改。
  • git push是将本地repo的修改同步到Github repo。
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值