git 参考文档:
progit.org(中文)
git reference
git manual
http://toolmantim.com/articles/setting_up_a_new_remote_git_repository
常用的 git 命令:
* 如何创建 git
repository
* 服务器端
# cd /export/git
# mkdir
git-sample.git
# cd sample.git
# git --bare init
# git update-server-info
* 用户端
# mkdir git-sample
# cd git-sample
# copy your file to this direcotry
#
git init
# echo "*.o" >> .gitignore >>>> 编辑
.gitignore 文件
# git add .
# git commit -m "init"
# git push SERVER_ADDRESS:/export/git/git-sample.git master
*
clone源代码:
# git clone
git+ssh://SERVER_ADDRESS/export/git/git-sample.git
* commit 修改
# git status
# git add file1 file2 ...
# git commit
#
git push
* 更新源代码
# git pull
关于 git branch
* git branch
-a 列举所有branch
* git branch -r 列举remote branch
* git branch
-l 列举local branch
remote branch 保存在 .git/refs/remote/,
而local branch保存在 .git/refs/heads/ .
* 使用不同的 branch 来同时做不同的工作:
例: branch master -> 工作1
branch work_a -> 工作2
# git checkout -b work_b
do you work_b here and then commit to
server.
# git push
# git checkout master
do
you work_a here and then commit to server.
# git push
如果都完成了,需要合并的话:
# git checkout master
# git merge work_b
# git push
# git branch -d work_b -> 删除branch
* 从remote
repository里更新源代码
假设进行内核开发,当前工作是基于 2.6.29 版本的内核开发的。现在
v2.6.30 的内核出来了,希望将版本升级为最新的 v2.6.30.
(Mainline kernel)
v2.6.29
: v2.6.30-rc1 v2.6.30
: : :
o-----o-----o---...........---o
master
(My kernel tree)
v2.6.29
: v2.6.30-rc1 v2.6.30
: : :
o-----o-----o---...........---o master (mainline)
| |
| \ (git pull)
| o update (my tree)
\
v2.6.29-zeng |
o-----o-----o-----o |
master (my tree)
M1 M2 M3 |
\ (git merge update)
o-----o-----o-----o master (my tree)
M1 M2 M3
可以用下面的步骤:
# git checkout -b update
v2.6.29 -> current branch is update
# git pull
git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git
# git checkout master
# git merge update
# git push
# git branch -d update -> delete this branch
* 在局域网中建立 git
repository 镜像
在局域网中,如果大家都需要访问(pull only) 外部git repository, 比如:
git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git, 这时
候可以在局域网中建立一个镜像 git repository, 定期从外部更新。
# mkdir
linux-2.6.git
# git --bare init
# git update-server-info
# mkdir linux-2.6
# git init
# git remote add -f -t master
-m master origin
git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git
# git merge origin
# git push
git+ssh://114.180.90.213/export/git/linux-2.6.git master
如果
merge(合并)过程中出现冲突(conflict), 需要恢复到合并之前的状态,使用:
# git reset --hard
HEAD