大部分的Git操作都是在電腦完成
若要跟別人共同協作,有Git伺服器會比較方便
GitHub = 一款有Web介面的Git伺服器
GitLab
BitBucket
優點:
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
如果選擇https
,https://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
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
git remote add origin
+ 某個網址
origin
可以代換為別的名字git push origin master
git pull
或git fetch
git pull
vs git fetch
git fetch: 跟remote比對,將local沒有的檔案拉下來
git pull: git fetch
+ git merge
git fetch origin master
origin
這個遠端節點,抓上面master
分支的內容,並且在我的電腦上建立一個origin/master
分支tingdeAir:git-rebase tingtinghsu$ git merge origin/master
Updating 6cc2c94..ea6fdcd
Fast-forward
index.html | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
在origin/master上按右鍵 -> merge
->
Are you sure you want to merge origin/master
into your current branch?
-> 確定
master
的貼紙移動到origin/master
git pull origin master
origin
這個遠端節點,抓上面master
分支的內容,並且在我的電腦上建立一個origin/master
分支某個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.
有時候同事更新比較快,會遇到推不上去的情形
git pull
git pull --rebase
的使用時機
因為git pull
包含使用git merge
功能,會讓兩者合併時產生新節點。
如果再加上--rebase
,會先把遠端的進度拉一份下來接在後面;
再接自己本地的進度接在後面;看起來像是無縫接軌。
git push -f
--force
推上前,先知會大家要以自己的版本為主。
方法:
fork
專案到自己的帳號下pull request
專案擁有者merge pull request
時,選擇 rebase and merge
選項,不會造成合併的節點,可以讓歷史記錄比較乾淨
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
Github按左上角的(+)
參考https://hackmd.io/X82JI7apT5mr6Gp3q4OnFg#%E9%96%8B%E6%96%B0%E5%B0%88%E6%A1%88
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
git remote -v
fatal: not a git repository (or any of the parent directories): .git
(要選擇有.git檔案的資料夾才可推)
移除設定推送的遠端位置:
git remote rm origin
-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