天天看点

Git

参考, git 使用指南

git init

在任意目录下执行, 将此目录变为工作树, 其实就是在目录中创建隐藏的.git目录(仓库)

仓库和工作树的区别?  仓库是指, 隐藏的.git目录, 工作树指当前整个目录  只要有仓库, 可以通过git clone, 简单的恢复整个工作树
$ git config --global user.name "your name comes here"

注意,这里config可以有3层,默认是--local,即是库级别

    --system           use system config file

    --global             use global config file

    --local               use repository config file 

分别对应于,/etc/gitconfig,~/.gitconfig,.git/config

让你可以从系统,用户,和库的层面去定义不同的配置

参考,http://hezhao2000.blog.163.com/blog/static/12243636720130233261165/

git在向仓库中添加文档时并非简单地复制,需要对文档进行处理, 生成git 仓库所能接受的数据格式, git 称这个过程为"take a snapshot".

所生成的快照被存放到一个临时的存储区域,git称该区域为索引

git add .   #增加当前目录下的所有文件和子目录到索引 git add file1  #增加某个file到索引

设置某文件或目录不被git管理, 这样方便对其他所有文件使用add .

echo "zh" > .gitignore  #忽略zh目录
git commit    #commit索引里面的内容 git commit -m "你的版本更新信息" git commit –a   #commit当前目录下所有改动的文件
Git

git log

git log  #commit的历史log git log --stat –summary  #可以显示出log中commit所提交的改动细节

git show

$ git show dfb02e6e4f2f7b573337763e5c0013802e392818 #根据commitid, 查看某个commit的细节 git show head   #最新的commit细节git show head   #最新的commit细节 git show head^ # 查看head 的父版本commit细节  git show head^^ # 查看head 的祖父版本commit细节git show head^^ # 查看head 的祖父版本commit细节 git show head~4  #^^^^
git reset commitid   #将版本回退到某个commit git reset head #git reset   #将版本回退到最新版本, 其实什么都不做 git reset head^   #将版本回退到最新版本的前一个版本

3种参数, --hard、—soft、—mixed

最直接的是, –hard, 索引和工作树都做reset, 新的更新全部丢弃, 所以比较威胁慎用 

默认的是, –mixed, 只reset索引, 而工作树不变.

使用效果, 今天做了10次commit, 使用reset到退到最初版本, 但是最新的代码还在工作树上的, 所以使用git commit –a, 可以将今天所有的更新一次性commit进去

最温柔的是, –soft, 索引和工作树都不reset, 仅仅把head指向<commit>. 

这种情况, 因为更新还在索引中, 直接git commit就可以重新commit内容 

和mixed的不同就是, mixed索引中的数据会删除, 所以光git commit不行, 需要git commit -a

其他人如果想参加开发, 首先必须把代码在本地clone一份

账户@ip:工作树路径, git-clone 命令只要碰到这种格式,它就会认为该地址是符合ssh 协议的

经过一段时间开发, lyr和tzc的代码需要merge

lyr, 使用pull来将tzc的代码同步到本机

git pull [email protected]:~/work/m2ge

当然如果发生冲突, git会报错, 那么需要手工merge, 然后commit

当如果开发的人多了, 完全依靠lyr去一个个pull, merge, 累死了, 所以提供push, 可以让每个人主动将更新提交 

这样每个人的流程如下,

Git

定义remote git库,

git remote rm origin #删除原有origin git remote add origin url #origin,别名,后面可以用来表示remote git; url, remote git的url git push origin master #后面orgin和master都可以不写, 是默认的, 也可选择push其他的branch

有时候,你push了错误的东西到remote库,你想删除这部分内容,

这样在本地reset,后push会报错的,non-fast-forward

git push -f origin master

这时候需要-f, 进行force-push,不过慎用。。。

其实, 我觉得对应git, branch并没有那么重要, 因为每个人本地都有完整的git仓库, 可以离线开发...已经很方便

但是某些情况下,

比如需要同时支持多个产品的版本, 为了保证本地master的clean……, 这些时候我们还是需要branch的

git branch local #创建branch local git branch #查看当前branch的状况, *表示当前branch, 所有branch是平等的, 只是习惯性将master当作主branch  local  * master git checkout local #切换branch git branch mybranch 92c134fa01 #在特定commit上创建branch
git checkout master # 将当前分支切换为master  git merge local # 将local分支与当前分支合并 git branch -d local #删除分支, 只有被merge过的分支可以删除  git branch -d local #强行删除
git push origin branch #origin为remote库的别名

可以在branch, 选择merge其他branch上的commit, 很方便

git cherry-pick 38361a68 #会将38361a68开头的commit, 在当前branch上增加该commit
git push origin <local_branch> # 创建远程分支, 名字同local  git push origin <local_branch>:<remote_branch> # 创建远程分支, 指定不同的分支名  git push origin :<remote_branch> #:左为空, 则删除远程分支 本文章摘自博客园,原文发布日期:2013-08-07