學習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。
相關: