Git基础要点:
- 直接快照,而非比较差异。
- 数据的本地化,有本地库支持
- 基于快照,所以保持了数据完整性
- 多数操作仅为添加数据
- 三种状态来回切换:工作态(modified,new file)、暂存态(staged)、入库态(committed)
基本操作:
取得仓库
- git init
- git clone git://xxxxx/xxx.git
记录每次更新到仓库
- git status
- git add modify-file new-file #到暂存态
- .gitignore #写入需要忽略的文件
- git update-index --assume-unchanged xxx #忽略在版本库中的文件的修改状态
- git diff #工作态<=>暂存态
- git diff --cached #工作台<=>暂存态<=>库
- git diff --staged #工作台<=>暂存态<=>库
- git commit #提交暂存态更新
- git commit -a #提交工作、暂存态更新
- git rm file #删除目前的文件,但是还在版本库中有根
- git mv xxx oxxx #rename file
- git log -l10 #显示10条历史
- git log --pretty=format:"%h %s" --graph #图形显示
- git log --follow -- removed-file-name
撤销操作
- git commit --amend #把目前修改和上次提交合并处理,或者就是修改上次提交的message
- git reset HEAD file #撤销暂存区的文件file
- git reset HEAD #撤销暂存区所有修改
- git checkout -- file #取消对文件的修改
- git revert HEAD # 撤销前一次 commit
- git revert HEAD^ # 撤销前前一次 commit
- git revert commit #(比如:fa042ce57ebbe5bb9c8db709f719cec2c58ee7ff)撤销指定的版本,撤销也会作为一次提交进行保存。
强制修改远程分支(eg:master):
- hack方式修改 .git/refs/remote/origin/master 到commit
- git push -f
- git fetch
- git reset --hard origin/master #其他成员进行修改
远程仓库操作
- git remote -v #列出本地的远程库信息
- git remote add shortname url # 添加一个远程库
- git fetch shortname #抓取远程库
- git push remote-name branch-name #推送到远程
- git remote show shortname
- git remote rename pb paul
- git remote rm pb
打标签
- git tag
- git tag -l 'v1.4.2.*'
- git tag -a v1.4 -m 'message' #带附注的标签
- git show v1.4
- git tag v1.4-1w #轻量级标签
- git push origin v1.5 #分享一个标签
- git push origin --tags#分享所有标签
小技巧
- sudo apt-get install git-core bash-completion #ubuntu 安装git bash 自动补全
- git config --global alias.ci commit #取别名
- git archive master --prefix='project/' --format=zip > 'git describe master'.zip #打包发布
分支处理
- git branch # 列出目前已有分支
- git branch -a # 列出本地分支和远程分支
- git checkout -b bn # 新建并切换到分支bn
- git checkout bt # 切换到已有分支bt
- git branch -d bd # 删除分支bd
- git merge bn # 将目前分支与bn分支合并
- git branch --merged #列出与当前分支合并了的分支
- git branch --no-merged
- git brance -D bx # 强制删除未被合并的分支bx