【Github学习】通过clone一个repo介绍Git command

学习LearnWebCode(Brad Schiff先生)的Github教学视频 Git Tutorial Part 3: Installation, Command-line & Clone 和 Git Tutorial Part 4: GitHub (Pushing to a Server),如何拷贝一个他人的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远程版本管理流程
 
参考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上。
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)

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

 
git remote set-url origin https://github.com/Dersu-git/travel-site.git
git remote 设定远端repository是Github上我的repo。
 
git push origin master
如果origin表示远端repo,即github repo的URL,master表示只有一个master branch,此时所有都上传。并没有staging。
 
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。
 
相关:
 
posted @ 2020-06-04 19:19  Dersu  阅读(2462)  评论(0编辑  收藏  举报