如何使用Git和GitHub
本文主要是對Udacity學(xué)城上如何使用Git和GitHub這一課程的學(xué)習(xí)記錄。
附上我隨后對Git和Github學(xué)習(xí)的補(bǔ)充踏枣。
Course 1 : 操作提交歷史
1. 課程概述
Git
Git is a version control system
GitHub
GitHub is a code sharing and collaboration platform
What we'll cover?
Lesson 1 : install git + red-only usage
Lesson 2 : read + write git
Lesson 3 : share + collaborate
2. 練習(xí)查找較大文件之間的差異
Automatically Compare Files
- Windows —— FC (file compare)
FC favorite-app-old.html favorite-app.html
- Mac & Linux —— Diff (difference)
diff -u favorite-app-old.html favorite-app.html
3. Windows配置Git Bash打開Sublime Text 3
Find the directory where Sublime is located for you. For many people, this is
C:/Program\ Files/Sublime\ Text\ 3/sublime_text.exe
within Git Bash. To test this, rundir C:/Program\ Files/Sublime\ Text\ 3/sublime_text.exe
. You should seesublime_text.exe
listed. If you get the errorNo such file or directory
, Sublime is located somewhere else for you and you'll need to find it. For example, it might beD:/Sublime\ Text\ 3/sublime_text.exe
Run the following command in Git Bash:
echo 'alias subl="C:/Program\ Files/Sublime\ Text\ 3/sublime_text.exe"' >> ~/.bashrc
If Sublime was in a different directory for you in step 1, use that dictory.Close and re-open Git Bash.
Type
subl
in Git Bash.
4. Git中的手動提交
Commits
In Git terminology, user-created checkpoints are called commits.
Commits are the basic building blocks of Git, each one representing a version of the conetent at one point in time.
Git requires the user to supply commit message each time a commit is created.
5. 使用Git瀏覽歷史
cd version-control/asteroids
git log
diff --git a/game.js b/game.js
6. 多文件Git提交
Repository(版本庫)
Git calls the collection of multiple files that you want to track together a repository.
git log --stat
, the --stat option gives some statistics about which files have changed in each commit.
7. 克隆和探索版本庫
克隆版本庫
git clone https://github.com/udacity/asteroids.git
退出 git log
要停止查看 git log 的輸出躬厌,請按 q (表示退出)扛施。
獲得彩色輸出
git config --global color.ui atuo
git log
運行g(shù)it log會列出最近的提交以及相關(guān)的信息(包括提交ID)
git diff
運行g(shù)it diff(后跟兩個提交ID)會比較這兩個提交的代碼版本
git diff old.txt nex.txt
8. Git 錯誤和警告
Should not be doing an octopus(不應(yīng)執(zhí)行octopus)
Octpus是 Git 用來合并多個不同代碼版本的一種策略匙奴。如果在不合適的情況下嘗試使用該策略昌阿,則可能會出現(xiàn)此消息灶轰。
You are in 'detached HEAD' state(你處于“分離的 HEAD”狀態(tài))
Git 將你目前所在的提交稱為 HEAD刷钢“槌危可通過切換到前一個提交來“分離”HEAD赋除,這在下一個視頻中有說明。雖然此警告聽起來不太好敞嗡,但分離 HEAD 實際上不是壞事喉悴。Git 只是向你發(fā)出警告婚脱,以便你知道自己正在這樣做障贸。
Panic! (the 'impossible' happened)((天哪惹想!“不可能的事”發(fā)生了))
這是真正的錯誤消息嘀粱,但它不是由 Git 輸出的锋叨,而是由 GHC(編程語言 Haskell 的編譯器)輸出的。它僅在發(fā)生特別讓人驚訝的錯誤時才會出現(xiàn)宛篇!
9. 檢出代碼的舊版本
git checkout
Checking out a commit means resetting all of your files to how they were at the time that commit was made.
git checkout
Course 2 : 習(xí)題集1
1. Old File Plus Diff
2. Tracking Versions Using Git
3. Git Command Review
4. Behaviour Of Git Clone
5. Behaviour Of Git Log
6. Behaviour Of Git Diff
7. Behaviour Of Git Checkout
8. Cloning A New Repository
9. Identifying A Bug
10. Fixing The Bug
11. Identifying A Second Bug
12. Fixing The Second Bug
Course 3 : 創(chuàng)建和修改代碼庫
1. 版本庫有何特點娃磺?
Repository
For the most part it just looks like a normal directory on your computer. The only real difference is that git repositories store a bunch of metadata about the history of the repository since it was created.
顯示隱藏文件.git,使用命令ls -a
2. 初始化版本庫
git init
初始化或創(chuàng)建新的Git代碼庫
3. 檢查新版本庫
git status
To see that this is a git repository, you can run git status
. It shows which files have changed since the last commit.
4. 暫存區(qū)
Staging area
You can add files one at a time to the staging area.
git add cake-recipe.txt
git add frosting-recipe.txt
git status
注意:如果你意外地將某個文件添加到暫存區(qū)中叫倍,可以使用git reset
刪除它偷卧。
git reset cake-recipe.txt
5. 提交更改
在提交更改過程中,我遇到一些問題吆倦。
- 在第一次進(jìn)行
git commit
的操作時听诸,出現(xiàn)***Please tell me who you are.Run
的信息
說明Git Bsah沒有配置好,因此我們需要進(jìn)行如下操作來解決這個問題
git config user.name "your name"
git config user.email "your email@example.com"
- 在解決了這個問題之后,進(jìn)行
git commit
時出現(xiàn)無法啟動sublime的提示
經(jīng)過google后泛领,找到如下的解決辦法
git config --global core.editor "'D:/Sublime Text 3/sublime_text.exe' -w -n"
注意:此處篓像,在路徑的空格之前不需要添加\轉(zhuǎn)義字符盒粮!
6. 再次學(xué)習(xí)git diff
當(dāng)你找到導(dǎo)致錯誤的代碼部分時,你可以進(jìn)行修改讼油,修改完成后根时,通過輸入
git diff
可以比較working directory
和staging area
中修改的地方。而當(dāng)你將修改后的文件通過
git add
添加至?xí)捍鎱^(qū)時,需要運行git diff --staged
來顯示你的修改(我理解為比較staging area
和Repository
中該文件的不同之處)。當(dāng)你想要放棄工作目錄或暫存區(qū)的所有更改時,你可以輸入
git reset --hard
概疆,取消你的更改概耻。注意:在運行該命令時一定要小心,因為該命令無法撤銷奉呛,你需要確定自己不需要這些更改匙握,才能使用該命令。
注意:此時小渊,HEAD仍處于檢出舊提交時的“分離”狀態(tài)揍愁,而下面的命令將修復(fù)該問題朽缎。
git checkout master
7. 分支
Branches
When you need to update your records and use this new commit as your real version, Git allows you to create labels for your commits.
Master
Master is the name given to main branch in most Git repositories and every time you create a repository, Git creates a master branch for you.
The tip of the branch
The current last commit on a branch.
Attention: If you check out a branch and then make a commit, the branch label automatically updates to the new commit. That branch also stays checked out, so you don't have to check it out again.
8. 創(chuàng)建分支
(1). You can use git branch
to show yourself the current branches
(2). Run the following command to create the branch.
git branch easy-mode
(3). After create the easy-mode
branch, you can run git branch
to see what happens. The shar next to master means that master is the branch that is currently checked out.
(4). You need to run the command git checkout easy-mode
in order to check out the branch.
(5). Then you can modify the codes and commit it.
9. 用分支協(xié)同工作
You can visualize the branch structure via the following command:
git log --graph --oneline master coins
10. 再次學(xué)習(xí)分離的HEAD
當(dāng)你檢出頂點前的ID時贺氓,該ID上的提交的commit在檢出到Master上就會變的不可訪問(即
git log
無法顯示該ID上的commit)扬蕊,除非你記得該ID。不過你可以使用下面的命令在該ID處創(chuàng)建一個分支:
git checkout -b new_branch_name
11. 通過命令行合并
- You can run the following command to create a merged version of the two branches.
git merge master coins
- When you want to see the diff between this commit and its parent, you can run the following command.
git show 3884#commit_id
- Then if you don't need the
coins
branch, you can run the following command to delete it.
git branch -d coins
注意:由于是合并兩個分支践磅,因此在命令行中按何順序鍵入分支并不重要。關(guān)鍵是要記住,git merge
始終將所有指定的分支合并到當(dāng)前檢出的分支中田柔,并為該分支新建一個提交。
- 合并沖突
如果你收到類似以下所示的消息:
Auto-merging game.js
CONFLICT (content): Merge conflict in game.js
Automatic merge failed; fix conflicts and then commit the result.
這表示在你開始合并時,文件的狀態(tài)不同于 Caroline 的文件狀態(tài)。要修復(fù)此問題,請完成以下步驟:
運行
git merge --abort
,將文件恢復(fù)到你開始合并之前的狀態(tài)。仔細(xì)檢查文件的狀態(tài)亡鼠。如果在檢出 master 分支時運行
git log
抗蠢,則應(yīng)看到 Caroline 的“Add color”提交是第二新的提交潜叛,而最新的提交應(yīng)為你修復(fù) bullet 錯誤的提交。如果使用git diff
將你的提交與 Caroline 的提交進(jìn)行對比,你的提交應(yīng)在第 424 行引入this.delayBeforeBullet = 10
; 這行代碼。應(yīng)僅使用空格(無制表符)使該行的縮進(jìn)程度與其下面一行相同军俊,而且該行之后應(yīng)無空格粪躬。在文件處于正確的狀態(tài)后,利用你所做的更改新建一個提交泳唠。
重新嘗試合并。
更多內(nèi)容請參考此頁闲孤。
12. 解決合并沖突
當(dāng)你合并分支時可能出現(xiàn)沖突的現(xiàn)象:
git checkout easy-mode
git merge master easy-mode
在輸入上述代碼后,可能跳出如下的說明:
Auto-merging game.js
CONFLICT (content): Merge conflict in game.js
Automatic merge failed; fix conflicts and then commit the result.
于是掌桩,就需要手動去解決此沖突〕馄蹋可以將自己修改的代碼部分整合入其它人的代碼中燎字。在修改后妖异,進(jìn)行下一步的操作。
git status#查看git狀態(tài)
git add game.js
git status#查看git狀態(tài)
git commit
git log -n 1#查看第一條記錄
Course 4 : 使用GitHub協(xié)作
1. 添加遠(yuǎn)程版本庫
首先在GitHub上創(chuàng)建一個代碼庫
然后使用命令
git remote
查看和創(chuàng)建遠(yuǎn)程代碼庫接著將GitHub上的代碼庫當(dāng)作遠(yuǎn)程代碼庫,運行命令
git remote add origin https://gith ub.com/evansyangs/reflections.git
肢预,其中origin
為代碼庫名稱擅威,如果你只有一個遠(yuǎn)程代碼庫导盅,標(biāo)準(zhǔn)做法是將其命名為origin,同時要提供該遠(yuǎn)程代碼庫的URL再次運行
git remote
就可以看到origin遠(yuǎn)程代碼庫已經(jīng)創(chuàng)建了為驗證添加的URL正確無誤,可以運行
git remote -v
,輸出更多信息要向遠(yuǎn)程代碼庫發(fā)送更改拉讯,可以使用命令
git push origin master
涤浇,將本地代碼庫的master分支復(fù)制到遠(yuǎn)程代碼庫origin上要從遠(yuǎn)程代碼庫拉回主分支時,可以使用命令
git pull origin master
魔慷,將遠(yuǎn)程代碼庫origin上的內(nèi)容同步到本地代碼庫的master分支上
2. Fork版本庫
在GitHub上你可以Fork其它用戶的版本庫只锭,作為副本存放在自己的賬號上,然后可以使用git clone
克隆至自己的計算機(jī)中院尔,進(jìn)行本地的代碼修改蜻展,再push至遠(yuǎn)程代碼庫的副本上喉誊。
3. 更新遠(yuǎn)程分支的本地副本
如果你對每個代碼庫進(jìn)行了不同的更新,即在本地進(jìn)行了一項更新纵顾,然后在遠(yuǎn)程進(jìn)行了另一項更新伍茄,這將有可能產(chǎn)生沖突。
你可以通過運行命令
git fetch
更新遠(yuǎn)程分支的本地副本施逾,而實際的本地版本保持不動敷矫。然后你可以使用
git merge
將它們包含在master分支中。實際上當(dāng)你運行
git pull
時發(fā)生的正是這種情況汉额。首先曹仗,遠(yuǎn)程分支會被取回,更新遠(yuǎn)程分支的本地副本蠕搜,然后該分支與本地分支進(jìn)行合并怎茫。即
git pull origin master
=git fetch origin
+git merge master origin/master
4.創(chuàng)建拉取請求
GitHub提供了一種叫pull request
的協(xié)作方式,這可以請求對方將一些修改加入到master分支中妓灌,方便自己拉取轨蛤。
5. 保持Fork最新
你可以使用以下命令git remote add upstream https://github.com/udacity/create-your-own-adventure.git
與遠(yuǎn)程代碼庫連接,然后通過git pull upstream master
來更新本地庫旬渠,接著使用git log
查看修改俱萍,并合并修改,最后使用命令git push origin master
保持Fork最新告丢。