Git basic

分散式版本控制系統
Git was created by Linus Torvalds in 2005 for development of the Linux kernel
Git is free software distributed under the terms of the GNU General Public License version 2

常見的online Git server
Github https://github.com/
Gitlab https://about.gitlab.com/


refer
http://blog.gogojimmy.net/2012/01/17/how-to-use-git-1-git-basic/
http://www.openfoundry.org/en/foss-programs/9318-git-github-firsttry
http://dylandy.github.io/Easy-Git-Tutorial/
http://mropengate.blogspot.tw/2015/04/git-github.html
http://gogojimmy.net/2012/01/17/how-to-use-git-1-git-basic/

…………………………………………………………………………………………………….

從git server複製project到local

在git server上看到很酷的project,想要下載它玩玩看時,就要用clone的方式複製到自己的電腦上

方法1 透過https
需要密碼,但不用放key在git server上
從git-lab project clone到local
git clone https://gitlab.com/< account >/< proejct name >.git

方法2 透過ssh
不需密碼,但要先放public key在git server上才可使用
從git-hub project clone到local
git clone -v git@github.com:< account >/< project name>.git
從git-lab project clone到local
git clone -v git@gitlab.com:< account >/< project name>.git


ex:
透過方法1https從git-lab將project載入到/root/git-repos
# mkdir /root/git-repos
# cd /root/git-repos
# git clone https://gitlab.com/raymond0820/afp2.git

Cloning into ‘afp2’…
Username for ‘https://gitlab.com’: raymond0820
Password for ‘https://raymond0820@gitlab.com’:
remote: Enumerating objects: 373, done.
remote: Counting objects: 100% (373/373), done.
remote: Compressing objects: 100% (314/314), done.
remote: Total 373 (delta 51), reused 373 (delta 51)
Receiving objects: 100% (373/373), 30.38 MiB | 2.57 MiB/s, done.
Resolving deltas: 100% (51/51), done.
Checking connectivity… done.

ex:
透過方法2ssh從git-hub將project載入到/root/git-repos
# mkdir /root/git-repos
# cd /root/git-repos
# git clone -v git@github.com:raymondtestproject/homework1.git

Initialized empty Git repository in /root/git-repos/homework1/.git/
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
# ls /root/git-repos


ps:
若project需要public key才可clone,會出現以下訊息( 加public key的方法  )
# git clone -v git@github.com:raymondtestproject/homework1.git
Cloning into ‘homework1’…
The authenticity of host ‘github.com (192.30.253.112)’ can’t be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘github.com,192.30.253.112’ (RSA) to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.

ps:
若己經public key放進git server,成功clone的結果如下
# git clone -v git@github.com:raymondtestproject/homework1.git
Cloning into ‘homework1’…
Enter passphrase for key ‘/root/.ssh/id_rsa’:
remote: Counting objects: 1475, done.
remote: Total 1475 (delta 0), reused 0 (delta 0), pack-reused 1475
Receiving objects: 100% (1475/1475), 80.38 MiB | 222.00 KiB/s, done.
Resolving deltas: 100% (876/876), done.

…………………………………………………………………………………………………..

從外部git server更新到local

git server上的project若有更新,需要update回local

#git pull
ex:
#git pull
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 5 (delta 3), reused 5 (delta 3), pack-reused 0
Unpacking objects: 100% (5/5), done.
From github.com:raymondtestproject/homework1
617bc6a..c9c9938 master -> origin/master
Updating 617bc6a..c9c9938
Fast-forward
command/index.php | 79 ++++++++++++++++++++++++++++++++++++++++++
integrity/__init__.pyc | Bin 119 -> 0 bytes
integrity/inc_integrity.pyc | Bin 17265 -> 0 bytes
integrity/lib_typemanage.pyc | Bin 3027 -> 0 bytes
lib_detection.pyc | Bin 5354 -> 0 bytes
5 files changed, 79 insertions(+), 0 deletions(-)
create mode 100644 command/index.php
delete mode 100644 integrity/__init__.pyc
delete mode 100644 integrity/inc_integrity.pyc
delete mode 100644 integrity/lib_typemanage.pyc
delete mode 100644 lib_detection.pyc

…………………………………………………………………………………………………..

提交檔案並從local更新到git server


如果原本的檔案有修改, 想更新回git server

#git commit -a  
#git push

ps:
git push時可能會跳出密碼輸入,此時需輸入ssh-key建立時輸入的passphrase
ex:
#git commit -a
writing something
#git push
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 262 bytes, done.
Total 3 (delta 1), reused 0 (delta 0)
To ssh://git@github.com/Raymond0820/viewnetflow.git
e359453..77156a7 master -> master

ps:
提交檔案另一個方法
#git commit -m “Add test.rb to test git function”


git push常見失敗原因
1.出現 ! [rejected] master -> master (non-fast forward) 
解決方式:
這代表你的parent commit和遠端的不同,也就是有分岔,需要先git pull回來,處理好merge 才能push上去。
2.出現No refs in common and none specified; doing nothing. Perhaps you should specify a branch such as ‘master’. fatal: The remote end hung up unexpectedly
解決方式:
執行git push –set-upstream origin master

…………………………………………………………………………………………………..

新增要追蹤的檔案更新到git server

若有新增檔案時, 預設這檔案有任何變動,都不會被git發現,因此要指令此檔案要被git追蹤, 追蹤後才可以透過git push更新到git server

要追蹤的檔案
#git add < track file>
ex:
增加readme檔
# cd /root/git-repos
#vim README.md
#git add README.md
ps:
如果一次新增很多檔案,可以輸入 git add . 這會將所有剛剛新增加的檔案一次Add 進stage 狀態

取消追蹤的檔案
#git rm –cached < filename>
從track list中刪除,不刪除filename
ex:
#git rm –cached lib_detection.py
rm ‘lib_detection.pyc’


ps:
從track list中刪除,並將filename刪除
#git rm -f < filename>
ex:
#git rm lib_detection.pyc
error: ‘lib_detection.pyc’ has local modifications
(use –cached to keep the file, or -f to force removal)


refer
http://oomusou.io/git/git-remove-stage/