7.10 0506 Github

GitHub簡介

大部分的Git操作都是在電腦完成
若要跟別人共同協作,有Git伺服器會比較方便

GitHub = 一款有Web介面的Git伺服器
GitLab
BitBucket

優點:

  1. 跟厲害的開發者們交朋友
  2. 開發者最好的履歷

開新專案

New repository,網頁版右上角 +

全新專案

…or create a new repository on the command line
echo "# 5x-demo" >> README.md
git init
git add README.md
git commit -m "first commit"

git remote add origin https://github.com/tingtinghsu/5x-demo.git
git push -u origin master

如果選擇httpshttps://github.com/tingtinghsu/5x-demo.git
在推送的過程中會需要輸入帳號密碼

如果選擇SSH ,git@github.com:tingtinghsu/5x-demo.git

現成專案

/*設定一個遠端節點*/
git remote add origin /*origin節點名字可以自己取*/
https://github.com/tingtinghsu/5x-demo.git

/*把本機的master推到遠端*/
git push -u origin master

在source tree設定

Repository -> Repository Settings ->
Remote name: origin
URL/Path: https://github.com/tingtinghsu/5x-demo.git

tingdeAir:git-rebase tingtinghsu$ git push -u origin master
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 440 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/tingtinghsu/5x-demo.git
   647145b..6cc2c94  master -> master

remote相關的git指令

git remote

git remote add origin + 某個網址

  • 新增一個叫做origin的遠端節點,這個節點會指向某個網址
  • origin可以代換為別的名字

git push

git push origin master

  • 要把本地的master分支,推往origin這個遠端節點,並且在遠端形成一個master分支
  • 當遠端節點的版本比較新的情況,使用git pullgit fetch

git pull vs git fetch

git fetch: 跟remote比對,將local沒有的檔案拉下來
git pull: git fetch + git merge

git fetch

git fetch origin master

  • origin這個遠端節點,抓上面master分支的內容,並且在我的電腦上建立一個origin/master分支

在CL

tingdeAir:git-rebase tingtinghsu$ git merge origin/master
Updating 6cc2c94..ea6fdcd
Fast-forward
 index.html | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

在source tree

在origin/master上按右鍵 -> merge ->
Are you sure you want to merge origin/master into your current branch?
-> 確定

  • master的貼紙移動到origin/master
  • 合併分支就是在移動貼紙

git pull

git pull origin master

  • origin這個遠端節點,抓上面master分支的內容,並且在我的電腦上建立一個origin/master分支
  • 同時與我本機的master分支進行合併

git clone + 某個Git網址

  • 要把某個git網址的內容,整個複製一份到我的電腦裡
tingdeAir:~ tingtinghsu
$ git clone https://github.com/tingtinghsu/learn-ruby-on-rails.git

Cloning into 'learn-ruby-on-rails'...
remote: Enumerating objects: 692, done.
remote: Total 692 (delta 0), reused 0 (delta 0), pack-reused 692
Receiving objects: 100% (692/692), 9.94 MiB | 1.76 MiB/s, done.
Resolving deltas: 100% (369/369), done.
Checking connectivity... done.

練習

  1. 在Github上建立一個Repository
  2. 把之前在本機上練習的檔案,推一份到這個新建的Repo裡
  3. 直接在Github上對某個檔案進行編輯並存檔
  4. 使用fetch或pull指令,把剛剛的更新抓下來

在Github 跟其他人一起工作

有時候同事更新比較快,會遇到推不上去的情形

狀況. A先push到master,B要push到master的時候,推上不去

1. 使用 git pull

git pull --rebase的使用時機

因為git pull包含使用git merge功能,會讓兩者合併時產生新節點。

如果再加上--rebase,會先把遠端的進度拉一份下來接在後面;
再接自己本地的進度接在後面;看起來像是無縫接軌。

2. 使用 git push -f

--force 推上前,先知會大家要以自己的版本為主。

3. 使用 PR (Pull Reqest)

  • 開自己的分支撰寫程式,避免跟別人的版本打架
  • 就不用每次push前先pull

方法:

  1. fork專案到自己的帳號下
  2. 寫好後提出pull request

專案擁有者merge pull request時,選擇 rebase and merge選項,不會造成合併的節點,可以讓歷史記錄比較乾淨

0701 實體課程 - Github

git push origin master的解釋

Git push origin master
把master的進度推一份到origin

Git push origin master
Git push abc master
Git push xyz master

Git push heroku master
Git remote add heroic git@heroku.com:tingtinghsu

Git push piano master (把貓推到鋼琴上)
git push origin master
=> 
git push origin master:master (遠端建立master branch的意思)

所以
git push origin master:cat (遠端建立cat branch的意思)

會用到git push origin master:cat的情境

git push heroku master
(heroku只能接受推master)
git push heroku dev
git push heroku dev:master
 (dev推上heroku遠端節點,再把遠端推成master branch的意思)
 git push origin master:cat
Total 0 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'cat' on GitHub by visiting:
remote:      https://github.com/tingtinghsu/gitfile/pull/new/cat
remote:
To github.com:tingtinghsu/gitfile.git
 * [new branch]      master -> cat

1. 在GitHub建立新專案

Github按左上角的(+)

參考https://hackmd.io/X82JI7apT5mr6Gp3q4OnFg#%E9%96%8B%E6%96%B0%E5%B0%88%E6%A1%88

2. 在本地電腦產生SSH Key

  ssh-keygen

Generating public/private rsa key pair.
Enter file in which to save the key (/Users/tingtinghsu/.ssh/id_rsa):

Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /Users/tingtinghsu/.ssh/id_rsa.
Your public key has been saved in /Users/tingtinghsu/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:Iq4xYSKyLdY27bThg4cZM+RZTPb+BhYj9TOT8jbLl2s tingtinghsu@Hui-TingdeMacBook-Air.local
The key's randomart image is:
+---[RSA 3072]----+
|                 |
|     o .         |
|    + o . .      |
|   . + = *       |
|+ = + + S +      |
|o=.O.. = +       |
|o.++O+. = o .    |
|...B=oo  = E     |
|  . .+. . o..    |
+----[SHA256]-----+


 ~  cd /Users/tingtinghsu/           
 ~  ls
Applications Documents    Dropbox      Movies       Pictures
Desktop      Downloads    Library      Music        Public
 ~  ~/.ssh                
 ~/.ssh  ls
id_rsa      id_rsa.pub  known_hosts
 ~/.ssh  code .        

 ~/.ssh  cat id_rsa.pub 

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDVr2bi2kTesH5q1qbs0kx2rlWjCV5poV87HI5d8J0WQXXEt5oRCEi3+TP1UklablEGTD2zRjIBLkzN7AIZadALzkFMdfyLT7Y8b1ll5kDc3lNyb6mCHfqjUe5XAWit2oVUS1zqaCbcBdRUvEmxR0vprO3ksaCPbHbYDkaK7lkq/rPgfGwMXVz1FHjj7vD4x7v1NcQnAyw0ZyGgV9/VeYs7eCILHdRXpAbstmFDyXcHYJwVgnRbEh0B4cCJ4/489sVsucZNgH/FghAOYAdX864I0OtNu44xBGyafauU2hOFgkxcRaio4Y09eUUD8IoEDmErShL/QT64ZJnxdG3sfswik3TvKuQWmbWn4CKN9Vxx0+1esg9dUgY7wjfDvhPgssEDmLp5ZLDy1PLhNf7uflSWmzwhrfa9XmNPW+X8ijpHwJSmfaKDvPaB78yNg9O3avjV6IMJW7FaV1tqqQyp9XLDpOnH180KMTNZjV68yUE+Jd11n6IJR0xy8J8WK04XkfE= tingtinghsu@Hui-TingdeMacBook-Air.local

3. 為GitHub設定SSH

  1. 確定該資料夾是否有設定遠端:
    git remote -v
fatal: not a git repository (or any of the parent directories): .git
(要選擇有.git檔案的資料夾才可推)
  1. 移除設定推送的遠端位置:
    git remote rm origin

  2. -u 設定預設的本地與推送的遠端位置配對
    (看你最近在忙什麼專案)
    git push -u origin master

註:如果沒有-u,每次git push會出現
The current branch master has no upstream branch.

git push origin master

The authenticity of host 'github.com (140.82.114.3)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com,140.82.114.3' (RSA) to the list of known hosts.
Enumerating objects: 9, done.
Counting objects: 100% (9/9), done.
Delta compression using up to 4 threads
Compressing objects: 100% (7/7), done.
Writing objects: 100% (9/9), 899 bytes | 299.00 KiB/s, done.
Total 9 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), done.
To github.com:tingtinghsu/gitfile.git
 * [new branch]      master -> master

git push

fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin master
 ~/Documents/projects/gitdemo   master  git remote add origin git@github.com:tingtinghsu/gitfile.git
 ~/Documents/projects/gitdemo   master  git remote -v
origin	git@github.com:tingtinghsu/gitfile.git (fetch)
origin	git@github.com:tingtinghsu/gitfile.git (push)


 ~/Documents/projects/gitdemo   master  git push origin master
The authenticity of host 'github.com (140.82.114.3)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com,140.82.114.3' (RSA) to the list of known hosts.

Enumerating objects: 9, done.
Counting objects: 100% (9/9), done.
Delta compression using up to 4 threads
Compressing objects: 100% (7/7), done.
Writing objects: 100% (9/9), 899 bytes | 299.00 KiB/s, done.
Total 9 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), done.
To github.com:tingtinghsu/gitfile.git
 * [new branch]      master -> master
Select a repo