Git的一些细节
|
一、对于任何一个文件,在Git 内都只有三种状态:已提交(committed),已修改(modified)和已暂存(staged)。已提交表示该文件已经被安全地保存在本地数据库中了;已修改表示修改了某个文件,但还没有提交保存;已暂存表示把已修改的文件放在下次提交时要保存的清单中。 二、Git工作变量的设置 1、/etc/gitconfig 文件:系统中对所有用户都普遍适用的配置。若使用git config 时用--system 选项,读写的就是这个文件。 2、~/.gitconfig 文件:用户目录下的配置文件只适用于该用户。若使用git config 时用--global 选项,读写的就是这个文件。 3、当前项目的git 目录中的配置文件(也就是工作目录中的.git/config 文件):这里的配置仅仅针对当前项目有效。每一个级别的配置都会覆盖上层的相同配置,所以.git/config 里的配置会覆盖/etc/gitconfig 中的同名变量。 #用户信息 git config --global user.name "John Doe" git config --global user.email johndoe@example.com #默认使用的文本编辑器 git config --global core.editor vim #差异分析工具 git config --global merge.tool vimdiff 三、提交,只有在Changes to be committed下的文件才会被提交。 git status # On branch master # Your branch is ahead of 'origin/master' by 3 commits. # # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: a.txt # # Changes not staged for commit: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: a.txt 跳过暂存区使用:git commit -a -m 'reason...' 四、git diff git diff:此命令比较的是工作目录中当前文件和暂存区域快照之间的差异,也就是修改之后还没有暂存起来的变化内容。 若要看已经暂存起来的文件和上次提交时的快照之间的差异,可以用git diff --cached 五、移除文件 git rm :删除文件,同时删除跟踪 git rm --cached filename :删除跟踪,但保留本地文件。 六、察看提交记录 git log -p:察看细节差异 git log -2:记录条数 git log --stat:显示简要的增改行数统计 选项说明 -(n) 仅显示最近的n 条提交 --since, --after 仅显示指定时间之后的提交。 --until, --before 仅显示指定时间之前的提交。 --author 仅显示指定作者相关的提交。 --committer 仅显示指定提交者相关的提交。 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 提交说明 七、回滚 1、git reset HEAD filename : 撤销暂存区的文件 2、git checkout -- filename : 撤销本地修改 八、远程仓库 察看有哪些远程仓库: git remote -v 添加远程仓库:git remote add pb git://github.com/paulboone/ticgit.git 从远程仓库抓取数据:git fetch pb 如果设置了某个分支用于跟踪某个远端仓库的分支,可以使用git pull 命令自动抓取数据下来,然后将远端分支自动合并到本地仓库中当前分支. 推送数据到远程仓库:git push origin master 查看远程仓库信息 $ git remote show origin * remote origin URL: git@github.com:defunkt/github.git #运行git pull 时将自动合并哪些分支 Remote branch merged with 'git pull' while on branch issues issues #运行git pull 时将自动合并哪些分支 Remote branch merged with 'git pull' while on branch master master #有哪些远端分支还没有同步到本地 New remote branches (next fetch will store in remotes/origin) caching #已同步到本地的远端分支在远端服务器上已被删除 Stale tracking branches (use 'git remote prune') libwalker walker2 Tracked remote branches acl apiv2 dashboard2 issues master postgres #运行git push 时缺省推送的分支是什么 Local branch pushed with 'git push' master:master 九、打标签 轻量级的(lightweight)和含附注的(annotated) git tag git tag -l 'v1.4.2.*' 列出特定标签 含附注的标签 git tag -a v0.8.0 -m 'my tag v0.8.0' 察看标签:git show v0.8.0 补打标签:git tag -a v1.2 9fceb02(校验数) 分享标签:git push origin --tags/v0.8.0 十、使用技巧 1、自动完成 下载Git 的源 代码,进入contrib/completion 目录,会看到一个git-completion.bash 文件。将此文件复制到你自己的用户主目录中(译注:按照下面的示例,还应改名加上点:cp gitcompletion. bash ~/.git-completion.bash),并把下面一行内容添加到你的.bashrc 文件中: source ~/.git-completion.bash 在输入Git 命令的时候可以敲两次跳格键(Tab),就会看到列出所有匹配的可用命令建议: $ git co<tab><tab> commit config 2、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 作者:51cto博客 phper-每天一点点 查看本栏目更多精彩内容:http://www.bianceng.cn/OS/Linux/ (编辑:佛山站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |

