git使用綜合

工作場景描述:
  一個遠程服務器CentOS(已安裝git:yum install git),本地電腦windows7(已安裝tortoisegit-1.8.6.0-1385537227873.msi和git-1.9.2-1401875809633.exe【即tortoisegit和git客戶端】)。第三方代碼託管方(git.oschina.net)。本地開發好後,提交第三方代碼託管方,然後在遠程服務器上更新代碼git pull。

SSH Keys

SSH key 可以讓你在你的電腦和 Git @ OSC 之間建立安全的加密連接。

你可以按如下命令來生成sshkey

ssh-keygen -t rsa -C "[email protected]"# Creates a new ssh key using the provided email
# Generating public/private rsa key pair...

此處注意兩點:

     (1)然後會讓輸入id_rsa的文件位置,當時我輸入了一個自定義的位置,結果總是提示無權限訪問,後來我直接定義到當前用戶的主目錄下的.ssh/文件夾下,就可以了。

  (2)是把id_rsa.pub裏的所有內容,包含頭部ssh-rsa和尾部郵箱都複製過去。

查看你的public key,並把他添加到 Git @ OSC http://git.oschina.net/keys

cat ~/.ssh/id_rsa.pub
# ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC6eNtGpNGwstc....

添加後,在終端(Terminal)中輸入

若返回

Welcome to Git@OSC, yourname!

則證明添加成功。

 

常用命令:

Linus爲Linux Kernel Project發起的版本控制項目。
HEAD代表當前最新狀態。
tag爲某個狀態的標籤。
SHA1爲每個提交日誌的唯一標識。

install:

apt-get install git-core


git clone:

git倉庫可以使用git clone獲得:
git clone git://git.longene.org/unifiedkernel.git

也可以通過瀏覽器瀏覽。
http://www.longene.org/gitweb/

通過git pull更新倉庫,使用git init-db初始化自己的倉庫。


config:

開發人員需要爲git倉庫配置相關信息,這樣在提交代碼時,這些信息會自動
反映在git倉庫的日誌中。

git config user.name "your name"
git config user.email [email protected]
git config core.editor vim
git config core.paper "less -N"
git config color.diff true
git config alias.co checkout

git config alias表示,可以用git co代表git checkout。git var -l可以查看
已經設置的配置。


diff:

開發人員在本地進行開發後,可以使用git diff查看改動。
除了直接比較當前開發後的改動外,git diff還可以:

git diff tag 比較tag和HEAD之間的不同。
git diff tag file 比較一個文件在兩者之間的不同。
git diff tag1..tag2 比較兩個tag之間的不同。
git diff SHA11..SHA12 比較兩個提交之間的不同。
git diff tag1 tag2 file or
git diff tag1:file tag2:file 比較一個文件在兩個tag之間的不同。


ORIG_HEAD用於指向前一個操作狀態,因此在git pull之後如果想得到pull的
內容就可以:

git diff ORIG_HEAD

git diff --stat 用於生成統計信息。
git diff --stat ORIG_HEAD


apply:

git apply相當於patch命令。
--check 檢查能否正常打上補丁,-v verbose模式, -R reverse模式,反打補丁。


log:

git log file 查看一個文件的改動。
git log -p 查看日誌和改動。
git log tag1..tag2 查看兩個tag之間的日誌。
git log -p tag1..tag2 file 查看一個文件在兩個tag之間的不同。
git log tag.. 查看tag和HEAD之間的不同。


commit:

git commit -a -e 提交全部修改文件,並調用vim編輯提交日誌。
git reset HEAD^ or
git reset HEAD~1 撤銷最後一次提交。
git reset --hard HEAD^ 撤銷最後一次提交併清除本地修改。
git reset SHA1 回到SHA1對應的提交狀態。


add/delete/ls:

git add -a 添加所有文件。
git rm file 從git倉庫中刪除文件。
git commit 添加或是刪除後要提交。

git ls-files -m 顯示修改過的文件。
git ls-files 顯示所有倉庫中的文件。

git中有四種對象:blob、tree、commit、tag。
blob代表文件,tree代表目錄,commit代表提交歷史,tag代表標籤。
這四種對象都是由SHA1值表示的。在倉庫的.git目錄中保存了git管理倉庫
所需要的全部信息。

git ls-tree HEAD file 顯示file在HEAD中的SHA1值。
git cat-file -t SHA1 顯示一個SHA1的類型。
git cat-file type SHA1 顯示一個SHA1的內容。type是blob、tree、commit、tag之一。

patch:

git format-patch -1 生成最後一個提交對應的patch文件。
git am < patch 把一個patch文件加入git倉庫中。
git am --resolved 如果有衝突,在解決衝突後執行。
git am --skip 放棄當前git am所引入的patch。


conflict:

git merge 用於合併兩個分支。
git diff 如果有衝突,直接使用diff查看,
衝突代碼用<<<和>>>表示。手動修改衝突代碼。
git update-index 更新修改後的文件狀態。
git commit -a -e 提交爲解決衝突而修改的代碼。


branch:

git branch -a 查看所有分支。
git branch new_branch 創建新的分支。
git branch -d branch 刪除分支。
git checkout branch 切換當前分支。-f參數可以覆蓋未提交內容。


daemon:

有時更新公共代碼倉庫使用patch的方式,或者直接
用git pull git://ip/repo branch的方式更新每個人的代碼。使用git pull的方式需要
提交代碼的機器運行:
git daemon --verbose --export-all --enable=receive-pack --base-path=/repo


request-pull:


git request-pull start url 用於產生本次pull請求的統計信息。


clean:


git clean -dxf 用於清除未跟蹤文件。
git clean -dnf 可以顯示需要刪除的文件,但不包括被.gitignore忽略的。
git reset --hard HEAD 用於清除跟蹤文件的修改。

安裝:

git,分佈式版本控制系統,作者:Linus Benedict Torvalds。

  1.下載和安裝git
  下載地址:http://git-scm.com/download

  或者wget獲取:wget http://kernel.org/pub/software/scm/git/git-1.7.6.tar.bz2

  解壓後切換到其目錄:
  #tar zxvf 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/bin/git /usr/share/man/man1/git.1.gz
  #git --version
  git version 1.7.6
  $git --help

  首先需要指定用戶名和電子郵箱地址:
  # git config --global user.name "sasa"
  # git config --global user.email [email protected]
  再驗證一下配置信息:
  # git config --list
  user.name=sasa
  [email protected]
  core.repositoryformatversion=0
  core.filemode=true
  core.bare=false
  core.logallrefupdates=true
   這些配置是存放在個人注目禮下的.gitconfig文件中的,可以查看#vi ~/.gitconfig

[user]
        name = sasa
        email = sasa@manyi.cc

 

 

  3.建立工程。
  本地存儲的任何一個目錄都可以建立git工程,如果已有工程位於/home/sasa/gitwork目錄,就可以把這個目錄定義爲git工程。
  # cd /home/sasa/gitwork
  # git init    (注意:要切換到gitwork目錄下,才能對該目錄進行提交,初始化的也是針對該目錄)
  Reinitialized existing Git repository in /home/sasa/gitwork/.git/
  這樣就建立了一個名爲.git的文件夾,這就是git用來存儲信息和跟蹤改動的文件夾。
  查看該文件夾下所有的文件:
  ## ls -altr .git 

複製代碼
總用量 52
drwxr-xr-x. 4 root root 4096  3月  7 11:28 refs
drwxr-xr-x. 2 root root 4096  3月  7 11:28 branches
-rw-r--r--. 1 root root   73  3月  7 11:28 description
drwxr-xr-x. 2 root root 4096  3月  7 11:28 info
drwxr-xr-x. 2 root root 4096  3月  7 11:28 hooks
-rw-r--r--. 1 root root   23  3月  7 11:28 HEAD
-rw-r--r--. 1 root root  112  3月  7 11:48 index
-rw-r--r--. 1 root root  303  3月  7 11:48 COMMIT_EDITMSG
drwxr-xr-x. 7 root root 4096  3月  7 11:49 objects
drwxr-xr-x. 3 root root 4096  3月  7 11:49 logs
drwxr-xr-x. 3 root root 4096  3月  7 11:51 ..
-rw-r--r--. 1 root root   92  3月  7 12:41 config
drwxr-xr-x. 8 root root 4096  3月  7 12:41 .
複製代碼

 

 

  4.向工程添加和提交文件。
  (目前gitwork目錄下僅有文件msg.php)
  # git add msg.php
  # git commit

[master 306e67d] This is the first update.
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 msg.php

 

 

  5.更改文件和提交改動
  編輯文件、添加或者刪除了一些字段:
  #vi msg.php
  查看和git倉庫中的文件相比有了哪些改動:
  #git diff

複製代碼
diff --git a/mynote.php b/mynote.php
deleted file mode 100644
index 6edf9e3..0000000
--- a/mynote.php
+++ /dev/null
@@ -1,2 +0,0 @@
-
-iii   hello ,this is my note work!
複製代碼

 

  注:如果要提交,需要先確保文件添加到了臨時區域(staging area),然後才能提交,提交時會自動打開系統的默認編輯器,用戶添加一些註釋後保存並退出編輯器的時候,這些註釋就同時提交到倉庫中去了。

  # git add msg.php
  # git commit

[master 9090cb1] This is the second change. ---->這裏就是備註
 1 files changed, 1 insertions(+), 1 deletions(-)

 

 

  查看狀態:
  #git status

複製代碼
# On branch master
# Changed but not updated:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       deleted:    mynote.php
#
no changes added to commit (use "git add" and/or "git commit -a")
複製代碼

 

  查看文件歷史和以往的註釋:
  # git log msg.php

複製代碼
commit 347aee4f50a3e82a3e664fd5f6726b35d678788b
Author: sasa <sasa@manyi.cc>
Date:   Thu Mar 7 14:39:19 2013 +0800

    This is the third change.

commit 9090cb164605d5945cadb4a0235cb25322277e96
Author: sasa <sasa@manyi.cc>
Date:   Thu Mar 7 14:36:21 2013 +0800

    This is the second change.

commit e47644aaaf4d4159c16743668dbc53e790962a6f
Author: sasa <sasa@manyi.cc>
Date:   Thu Mar 7 14:34:59 2013 +0800

    This is the first change.

commit 306e67dab039edc364e595f9089e67a8a4519dfd
Author: sasa <sasa@manyi.cc>
Date:   Thu Mar 7 12:47:59 2013 +0800

    This is the first update.
複製代碼

 

 

Git在windows下使用方法:
  Git是一個免費的、開源的版本控制軟件。在Windows上安裝git,一般爲msysgit,官方下載地址爲
http://code.google.com/p/msysgit/downloads/list

(1)下載:在官方網站下載最新版本。

2 安裝:點擊下一步,直到出現步驟三。

3 在Windows Explorer integration選項中將“Git Bash here”和“Git GUI here”打對勾。

4 在“Adjusting your PATH environment”選項中,默認即可。

5 在“Configuring the line ending conversions”選項中, 第一個選項:如果是跨平臺項目,在windows系統安裝,選擇; 第二個選項:如果是跨平臺項目,在Unix系統安裝,選擇; 第三個選項:非跨平臺項目,選擇。

6 選擇“Finish”,結束安裝。

7 在桌面上,可以看到安裝後的“Git Bash”選項,雙擊打開,可以進行操作了。

生成ssh公鑰
(1)啓動Git Bash:
(2)在程序中依次輸入:配置email,命令如下:

$git config --global user.name "your name"   
$git config --global user.email 

"[email protected]"其中"your name"換成你的名字,"[email protected]"換成你的郵箱地址。
(3)生成公鑰,用於身份認證:

ssh-keygen.exe -C "[email protected]" -t rsa

這樣就可以在你的用戶下取得需要的公鑰匙,公鑰位於你的電腦中“C:\Documents and Settings\Airen\.ssh”的“id_rsa.pub”文件下;
此時只要把你的公鑰放進你項目的服務中就可以進行git的相關操作。

解決中文目錄支持問題:
1、ls不能顯示中文目錄解決辦法:
      在git/etc/git-completion.bash中增加一行:alias ls='ls --show-control-chars --color=auto'
2、git commit不能提交中文註釋解決辦法:
       修改git/etc/inputrc中對應的行:set output-meta on  set convert-meta off
3、git log無法顯示中文註釋解決辦法:
      在git/etc/profile中增加一行:export LESSCHARSET=iso8859.

 

在外網服務器上搭建GIT服務器:

克隆:
$ git [email protected]:/home/sasa/git1   (內網用戶)
$ git clone  ssh:/[email protected]:9092/home/sasa/git1   (外網用戶)

問題:clone、pull都沒問題,但是push的時候出問題,錯誤提示:remote: error: To squelch this message and still keep the default behaviour....

解決辦法:

這是由於git默認拒絕了push操作,需要進行設置,修改.git/config文件後面添加如下代碼:

[receive]

 denyCurrentBranch = ignore

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章