Git和Github學(xué)習(xí)

如何使用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

  1. 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, run dir C:/Program\ Files/Sublime\ Text\ 3/sublime_text.exe. You should see sublime_text.exe listed. If you get the error No such file or directory, Sublime is located somewhere else for you and you'll need to find it. For example, it might be D:/Sublime\ Text\ 3/sublime_text.exe

  2. 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.

  3. Close and re-open Git Bash.

  4. 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 directorystaging area中修改的地方。

  • 而當(dāng)你將修改后的文件通過git add添加至?xí)捍鎱^(qū)時,需要運行git diff --staged來顯示你的修改(我理解為比較staging areaRepository中該文件的不同之處)。

  • 當(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ù)此問題,請完成以下步驟:

  1. 運行 git merge --abort,將文件恢復(fù)到你開始合并之前的狀態(tài)。

  2. 仔細(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)無空格粪躬。

  3. 在文件處于正確的狀態(tài)后,利用你所做的更改新建一個提交泳唠。

  4. 重新嘗試合并。

  5. 更多內(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)程版本庫

  1. 首先在GitHub上創(chuàng)建一個代碼庫

  2. 然后使用命令git remote查看和創(chuàng)建遠(yuǎn)程代碼庫

  3. 接著將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

  4. 再次運行git remote就可以看到origin遠(yuǎn)程代碼庫已經(jīng)創(chuàng)建了

  5. 為驗證添加的URL正確無誤,可以運行git remote -v,輸出更多信息

  6. 要向遠(yuǎn)程代碼庫發(fā)送更改拉讯,可以使用命令git push origin master涤浇,將本地代碼庫的master分支復(fù)制到遠(yuǎn)程代碼庫origin上

  7. 要從遠(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最新告丢。

感謝閱讀枪蘑!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市岖免,隨后出現(xiàn)的幾起案子岳颇,更是在濱河造成了極大的恐慌,老刑警劉巖颅湘,帶你破解...
    沈念sama閱讀 218,036評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件话侧,死亡現(xiàn)場離奇詭異,居然都是意外死亡闯参,警方通過查閱死者的電腦和手機(jī)瞻鹏,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,046評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來鹿寨,“玉大人新博,你說我怎么就攤上這事〗挪荩” “怎么了赫悄?”我有些...
    開封第一講書人閱讀 164,411評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我埂淮,道長姑隅,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,622評論 1 293
  • 正文 為了忘掉前任倔撞,我火速辦了婚禮讲仰,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘误窖。我一直安慰自己叮盘,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,661評論 6 392
  • 文/花漫 我一把揭開白布霹俺。 她就那樣靜靜地躺著柔吼,像睡著了一般。 火紅的嫁衣襯著肌膚如雪丙唧。 梳的紋絲不亂的頭發(fā)上愈魏,一...
    開封第一講書人閱讀 51,521評論 1 304
  • 那天,我揣著相機(jī)與錄音想际,去河邊找鬼培漏。 笑死,一個胖子當(dāng)著我的面吹牛胡本,可吹牛的內(nèi)容都是我干的牌柄。 我是一名探鬼主播,決...
    沈念sama閱讀 40,288評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼侧甫,長吁一口氣:“原來是場噩夢啊……” “哼珊佣!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起披粟,我...
    開封第一講書人閱讀 39,200評論 0 276
  • 序言:老撾萬榮一對情侶失蹤咒锻,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后守屉,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體惑艇,經(jīng)...
    沈念sama閱讀 45,644評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,837評論 3 336
  • 正文 我和宋清朗相戀三年拇泛,在試婚紗的時候發(fā)現(xiàn)自己被綠了滨巴。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片兢卵。...
    茶點故事閱讀 39,953評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖柠横,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情晨继,我是刑警寧澤紊扬,帶...
    沈念sama閱讀 35,673評論 5 346
  • 正文 年R本政府宣布餐屎,位于F島的核電站腹缩,受9級特大地震影響空扎,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜盘寡,卻給世界環(huán)境...
    茶點故事閱讀 41,281評論 3 329
  • 文/蒙蒙 一竿痰、第九天 我趴在偏房一處隱蔽的房頂上張望菇曲。 院中可真熱鬧常潮,春花似錦楷力、人聲如沸喊式。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,889評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽岔留。三九已至,卻和暖如春检柬,著一層夾襖步出監(jiān)牢的瞬間献联,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,011評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留里逆,地道東北人进胯。 一個月前我還...
    沈念sama閱讀 48,119評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像原押,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子诸衔,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,901評論 2 355

推薦閱讀更多精彩內(nèi)容