GIT在Linux上的安裝和使用簡介
GIT最初是由Linus Benedict Torvalds為了更有效地管理Linux核心開發而創立的分布式版本控制軟體,與常用的版本控制工具如CVS、Subversion不同,它不必伺服器端軟體支援,速度和效率也有着相當程度的提高。
www.2cto.com
如果擁有CVS或者SVN的使用背景,那麼更熟悉的方法是用戶端-伺服器端模式,所有的檔案倉庫(repository)都是存放在伺服器上的,使用者需要在本地安裝用戶端去伺服器上的項目中擷取舊版本,送出新版本。
GIT抛棄了這種模式,當使用者從遠端GIT倉庫下載下傳一個工程(project)時,這個工程的所有檔案,包括版本曆史,檔案改動都會下載下傳下來,這時 候本地GIT就演變成了一個伺服器,所有的送出(check-in)、提出(check-out)都會在這個本地伺服器上執行,當你确定一項修改之後,可 以再和遠端倉庫進行合并和同步(merge)。是以,GIT的安裝和配置步驟無論在本機還是伺服器上都是完全一樣的。
這裡簡單地介紹GIT在Linux上的安裝和使用,算做一個新手入門的簡單教程。另外,GIT是有Windows上的用戶端的。
1、下載下傳和安裝GIT
從這裡 http://git-scm.com/download 下載下傳GIT或者使用wget指令擷取
$ cd
$ wget http://kernel.org/pub/software/scm/git/git-1.7.6.tar.bz2
以上位址若是無法下載下傳到的話
解壓後切換到其目錄
$ tar xvfj git-1.7.6.tar.bz2
$ cd git-1.7.6
使用預設配置進行安裝,如果想修改配置,可以使用 ./configure --help 來擷取幫助
$ ./configure
$ make
$ make install
2、初始化配置
GIT預設安裝在 /usr/local/bin ,安裝之後可以驗證一下是否安裝好
$ whereis git
git: /usr/local/bin/git
$ git --version
git version 1.7.6
$ git --help
首先需要指定使用者名和電子郵件位址
$ git config --global user.name “GIT Admin”
$ git config --global user.emal [email protected]
再驗證一下配置資訊
$ git config --list
user.name=GIT Admin
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
其實這些配置是存放在個人主目錄下的 .gitconfig 檔案中的
$ cat ~/.gitconfig
[user]
name = GIT Admin
email = [email protected]
3、建立工程
本地存儲的任何一個目錄都可以建立GIT工程,如果已有工程位于 /home/obugs/projects/orangebugs 目錄,就可以把這目錄定義為GIT工程
$ cd /home/obugs/projects/orangebugs
$ git init
Initialized empty Git repository in /home/obugs/projects/orangebugs/.git/
這樣就建立了一個名為 .git 的檔案夾,這就是GIT用來存儲資訊和跟蹤改動的檔案夾。
$ ls -altr .git
total 40
drwxrwxr-x 4 git git 4096 Aug 13 22:39 refs
drwxrwxr-x 4 git git 4096 Aug 13 22:39 objects
drwxrwxr-x 2 git git 4096 Aug 13 22:39 info
drwxrwxr-x 2 git git 4096 Aug 13 22:39 hooks
-rw-rw-r -- 1 git git 23 Aug 13 22:39 HEAD
-rw-rw-r -- 1 git git 73 Aug 13 22:39 description
-rw-rw-r -- 1 git git 92 Aug 13 22:39 config
drwxrwxr-x 2 git git 4096 Aug 13 22:39 branches
drwxrwxr-x 36 git git 4096 Aug 13 22:39 ..
drwxrwxr-x 7 git git 4096 Aug 13 22:39 .
4、向工程添加和送出檔案
這些動作和CVS、SVN等操作類似
$ git add *.java *.c
$ git commit -m ‘Initial upload of the project’
create mode 100755 Orangebugs.java
create mode 100755 pwm/ui/DataManager.java
create mode 100755 pwm/ui/PasswordFrame.java
create mode 100755 pwm/tools/StrongEncryption.java
create mode 100755 pwm/tools/PasswordStrength.java
..
注意如果之前沒有使用 git config 指定使用者名和電子郵件位址,這裡會報錯
$ git commit -m ‘Initial upload of the project'
*** Please tell me who you are.
Run
git config --global user.email “[email protected]”
git config --global user.name “Your Name”
to set your account’s default identity.
Omit --global to set the identity only in this repository.
fatal: empty ident not allowed
5、更改檔案和送出改動
編輯檔案、添加或者删除了一些字段
$ vi Orangebugs.java
檢視和GIT倉庫中的檔案相比有了那些改動
$ git diff
diff --git a/Orangebugs.java b/Orangebugs.java
index 6166ed1..fd82d32 100644
— a/Orangebugs.java
+++ b/Orangebugs.java
@@ -2,7 +2,7 @@
- public counter=10
+ public counter=55
如果要送出,需要先確定将檔案添加到了臨時區域(staging area)然後才能送出,送出時會自動打開系統的預設編輯器,使用者添加一些注釋後儲存并退出編輯器的時候,這些注釋就同時送出到倉庫中去了
$ git add Orangebugs.java
$ git commit
[master 80f10a9] Added password strength meter functionality
1 files changed, 56 insertions(+), 7 deletions(-)
或者,簡單一點的方法是使用 git commit -a 把上面兩個指令合二為一。
6、檢視狀态和檢視注釋
如果本地的檔案和遠端GIT倉庫上的檔案相比沒有任何改動,則
$ git status
# On branch master
nothing to commit (working directory clean)
如果本地做了改動但是沒有送出,則
# Changes not staged for commit:
# (use “git add …” to update what will be committed)
# (use “git checkout — …” to discard changes in working directory)
#
# modified: Orangebugs.java
no changes added to commit (use "git add" and/or "git commit -a")
另外,可以用下面的指令檢視檔案曆史和以往的注釋
$ git log Orangebugs.java
commit c919ced7f42f4bc06d563c1a1eaa107f2b2420d5
Author: GIT Admin www.2cto.com
Date: Sat Aug 13 22:54:57 2011 -0700
Added password strength meter functionality
commit c141b7bdbff429de35e36bafb2e43edc655e9957
Author: GIT Admin
Date: Sat Aug 13 20:08:02 2011 -0700
Initial upload of the project
6、各種問題
(1)出現’gitosis-admin’ does not appear to be a git repository,短路徑無效時替換為全路徑
在伺服器上使用短路徑會取現下面這個問題,原因是沒有找到對應比對的密鑰所緻,如果正确使用了ssh密鑰則不會出現這個問題,這裡就不去折騰了
1 2 3 4 5 | |
使用以下指令解決,隻不過每次push和pull的時候都需要輸入一遍git使用者的密碼,略為繁瑣
|
(2)出現Unable to create temporary file: Permission denied
在Windows上使用TortoiseGit執行Push時出現以下錯誤
06 07 08 09 10 | |
原來是伺服器上是用root賬戶建立的庫目錄,導緻git賬戶無權寫入,方法就是修改檔案夾的所屬使用者和所屬使用者組
|
(3)出現failed to push some refs to ‘[email protected]:channelv.git’
11 12 | |
在伺服器對應的庫目錄下執行以下指令增加配置即可
|
學習時的痛苦是暫時的 未學到的痛苦是終生的