引用網址http://zhwen.org/?p=articles/git
Git 介紹
最近的小項目想使用git來管理,應為git可以不需要服務器而在任意的Linux機器上管理代碼,相對svn和cvs還是有它的優勢的,所以我選用了git來管理我的小項目,以后在提供svn的管理。 在使用了一段時間后想寫一點總結,可能也是和網絡上其其它的git的文章差不多。但是作為我的使用總結還是很有必要的。git安lixnus的解釋是--The stupid content tracker, 傻瓜內容跟蹤器。呵呵!其實一點也不傻了,相當的智能化,也許應該這樣說是”content tracker for stupid guy”,呵呵!
git的管理是在本地建立存儲倉庫,代碼的所有變化的記錄都在本地存儲。也就是代碼和管理倉庫是形影不理的。不想svn分為客戶端和服務器端。客戶端只有 一些簡單的倉庫信息,而真正的代碼和代碼的變化信息全都在服務器上保存。客戶端一般只能得到代碼文件(只是一般情況,如果非要得到當然也還是可以的)。所 以git的這種方式可以減輕服務器的負擔--不用擔心服務器壞了或是連接不到怎么辦。
git的配置
所以首先我應當先說git的配置:Git命令的使用,一般有兩種兩種形式,一種是git后面帶參數(如:git add),另一種是直接減號連接的一條命令(如:git-add),后面講解全部使用后者,這樣可以避免空格的使用帶來的問題。
helight@helight:~/mywork/zhwen.org$ ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/home/helight/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/helight/.ssh/id_rsa. Your public key has been saved in /home/helight/.ssh/id_rsa.pub. The key fingerprint is: e0:4a:ba:d9:ba:b9:7a:0a:e4:aa:86:6c:a7:d8:85:c0 helight@helight The key's randomart image is: +--[ RSA 2048]----+ | | | | | . | |. . . | |.E . . S | |o. + . | |+.o o | |==.B | |X=@+. | +-----------------+ helight@helight:~/mywork/zhwen.org$ vim /home/helight/.ssh/ id_rsa id_rsa.pub known_hosts生成密鑰,用戶通信加解密。如果接受默認設置,那么生成2048bits RAS的密鑰,私鑰和公鑰文件分別位于:~/.ssh/id_rsa和~/.ssh/id_rsa.pub。用戶需要向服務器管理員提供公鑰 (id_rsa.pub),在用戶同步版本庫時對用戶進行身份認證。用戶必須妥善保管私鑰。
helight@helight:~/kernel-mod/hello$ git-config user.name Zhwen Xu helight@helight:~/kernel-mod/hello$ git-config user.email Helight.Xu@gmail.com配置用戶名,在生成補丁、日志時使用。git-config命令帶--global選項是對所有用戶信息進行配置,默認只針對對當前用戶。 git-config中寫入配置信息。 如果git-config加了--global選項,配置信息就會寫入到~/.gitconfig文件中。 因為你可能用不同的身份參與不同的項目,而多個項目都用git管理,所以建議不用global配置。
用戶可以通過git-config的其他選項來對git做其他配置,--list可以查看用戶已經選項。如:
helight@helight:~/kernel-mod/hello$ git-config --list user.name=Zhwen Xu user.email=Helight.Xu@gmail.com core.repositoryformatversion=0 core.filemode=true core.bare=false core.logallrefupdates=true helight@helight:~/kernel-mod/hello$
git的使用
命令:git-init-db 在我們要管理的目錄中執行這條命令即可建立相關的git數據庫文件。 我們首先以一個小例子來說,如下在hello這個文件夾中有兩個文件,一個c程序和一個Makefile文件。helight@helight:~/kernel-mod/hello$ ls -a . .. hello.c Makefile helight@helight:~/kernel-mod/hello$接著執行上面所說的命令:
helight@helight:~/kernel-mod/hello$ git-init-db Initialized empty Git repository in /home/helight/kernel-mod/hello/.git/ helight@helight:~/kernel-mod/hello$ ls -a . .. .git hello.c Makefile helight@helight:~/kernel-mod/hello$執行完上面的命令之后用“ls -a”來看,就會發現多了一個“.git”的文件,這個文件就是hello個小項目的git倉庫。關于這個倉庫有興趣的讀者可以進入該文件夾繼續分析。 接下來介紹如何將項目中的文件添加到倉庫中,讓git來作版本管理。將文件添加到git倉庫中的命令是git-add,當然這并不是真的將文件copy到git的倉庫文件夾中。只是在倉庫中標識它,以示要用它git來管理它了。具體操作如下:
helight@helight:~/kernel-mod/hello$ git-add hello.c helight@helight:~/kernel-mod/hello$ git-add * helight@helight:~/kernel-mod/hello$可以看到添加可以是單個文件添加,也可以是多個文件一起添加。接下來是提交和項目狀態的查看。相信有了前面的一點說明大家對git的管理也多少有點感覺了吧!
項目的提交和狀態查看:
命令:git-commit, git-statushelight@helight:~/kernel-mod/hello$ git-status # On branch master # # Initial commit # # Changes to be committed: # (use "git rm --cached ..." to unstage) # # new file: Makefile # new file: hello.c # helight@helight:~/kernel-mod/hello$ git-commit -m "init the project" ./* Created initial commit f4808f0: init the project 2 files changed, 27 insertions(+), 0 deletions(-) create mode 100644 Makefile create mode 100644 hello.c helight@helight:~/kernel-mod/hello$ git-status # On branch master nothing to commit (working directory clean) helight@helight:~/kernel-mod/hello$第一個“git-status”的提示信息告訴我們版本庫中加入了兩個新的文件(這是和上一個版本的變化),并且 git 提示我們提交這些文件,我們可以通過 git-commit 命令來提交。提交后再次使用就會提示沒有變化需要提交了。
分支管理:
git主要提倡的一種管理方式就是分支管理,所以這應該是每一個學習git的人應該掌握的。分支查看,分支建立和分支切換:
helight@helight:~/kernel-mod/hello$ git-branch * master helight@helight:~/kernel-mod/hello$ git-branch helight helight@helight:~/kernel-mod/hello$ git-branch helight * master helight@helight:~/kernel-mod/hello$ git-checkout helight Switched to branch "helight" helight@helight:~/kernel-mod/hello$ git-branch * helight master helight@helight:~/kernel-mod/hello$可以看出“git-branch”是查看分支情況和創建分支,”git-checkout”個用來切換分支。其中“*”表示當前工作的分支。
刪除分支:git-branch -D xxx
helight@helight:~/kernel-mod/hello$ git-branch xux helight@helight:~/kernel-mod/hello$ git-branch * helight master xux helight@helight:~/kernel-mod/hello$ git-branch -D xux Deleted branch xux. helight@helight:~/kernel-mod/hello$ git-branch * helight master helight@helight:~/kernel-mod/hello$
分支差異查看:git-show-branch,git-diff,git-whatchanged
我們對文件修改一下后在查看。helight@helight:~/kernel-mod/hello$ git-show-branch * [helight] init the project ! [master] init the project -- *+ [helight] init the project
git-diff:上次提交到現在的變化差異
helight@helight:~/kernel-mod/hello$ git-diff diff --git a/hello.c b/hello.c index 843a6b8..c762de7 100644 --- a/hello.c +++ b/hello.c @@ -14,6 +14,7 @@ static int __init hello_init(void) static void __exit hello_exit(void) { printk("kernel: %s\n","Bey world!"); + printk("kernel: %s\n","Bey world!"); } module_init(hello_init);
git-commit:提交
helight@helight:~/kernel-mod/hello$ git-commit -m "some change" ./* Created commit 2d900d9: some change 1 files changed, 1 insertions(+), 0 deletions(-)
git-whatchanged:查看本分支的修改情況
helight@helight:~/kernel-mod/hello$ git-whatchanged commit 2d900d918d24943b32f3d41b1974e0375be02c9e Author: Zhenwen Xu Date: Wed Nov 12 22:09:45 2008 +0800 some change :100644 100644 843a6b8... c762de7... M hello.c commit f4808f013f44e815831a3830a19925472be83424 Author: Zhenwen Xu Date: Wed Nov 12 21:53:52 2008 +0800 init the project :000000 100644 0000000... c151955... A Makefile :000000 100644 0000000... 843a6b8... A hello.c helight@helight:~/kernel-mod/hello$譬如我們要查看標號為 master和helight的版本的差異情況, 我們可以使用這樣的命令:
helight@helight:~/kernel-mod/hello$ git-diff helight master diff --git a/hello.c b/hello.c index c762de7..843a6b8 100644 --- a/hello.c +++ b/hello.c @@ -14,7 +14,6 @@ static int __init hello_init(void) static void __exit hello_exit(void) { printk("kernel: %s\n","Bey world!"); - printk("kernel: %s\n","Bey world!"); } module_init(hello_init); helight@helight:~/kernel-mod/hello$ 補丁制作: git-format-patch helight@helight:~/kernel-mod/hello$ vim hello.c helight@helight:~/kernel-mod/hello$ git-commit -m "change by helight" hello.c Created commit 4772773: change by helight 1 files changed, 1 insertions(+), 0 deletions(-)
制作用于郵件發送的補丁:
helight@helight:~/kernel-mod/hello$ git-format-patch -s master 0001-change-by-helight.patch helight@helight:~/kernel-mod/hello$ cat 0001-change-by-helight.patch From 4772773ecbbde66b8febc1d8aed0da67d480f1e4 Mon Sep 17 00:00:00 2001 From: Zhenwen Xu Date: Thu, 13 Nov 2008 10:30:10 +0800 Subject: [PATCH] change by helight Signed-off-by: Zhenwen Xu --- hello.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-) diff --git a/hello.c b/hello.c index 89795d2..2cbf9ee 100644 --- a/hello.c +++ b/hello.c @@ -6,6 +6,7 @@ MODULE_LICENSE("GPL"); static int __init hello_init(void) { printk("kernel: %s\n","Hello world!"); + printk("kernel: %s\n","This is Helight.Xu!"); return 0; } -- 1.5.6.5 helight@helight:~/kernel-mod/hello$
分支合并:git-merge
現在我們看看怎么將helight分支上的工作合并到master分支中。現在轉移我們當前的工作分支到 master,并且將helight分支上的工作合并進來。helight@helight:~/kernel-mod/hello$ git-checkout master Switched to branch "master" helight@helight:~/kernel-mod/hello$ git-merge "merge helight" HEAD helight Updating f4808f0..2d900d9 Fast forward hello.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-) helight@helight:~/kernel-mod/hello$但是更多的是將現在的工作pull到主分支上去,如下命令:
helight@helight:~/kernel-mod/hello$ vim hello.c helight@helight:~/kernel-mod/hello$ git-commit -m "another change" ./* Created commit 1d6b878: another change 1 files changed, 0 insertions(+), 3 deletions(-)git-pull:將工作更新到分支上
helight@helight:~/kernel-mod/hello$ git-checkout master Switched to branch "master" helight@helight:~/kernel-mod/hello$ git-pull . helight From . * branch helight -> FETCH_HEAD Updating 2d900d9..1d6b878 Fast forward hello.c | 3 --- 1 files changed, 0 insertions(+), 3 deletions(-)現在來看看如何退回到上一個版本:git-reset
命令形式:
git-reset [ --soft | --hard] []
命令的選項:
--soft
恢復到 git-commit命令之前,但是所作的修改是不會發生變化的。
--hard
將工作樹中的內容和頭索引都切換至指定的版本位置中,也就是說自上上一個git-commit命令之后的所有的跟蹤內容和工作樹中的內容都會全部丟失。 因此,這個選項要慎用,除非你已經非常確定你的確不想再看到那些東西了。
git信息查看和日志查看:
git-loggit-show
git-show-branch
git-show-index