git 学习系列之一:快速入门
2015-10-22 10:41
507 查看
git使用入门:
初次运行git前的配置:Git 自带一个 git config 的工具来帮助设置控制 Git 外观和行为的配置变
量。这些变量存储在三个不同的位置:
. /etc/gitconfig 文件: 包含系统上每一个用户及他们仓库的通用配置。 如果使用带有 --system 选项的 git config 时,它会从此文件读
写配置变量。
. ~/.gitconfig 或 ~/.config/git/config 文件:只针对当前用户。可以传递 --global 选项让 Git 读写此文件。
. 当前使用仓库的 Git 目录中的 config 文件(就是 .git/config):针对该仓库。
每一个级别覆盖上一级别的配置,所以 .git/config 的配置变量会覆
盖 /etc/gitconfig 中的配置变量。
进行git 使用前配置:
$ 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 config user.name
获取git命令帮助:
$ git help <verb> $ git <verb> --help $ man git-<verb> $git help config
进入项目目录进行git管理初始化:
$ git init
文件状态简览:
$ git status -s M README :文件被修改未被放入缓存区 MM Rakefile :文件被修改已放入缓存区后又在工作区被修改 A lib/git.rb : 新增加到缓存区文件。 M lib/simplegit.rb : 文件被修改已放入缓存区 ?? LICENSE.txt : 新添加未跟踪文件。
忽略文件:
重要是.gitignore配置文件
文件 .gitignore 的格式规范如下: • 所有空行或者以 # 开头的行都会被 Git 忽略。 • 可以使用标准的 glob 模式匹配。 • 匹配模式以(/)结尾说明要忽略的是目录。 • 要忽略指定模式以外的文件或目录,可以在模式前加上惊叹号(!)取反
示例:
# no .a files *.a # but do track lib.a, even though you're ignoring .a files above !lib.a # only ignore the root TODO file, not subdir/TODO /TODO # ignore all files in the build/ directory build/ # ignore doc/notes.txt, but not doc/server/arch.txt doc/*.txt # ignore all
GitHub 有一个十分详细的针对数十种工程及语言的 .gitignore 文件列表,你可以在 https://github.com/github/gitignore 找到它.
文件差异比较:
$ git diff :查看尚未暂存的文件更新了哪些部分 $ git diff --cached/staged :已暂存的将要添加到下次提交里的内容
文件提交:
$ git commit :调用文本编辑器增加提交注释。 $ git commit -m :直接增加提交注释。 $ git commit -a -m :跳过暂存直接提交(暂存和提交同时进行)。
文件移除:
$ git rm $ git rm --cached $ git rm -f
查看提交历史:
$ git log :根据时间排序列出所有更新 $ git log -p :显示每次提次的内容差异 $ git log -p -2 : 显示最近两次提交差异 $ git log --stat : 查看每次提交的简略统计信息 $ git log --pretty=oneline/short/full/fuller :将每个提交信息放在一行显示等 $ git log --pretty=format:"%h - %an, %ar : %s" :将提交信息按一定格式输出。 $ git log --pretty=format:"%h - %an, %ar : %s" --graph :将提交信息按一定格式以图表的形式展示分支情况输出。 $ git log --since=2.weeks :列出所有最近两周内的提交 $ git log -Sfunction_name : 列出function_name相关的提交
选项说明 -(n) 仅显示最近的 n 条提交 --since, --after 仅显示指定时间之后的提交。 --until, --before 仅显示指定时间之前的提交。 --author 仅显示指定作者相关的提交。 --committer 仅显示指定提交者相关的提交。 --grep 仅显示含指定关键字的提交 -S 仅显示添加或移除了某个关键字的提交
%H | 提交对象(commit)的完整哈希字串 |
---|---|
%h | 提交对象的简短哈希字串 |
%T | 树对象(tree)的完整哈希字串 |
%t | 树对象的简短哈希字串 |
%P | 父对象(parent)的完整哈希字串 |
%p | 父对象的简短哈希字串 |
%an | 作者(author)的名字 |
%ae | 作者的电子邮件地址 |
%ad | 作者修订日期(可以用 -date= 选项定制格式) |
%ar | 作者修订日期,按多久以前的方式显示 |
%cn | 提交者(committer)的名字 |
%ce | 提交者的电子邮件地址 |
%cd | 提交日期 |
%cr | 提交日期,按多久以前的方式显示 |
%s | 提交说明 |
-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(后跟指定格式)。 |
$ git commit -m 'initial commit' $ git add forgotten_file $ git commit --amend $ git reset HEAD CONTRIBUTING.md
远程仓库:
$ git remote $ git remote add pb https://github.com/paulboone/ticgit $ git remote -v $ git fetch pb $ git remote show $ git remote rename pb paul $ git remote rm paul
Git 标签:
$ git tag $ git tag -l 'v1.8.5*' $ git tag -a v1.4 -m 'my version 1.4' :含附注标签 $ git show v1.4 $ git tag v1.4-lw : 轻量级标签 $git tag -a v1.2 9fceb02 :后期标签指定打标签的提交号 $ git push origin v1.5 :提交某个tag $ git push origin --tags :提交所有tag $ git checkout -b version2 v2.0.0 :为tag创建分支
Git 别名:
$ git config --global alias.co checkout $ git config --global alias.br branch $ git config --global alias.ci commit $ git config --global alias.st status $ git config --global alias.unstage 'reset HEAD --' $ git config --global alias.visual '!gitk'
参考:Pro Git , Scott Chacon 和 Ben Straub 著
相关文章推荐
- RPC failed; result=22, HTTP code = 411
- git更新已經刪除的文件
- 提取Git每次提交后Commit的文件
- GIT迁移服务器
- 分布式版本管理git入门指南使用资料汇总及文章推荐
- Git远程操作详解
- 25个 Git 进阶技巧(翻译)
- 详解版本控制利器Git,SVN的异同以及适用范围
- Ruby实现的删除已经合并的git分支脚本分享
- 在 Shell 提示符中显示 Git 分支名称的方法
- Git使用基础篇(一些常用命令和原理)
- git fork同步是什么意思?
- Git使用小坑 Out of memory错误的解决方法
- Python的高级Git库 Gittle
- 使用GIT进行源码管理――GUI客户端小结
- 使用git代替FTP部署代码到服务器的例子
- linux系统安装git及git常用命令
- 分享下自己总结的Git常用命令
- Git 常用命令速查表(图文+表格)
- mac git xcrun error active developer path 错误