27. 使用Git
git add本身只是告訴git檔案的存在,並不會馬上寫入版本庫。
我們再新增一個檔案,”git add .”後用”git status”查看結果
vagrant@debian-7:~/project$ echo "<h1>Why am I taking this class?</h1>" >
why.html
vagrant@debian-7:~/project$ git add .
vagrant@debian-7:~/project$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: index.html
# new file: why.html
#
vagrant@debian-7:~/project$
28. 使用Git
$ git commit -m "PUT YOUR COMMENT HERE"
這個命令會提交檔案變化到git的版本庫。
git add只是單純的追蹤檔案變化,git commit這個指令才會實際的把檔案寫入版本
庫。 -m 選項是必須的,在裡面你可以輸入這個版本更動的提示訊息。
vagrant@debian-7:~/project$ git commit -m "ready for gh-pages purposes"
[master (root-commit) 89ef72e] ready for gh-pages purposes
Committer: Vagrant User <vagrant@debian-7.2.0>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:
git config --global user.name "Your Name"
git config --global user.email you@example.com
After doing this, you may fix the identity used for this commit with:
git commit --amend --reset-author
2 files changed, 2 insertions(+)
create mode 100644 index.html
create mode 100644 why.html
vagrant@debian-7:~/project$
29. 使用Git
Committer: 提交者
提交者訊息至關重要,它可以表明這份代碼更動的負責人是誰,git會根據你登入
作業系統的使用者名稱以及主機名稱自動猜測,但如此並非總是準確。
透過以下命令,可以根據系統使用者,設定Committer訊息,這項行為在每台電腦
只需做一次:
$ git config --global user.name “Your Name”
$ git config --global user.email you@example.com
完成之後,使用下面訊息更動目前專案的Committer訊息:
$ git commit --amend --reset-author -m “Change committer info
for previous commits” (這行也是commit, 所以要加上comment)
Committer: Vagrant User <vagrant@debian-7.2.0>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:
git config --global user.name "Your Name"
git config --global user.email you@example.com
After doing this, you may fix the identity used for this commit with:
git commit --amend --reset-author
33. 使用Git
$ git reset --hard <COMMIT_SHA>
這個命令會還原到指定commit的狀態,且刪除commit之後的記錄。(更多的還原方法看這
裡)
vagrant@debian-7:~/project$ cat why.html
<h1>Why am I taking this class?</h1>
<h3>Does Git really works?</h3>
vagrant@debian-7:~/project$ git reset --hard
HEAD is now at 891940d ready for gh-pages
purposes
vagrant@debian-7:~/project$ cat why.html
<h1>Why am I taking this class?</h1>
vagrant@debian-7:~/project$
39. 使用GitHub
1. 產生一對金鑰(Pair of Key)
金鑰分成兩隻,公鑰(Public Key)用來加密,私鑰(Private Key)用來解密。
SSH會在~/.ssh這資料夾下儲存你的私鑰,讓你在登入時使用。
但一開始我們需要先打造這兩把鑰匙:
1. ~/.ssh不是一個git工作目錄,在裡面打git會GG噢~
2. 找不到~/.ssh怎麼辦?自己建一個吧:mkdir ~/.ssh (mkdir是啥)
vagrant@debian-7:~$ cd .ssh/
vagrant@debian-7:~/.ssh$ ls
authorized_keys
vagrant@debian-7:~/.ssh$ ssh-keygen -t rsa -C "wp2014nccu@gmail.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/vagrant/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/vagrant/.ssh/id_rsa.
Your public key has been saved in /home/vagrant/.ssh/id_rsa.pub.
The key fingerprint is:
xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx wp2014nccu@gmail.com
The key's randomart image is:
+--[ RSA 2048]----+
============================以下省略============================
40. 使用GitHub
注意:#開頭的句子不需要輸入(井字號也不用)
vagrant@debian-7:~$ cd .ssh/ #切換到.ssh資料夾
vagrant@debian-7:~/.ssh$ ls #列出資料夾中檔案
authorized_keys
vagrant@debian-7:~/.ssh$ ssh-keygen -t rsa -C "wp2014nccu@gmail.com" #產生一組金
鑰,參數C填入你在github註冊的電郵地址
Generating public/private rsa key pair.
Enter file in which to save the key (/home/vagrant/.ssh/id_rsa): #使用預設值
Enter passphrase (empty for no passphrase): #直接enter不用口令,要增強安全性看 這裡
Enter same passphrase again: #直接enter
Your identification has been saved in /home/vagrant/.ssh/id_rsa.
Your public key has been saved in /home/vagrant/.ssh/id_rsa.pub.
The key fingerprint is:
xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx wp2014nccu@gmail.com
The key's randomart image is:
+--[ RSA 2048]----+
#============================以下省略============================
44. 使用GitHub
4. 測試GitHub公鑰運作正常
vagrant@debian-7:~$ ssh -T git@github.com # 這裡不用換,保留git@github.com
The authenticity of host 'github.com (192.30.252.130)' can't be established.
RSA key fingerprint is xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,192.30.252.130' (RSA) to the list of
known hosts.
Hi wp2014nccu! You've successfully authenticated, but GitHub does not provide
shell access.
vagrant@debian-7:~$
52. 用Git把程式碼送上GitHub
我改變了index.html檔案內容,做了一次commit,在push上GitHub。
vagrant@debian-7:~/project$ echo "When I'm producing the ppt, I stay up late cause I
messed around at day." > index.html
vagrant@debian-7:~/project$ git add .
vagrant@debian-7:~/project$ git commit -m "How the ppt was made."
[master 82e0287] How the ppt was made.
1 file changed, 1 insertion(+), 1 deletion(-)
vagrant@debian-7:~/project$ git push
Warning: Permanently added the RSA host key for IP address '192.30.252.129' to the
list of known hosts.
Counting objects: 5, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 350 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:wp2014nccu/hello-github.git
891940d..82e0287 master -> master