Git是一款功能强大的版本管理工具,在开发过程中可以方便的实现团队项目的版本管理和代码管理。可在win,linux,mac等系统上使用。
Git的各项操作一般是由命令完成的,这与win上的软件有一些区别。
在学习使用Git时,主要是理解版本管理,文件提交与同步等操作,理解多人工作时版本代码同步的原理。
这些命令分四种类型:
①不需要和其他开发者协作的独立开发者,会经常用到 git init、git show branch、git commit 等命令;
②需要和其他人协作的开发者,会常用到 git clone、git push、git pull、git format patch 。
③在项目中负责接收其他开发者发来更新的核心开发者,会常用到 git am、git pull、git format patch、git revert、git push;
④ 代码仓库管理员常用 git daemon、git shell……
对于任何想做提交的人来说,甚至对于某位单独工作的人来说,【个人开发者(单独开发)】部分命令都是必不可少的。如果你和别人一起工作,你也会需要【个人开发者(参与者)】部分列出的命令。
除了上述的部分,担当【集成人员】角色的人需要知道更多命令。【代码库管理】命令帮助系统管理员负责管理,及向git代码库提交内容。
单独的个人开发者不会与他人交换修补程序,只用到下列命令,独自在单独的代码库上工作:
1
2
3
4
5
6
|
$ tar zxf frotz.tar.gz
$ cd frotz
$ git init
$ git add . <1>
$ git commit -m "import of frotz source tree."
$ git tag v2.43 <2>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
$ git checkout -b alsa-audio <1>
$ edit/compile/test
$ git checkout -- curses/ux_audio_oss.c <2>
$ git add curses/ux_audio_alsa.c <3>
$ edit/compile/test
$ git diff HEAD <4>
$ git commit -a -s <5>
$ edit/compile/test
$ git reset --soft HEAD^ <6>
$ edit/compile/test
$ git diff ORIG_HEAD <7>
$ git commit -a -c ORIG_HEAD <8>
$ git checkout master <9>
$ git merge alsa-audio <10>
$ git log --since='3 days ago' <11>
$ git log v2.43.. curses/ <12>
|
作为在一个团体项目里参与角色的开发人员,需要学习如何与他人沟通,除了那些单独开发者需要掌握的命令以外,还要使用这些命令。
1
|
$ git clone git://git.kernel.org/pub/scm/.../torvalds/linux-2.6 my2.6
|
1
2
3
4
5
6
7
8
9
|
$ cd my2.6
$ edit/compile/test; git commit -a -s <1>
$ git format-patch origin <2>
$ git pull <3>
$ git log -p ORIG_HEAD.. arch/i386 include/asm-i386 <4>
$ git pull git://git.kernel.org/pub/.../jgarzik/libata-dev.git ALL <5>
$ git reset --hard ORIG_HEAD <6>
$ git gc <7>
$ git fetch --tags <8>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
satellite$ git clone mothership:frotz frotz <1>
satellite$ cd frotz
satellite$ git config --get-regexp '^(remote|branch)\.' <2>
remote.origin.url mothership:frotz
remote.origin.fetch refs/heads/*:refs/remotes/origin/*
branch.master.remote origin
branch.master.merge refs/heads/master
satellite$ git config remote.origin.push \
master:refs/remotes/satellite/master <3>
satellite$ edit/compile/test/commit
satellite$ git push origin <4>
mothership$ cd frotz
mothership$ git checkout master
mothership$ git merge satellite/master <5>
|
1
2
3
4
5
|
$ git checkout -b private2.6.14 v2.6.14 <1>
$ edit/compile/test; git commit -a
$ git checkout master
$ git format-patch -k -m --stdout v2.6.14..private2.6.14 |
git am -3 -k <2>
|
在一个团队项目中担任集成者的是一名相当重要的人员,他接受别人的修改,评审并且集成并且发布结果,供他人使用;除了那些参与者需要的命令之外,还会使用这些命令。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
$ git status <1>
$ git show-branch <2>
$ mailx <3>
s 2 3 4 5 ./+to-apply
s 7 8 ./+hold-linus
q
$ git checkout -b topic/one master
$ git am -3 -i -s -u ./+to-apply <4>
$ compile/test
$ git checkout -b hold/linus && git am -3 -i -s -u ./+hold-linus <5>
$ git checkout topic/one && git rebase master <6>
$ git checkout pu && git reset --hard next <7>
$ git merge topic/one topic/two && git merge hold/linus <8>
$ git checkout maint
$ git cherry-pick master~4 <9>
$ compile/test
$ git tag -s -m "GIT 0.99.9x" v0.99.9x <10>
$ git fetch ko && git show-branch master maint 'tags/ko-*' <11>
$ git push ko <12>
$ git push ko v0.99.9x <13>
|
1
2
3
4
5
6
7
8
9
|
$ cat .git/remotes/ko
URL: kernel.org:/pub/scm/git/git.git
Pull: master:refs/tags/ko-master
Pull: next:refs/tags/ko-next
Pull: maint:refs/tags/ko-maint
Push: master
Push: next
Push: +pu
Push: maint
|
在从git show-branch的输出里,主分支应该有所有ko-master有的,并且next应该有ko-next有的所有内容。
代码库管理员使用下列工具来设置及维护开发者对代码库的访问。
update hook howto有一个很好的管理共享中央代码库的实例。
1
2
|
$ grep 9418 /etc/services
git 9418/tcp # Git Version Control System
|
从inetd运行git-daemon来服务于/pub/scm
$ grep git /etc/inetd.conf git stream tcp nowait nobody \ /usr/bin/git-daemon git-daemon --inetd --export-all /pub/scm
实际的配置应该在1行里。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
$ cat /etc/xinetd.d/git-daemon
# default: off
# description: The git server offers access to git repositories
service git
{
disable = no
type = UNLISTED
port = 9418
socket_type = stream
wait = no
user = nobody
server = /usr/bin/git-daemon
server_args = --inetd --export-all --base-path=/pub/scm
log_on_failure += USERID
}
|
检查xinetd(8)文档并设置,这个文档来自于Fedora系统。其他也许会不一样。
授予开发者只推/拉访问操作权限。
$ grep git /etc/passwd <1> alice:x:1000:1000::/home/alice:/usr/bin/git-shell bob:x:1001:1001::/home/bob:/usr/bin/git-shell cindy:x:1002:1002::/home/cindy:/usr/bin/git-shell david:x:1003:1003::/home/david:/usr/bin/git-shell $ grep git /etc/shells <2> /usr/bin/git-shell
CVS风格的共享代码库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
$ grep git /etc/group <1>
git:x:9418:alice,bob,cindy,david
$ cd /home/devo.git
$ ls -l <2>
lrwxrwxrwx 1 david git 17 Dec 4 22:40 HEAD -> refs/heads/master
drwxrwsr-x 2 david git 4096 Dec 4 22:40 branches
-rw-rw-r-- 1 david git 84 Dec 4 22:40 config
-rw-rw-r-- 1 david git 58 Dec 4 22:40 description
drwxrwsr-x 2 david git 4096 Dec 4 22:40 hooks
-rw-rw-r-- 1 david git 37504 Dec 4 22:40 index
drwxrwsr-x 2 david git 4096 Dec 4 22:40 info
drwxrwsr-x 4 david git 4096 Dec 4 22:40 objects
drwxrwsr-x 4 david git 4096 Nov 7 14:58 refs
drwxrwsr-x 2 david git 4096 Dec 4 22:40 remotes
$ ls -l hooks/update <3>
-r-xr-xr-x 1 david git 3536 Dec 4 22:40 update
$ cat info/allowed-users <4>
refs/heads/master alice\|cindy
refs/heads/doc-update bob
refs/tags/v[0-9]* david
|
支持dumb协议传送的HTTP服务器
1
2
3
|
dev$ git update-server-info <1>
dev$ ftp user@isp.example.com <2>
ftp> cp -r .git /home/user/myproject.git
|
学习了这些内容,相信你就是Git高手了。相信Git将给你日常工作带来很多便利。