1.git clone http://xxxxxxx.git
make a clone of the code from the url
2.git remote add xxx_alias http://xxxxxx.git
give a alias to the url
3.git pull xxx_alias master
==svn checkout
4.git diff master
show the difference of version on server and local version
5.git diff master > ~/Desktop/tmp.patch
make a path with version on server and local version
6.After I executed the command 'git config --global core.autocrlf false' in my git command line window, the code(checked out after the command execution) will be the same as the ones checked out from svn. ==> remove ^M issue
7.git checkout -f
To revert the change unchecked.
---------Git Info-------
http://trac.parrot.org/parrot/wiki/git-svn-tutorial
发现一个不错的Git中文翻译的书籍 http://progit.org/book/zh/。根据其内容,总结 Git 命令集如下:
版本控制系统(VCS) 可以分为集中化的版本控制(CVCS), 如 CVS, Subversion, Perforce 等; 和分布式版本控制系统(DVCS),如 Git 等。
文件状态:未跟踪(untracked)和已跟踪(tracked),已跟踪又分为三种状态:已提交(committed),已修改(modified)和已暂存(staged)
未跟踪的文件是放在工作目录 (working directory) 中的;
已修改的文件是放在工作目录 (working directory) 中的;
已暂存的文件(快照)是放在暂存区 (staging area) 中的;
已提交的文件是放在git 目录(repository) 中的;
用git status 查看目前所有文件的状态。
Git 配置类型
/etc/gitconfig 所有用户 --system
~/.gitconfig 本用户 --global
.git/config 本项目
用户信息
$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com
文本编辑器
$ git config --global core.editor emacs
查看配置信息
$ git config --list
获取帮助
$ git help <verb>
常用命令:
初始化仓库: $ git init
克隆仓库: $ git clone git://github.com/schacon/grit.git
跟踪一个新文件: $ git add readme.txt
跟踪一组新文件: $ git add *.txt
跟踪一个目录中的所有文件: $ git add mydir
暂存一个文件: $ git add readme.txt
忽略某些文件: 在.gitignore 文件中加入下面两行: *.[oa] *~
*.a # 忽略所有 .a 结尾的文件
!lib.a # 但 lib.a 除外
/TODO # 仅仅忽略项目根目录下的 TODO 文件,不包括 subdir/TODO
build/ # 忽略 build/ 目录下的所有文件
doc/*.txt # 会忽略 doc/notes.txt 但不包括 doc/server/arch.txt
查看尚未暂存的更新: $ git diff
查看尚未提交的更新: $ git diff --cached 或 $ git diff --staged
提交更新(只更新暂存中的): $ git commit
提交更新(只更新暂存中的): $ git commit -m "task12, added implementation for usr mrg."
直接提交更新(更新暂存中的和已修改的): $ git commit -a -m "task12, added implementation forg."
移除文件(从暂存区和工作区中删除): $ git rm a.a
移除文件(只从暂存区中删除): $ git rm --cached a.a
移除文件(从Git中删除): $ git commit -m "remove"
强行移除修改后文件(从暂存区和工作区中删除): $ git rm -f a.a
移除目录下的所有文件(递归): $ git rm log//*.log
移除目录下的所有文件(无递归): $ git rm log/*.log
改名(只改工作区和暂存区): $ git mv file1 file2
相当于: $ mv README.txt README
$ git rm README.txt
$ git add README
查看提交历史(全部): $ git log
查看最近2次提交历史并查看差异: $ git log -p -2
查看最近2周内提交历史: $ git log --since=2.weeks
查看某个时刻之后的提交历史: $ git log --since="2008-09-14"
查看某个时刻以前的提交历史: $ git log --until="2008-09-14"
查看某个作者的提交历史: $ git log --author="stupid"
其他:
-(n) 仅显示最近的 n 条提交
--since, --after 仅显示指定时间之后的提交。
--until, --before 仅显示指定时间之前的提交。
--author 仅显示指定作者相关的提交。
--committer 仅显示指定提交者相关的提交。
例如:
$ git log --pretty="%h:%s" --author=gitster --since="2008-10-01" /
--before="2008-11-01" --no-merges -- t/
查看提交历史,但仅显示增加行: $ git log --stat
查看提交历史,并单行显示: $ git log --pretty=oneline
查看提交历史,并格式化显示: $ git log --pretty=format:"%h - %an, %ar : %s"
%H 提交对象(commit)的完整哈希字串
%h 提交对象的简短哈希字串
%T 树对象(tree)的完整哈希字串
%t 树对象的简短哈希字串
%P 父对象(parent)的完整哈希字串
%p 父对象的简短哈希字串
%an 作者(author)的名字
%ae 作者的电子邮件地址
%ad 作者修订日期(可以用 -date= 选项定制格式)
%ar 作者修订日期,按多久以前的方式显示
%cn 提交者(committer)的名字
%ce 提交者的电子邮件地址
%cd 提交日期
%cr 提交日期,按多久以前的方式显示
%s 提交说明
查看提交历史,并图形化显示: $ git log --pretty=format:"%h %s" --graph
其它:
-p 按补丁格式显示每个更新之间的差异。
--stat 显示每次更新的文件修改统计信息。
--shortstat 只显示 --stat 中最后的行数修改添加移除统计。
--name-only 仅在提交信息后显示已修改的文件清单。
--name-status 显示新增、修改、删除的文件清单。
--abbrev-commit 仅显示 SHA-1 的前几个字符,而非所有的 40 个字符。
--relative-date 使用较短的相对时间显示(比如,“2 weeks ago”)。
--graph 显示 ASCII 图形表示的分支合并历史。
--pretty 使用其他格式显示历史提交信息。可用的选项包括 oneline,short,full,fuller 和 format(后跟指定格式)。
图形化工具: gitk
http://www.enjoyrails.com/wikis/Git%E4%B8%80%E5%88%86%E9%92%9F%E4%B8%8A%E6%89%8B
Git一分钟上手
流程:取代码 → 每次工作前更新代码到最新版本 → 修改代码 → 提交代码到服务器
取代码及修改全局设置
设置用户名与邮箱
1 2 | git config --global user.name "My Name" git config --global user.email "my@email.com" |
从已有的git库中提取代码
| git clone git@server:app.git myrepo |
每次更改代码的操作
更新本地代码到最新版本(需要merge才能合到本地代码中)
| git fetch |
合并更新后的代码到本地
| git merge |
更新代码方式的另一种方法(git pull是git fetch和git merge命令的一个组合)
| git pull |
修改代码后,查看已修改的内容
| git diff --cached |
将新增加文件加入到git中
| git add file1 file2 file3 |
从git中删除文件
1 2 | git rm file1 git rm -r dir1 |
提交修改
| git commit -m 'this is memo' |
如果想省掉提交之前的 git add 命令,可以直接用
| git commit -a -m 'this is memo' |
commit和commit -a的区别, commit -a相当于:
- 第一步:自动地add所有改动的代码,使得所有的开发代码都列于index file中
- 第二步:自动地删除那些在index file中但不在工作树中的文件
- 第三步:执行commit命令来提交
提交所有修改到远程服务器,这样,其它团队成员才能更新到这些修改
| git push |
其它常用命令
显示commit日志
| git log |
不仅显示commit日志,而且同时显示每次commit的代码改变。
| git log -p |
回滚代码:
| git revert HEAD |
你也可以revert更早的commit,例如:
| git revert HEAD^ |
销毁自己的修改
| git reset --hard |
查看最新版本和上一个版本的差异(一个^表示向前推进一个版本)
| git diff HEAD HEAD^ |
将branchname分支合并到当前分支中。(如果合并发生冲突,需要自己解决冲突)
| git merge branchname |
解决冲突
当merge命令自身无法解决冲突的时候,它会将工作树置于一种特殊的状态,并且给用户提供冲突信息,以期用户可以自己解决这些问题。当然在这个时候,未发生冲突的代码已经被git merge登记在了index file里了。如果你这个时候使用git diff,显示出来的只是发生冲突的代码信息。
在你解决了冲突之前,发生冲突的文件会一直在index file中被标记出来。这个时候,如果你使用git commit提交的话,git会提示:filename.txt needs merge
在发生冲突的时候,如果你使用git status命令,那么会显示出发生冲突的具体信息。
在你解决了冲突之后,你可以使用如下步骤来提交:
第一步(如果需要增加文件):
| git add file1 |
第二步:
| git commit |
git恢复删除了的文件
git pull 从git服务器取出,并且和本地修改merge, 类似于SVN up,但是对删除的文件不管用,恢复删除文件用
| git checkout -f |