1. 安装
# apt-get install git git-core
2. 客户端使用
2.1. 导出版本
2.1.1. 获取一个版本
1.5版本
# git-clone ssh://172.16.10.210/usr/local/src/share/chui/android/git/mydroid_1.5
1.0版本
# git-clone ssh://172.16.10.210/usr/local/src/share/chui/android/git/cupcake
或
# git-clone root@172.16.10.210:/usr/local/src/share/chui/git/gittest
--origin
-o
Instead of using the remote name origin to keep track of the upstream repository, use .
2.1.2. 更新版本库
# git-fetch[微软用户1]
git-pull
2.2. 添加文件
# git-add filename
…
Files to add content from. Fileglobs (e.g. *.c) can be given to add all matching files. Also a leading directory name (e.g. dir to add dir/file1 and dir/file2) can be given to add all files in the directory, recursively.
2.3. 提交文件[微软用户2]
2.3.1. 远处提交
// 把本地仓库提交到远程仓库的master分支中
# git push ssh://172.16.10.210/usr/local/src/share/chui/git/gittest
2.3.2. 本地提交
通过 git-commit 命令来提交:
-a (or --all )
Tell the command to automatically stage files that have been modified and deleted, but new files you have not told git about are not affected[微软用户3] .
# git-commit -a -m[微软用户4] "Initial commit of gittutor reposistory"
2.4. 查看版本差异
既然我们刷新了 Git 的跟踪信息,现在我们看看版本库的状态:
Show the working tree status
# git-status[微软用户5]
git-diff [微软用户6] 命令将比较当前的工作目录和版本库数据库中的差异。现在我们编辑一些文件来体验一下 git 的跟踪功能。
Show changes between commits, commit and working tree, etc
# echo "It's a new day for git" >> hello
# git-diff
2.5. 查看版本历史
查看修改、提交记录
# git-log
2.6. 更新某个分支或标签
Checkout a branch or paths to the working tree
# git-checkout -f
-b
Create a new branch named and start it at ; see git-branch(1) for details.
3. 服务器端使用
3.1. 创建版本库
创建一个版本库git-init-db
$ mkdir gittutorcn
$ cd gittutorcn
$ git-init-db
3.2. 标签操作
3.2.1. 查看所有标签
git-tag -n
3.2.2. 创建标签
git-tag tag_name -m “xxx”
3.2.3. 删除标签
git-tag -d tag_name
3.3. 分支操作
3.3.1. 分支创建
# git-branch branch-name
3.3.2. 分支删除
# git-branch -D branch-name
3.3.3. 分支变动查看
# git-show-branch
# git-whatchanged
3.3.4. 分支比较
# git-diff branch_name1^ branch_name2
3.3.5. 分支查看[微软用户7]
# git-branch
3.4. 版本回退
git-reset
--mixed
Resets the index but not the working tree (i.e., the changed files are preserved but not marked for commit) and reports what has not been updated. This is the default action.
--soft[微软用户8]
Does not touch the index file nor the working tree at all, but requires them to be in a good order. This leaves all your changed files "Changes to be committed", as git-status would put it.
--hard
Matches the working tree and index to that of the tree being switched to. Any changes to tracked files in the working tree since are lost.
3.5. 版本合并
现在转移我们当前的工作分支到 master,并且将 robin 分支上的工作合并进来
# git-checkout master
# git-merge -m "Merge work in robin" HEAD [微软用户9] robin
或者将某个branch合并到当前分支中
# git-merge -m "Merge work in robin" robin
index
[微软用户10]
git-clone ssh://172.16.8.107/usr/local/src/share/chui/android/git/mydroid_1.5
# git-clone ssh://10.10.10.210/usr/local/src/share/chui/android/git/mydroid_1.5
# git-branch -r
# git-checkout origin/rkdroid
# git log
Checkout
某个分支,加参数-o
# git-branch $(newBranch)
将客户机上的分支mybranch提交到服务机(ssh://172.16.10.210/usr/ … /mydroid_1.5)上的新分支rockchip上。
git push ssh://10.10.10.210/usr/local/src/share/chui/android/git/mydroid_1.5
$(newBranch):rkdroid
将本地分支递交到服务器端分支上
git push origal local_branch:remote_branch
将当前版本提交到remote机器上的mybranch分支上(新建的),
git push rockchip mybranch
删除文件
在使用Svn rm删除一个目录的时候,因为每个目录下都存在.svn目录,记录了这个目录于服务器端仓库相关的信息,所以在commit之前,目录里的其它文件会被删除,但是目录及其子目录并不会被真正删除,只有commit以后,目录才会被删除。
在git中,同样,使用git rm 删除文件。但是git对目录的处理有些奇怪,如果某个目录下的所有文件都被删除以后,该目录就会被自动删除,也就是说你无法保留一个空的目录。你也无法添加一个空目录到仓库里。也就是说git 自动忽略空目录,不知道这样做的目的是什么?
[微软用户1]Fetch后需要checkout,才会将working tree更新为最新代码
[微软用户2]提交修改需要先commit到本地,然后再push到远程版本库中
对于SVN来说,由于是中心式的仓库管理形式,所以并不存在特殊的远程提交的概念,所有的commit操作都可以认为是对远程仓库的更新动作。
在git中,因为有本地仓库和remote仓库之分,所以也就区别于commit 操作,存在额外的push命令,用于将本地仓库的数据更新到远程仓库中去。
git push 可以选择需要提交的更新的分支以及制定该分支在远程仓库上的名字。
[微软用户3]新增文件如果不git-add的话,commit是不会提交的
[微软用户4]如果不带上此参数会有错误信息提示
[微软用户5]是将working tree内容和版本库内容进行比较
[微软用户6]是将working tree内容和index内容进行比较
[微软用户7]如果你忘记了你现在工作在哪个分支上,运行下面命令告诉你:
# cat .git/HEAD
[微软用户8]会更新版本库,而不会更新index和working tree
[微软用户9]we will use the word "branch" to mean a line of development, and "branch head" (or just "head") to mean a reference to the most recent commit on a branch
[微软用户10]Git uses a staging area called "the index" to determine what will be included in a commit. Other version control systems like Subversion use the working copy itself as a staging area (ie. svn ci will check in all changes in the working copy), but Git instead will only commit the changes that you explicitly tell it to commit; here "telling" Git means "adding to the index".