http://www.cnblogs.com/ang-/p/7352909.html
前言
以前写个一个git小结,但是实际上并不够用。于是结合实际工作上碰到的一些情况,参考了一些资料,重新总结了一下。目标是在日常工作中不用再去查阅其他的资料了,如果有什么遗漏或者错误的地方,请评论指出!
基本概念
Workspace:工作区
Index / Stage:暂存区
Repository:仓库区(或本地仓库)
Remote:远程仓库
文件几种状态
- untracked:git未跟踪的文件,新增的文件未 git add 就会处于这种状态
- not staged:被索引过又被修改了的文件
- staged:通过 git add后即将被提交的文件
创建新仓库
# 在当前目录
git init
配置
# 显示当前的Git配置
git config –list
# 编辑Git配置文件
git config -e [–global]
# 设置提交代码时的用户信息
git config [–global] user.name "example"
git config [–global] user.email "example@gmail.com"
# 配置自动换行,提交到git时自动将换行符转换为lf
git config --global core.autocrlf input
# 配置密钥
ssh-keygen -t rsa -C example@gmail.com # 生成密钥
ssh -T git@github.com # 测试是否成功
# 配置别名,--global 表示全局配置
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
检出仓库
# 创建一个本地仓库的克隆版本:
git clone /path/to/repository
# 如果是远端服务器上的仓库:
git clone username@host:/path/to/repository
# 克隆到自定义文件夹:
git clone username@host:/path/to/repository my-cloned-repo
新建仓库常见流程
# 初始化
git init
# 获取状态
git status
git add README.md
git commit -m "message"
# 连接远程仓库
git remote add origin git@github.com:example/test.git
# 推送内容到远程仓库的同时设置默认跟踪分支
git push -u origin master
gitignore
vim .gitignore
!为模式取反
*.a
!lib.a
添加、删除
# 添加指定文件到暂存区
git add [file1] [file2] ...
# 添加指定目录到暂存区,包括子目录
git add [dir]
# 添加当前目录的所有文件到暂存区,.或*代表全部添加
git add .
# 添加每个变化前,都会要求确认
# 对于同一个文件的多处变化,可以实现分次提交
git add -p
# 删除工作区文件,并且将这次删除放入暂存区
git rm [file1] [file2] ...
# 停止追踪指定文件,但该文件会保留在工作区
git rm --cached [file]
# 文件重命名,并加入暂存区
git mv [file-original] [file-renamed]
# 通配符批量移动
git mv *.html src/
提交
git commit -m "message"
# 补提交文件,提交时漏掉了某些文件时不应该再单独提交一次
git commit --amend
# 覆盖提交日期,不知道有啥实际用途
git commit -m "message" --date "2017-01-01 00:00:00"
branch
# 列出所有本地分支
git branch
# 新建一个分支,但依然停留在当前分支
git branch [branch-name]
# 新建一个分支,并切换到该分支
git checkout -b [branch]
# 切换到指定分支,并更新工作区
git checkout [branch-name]
# 切换到上一个分支
git checkout -
# 建立追踪关系,在现有分支与指定的远程分支之间
git branch --set-upstream [branch] [remote-branch]
# 根据一个特定的提交创建新分支,忘记开新的分支就修改并提交了代码时的处理
git branch test-branch HEAD~1
# 删除分支
git branch -d [branch-name]
# 推送分支到远程仓库
git push origin [branch-name]
# 删除远程分支
$ git push origin --delete [branch-name]
$ git branch -dr [remote/branch]
tag
# 列出所有tag
git tag
# 显示 tag list 和注解
git tag -n
# 在当前commit新建一个轻标签
git tag [tagname]
# 在指定commit新建一个tag
git tag [tagname] [commit]
# 添加注解标签
git tag -a [tagname]
# 添加注解标签并添加注解
git tag -am "message" [tagname]
# 删除本地tag
git tag -d [tagname]
# 删除远程tag
git push origin :refs/tags/[tagName]
# 推送所有tag
git push --tags
# 新建一个分支,指向某个tag
git checkout -b [branch] [tagname]
# 切换到tag
git checkout [tagname]
# tag和分支同名时,显示指定切换到tag
git checkout tags/[tagname]
远程仓库和合并分支
git remote
# 查看远程仓库的 URL
git remote -v
# 添加远程仓库
git remote add origin git@github.com:example/test.git
# 删除远程仓库
git remote rm origin
# 修改远程仓库地址
git remote set-url origin [url]
# 将本地的远端和远端进行同步
git fetch origin
# 将本地的远端合并到本地分支
git merge origin/master
# 拉取远程仓库,这相当于上面两条命令
git pull origin master
# 使用rebase可以使提交的历史记录显得更简洁
git rebase mater
# 也可以指定分支:
git pull origin remote:local
# 推送:
git push origin local:remote
改写提交
# 摘出某个提交
git cherry-pick [hash]
# 交互式提交,当涉及提交修改时使用,如 squash、调整 commit 顺序等
git rebase -i
git rebase -i HEAD~4
# 将 upstream 后的 commit 节点嫁接到 newbase,如果有 branch ,会先 checkout 到这个 branch,再 rebase
git rebase --onto <newbase> <upstream> <branch>
# 删除 topicA~3、topicA~4 这两个 commit
git rebase --onto topicA~5 topicA~3 topicA
# 中途要停止rebase操作
git rebase --abort
# 复原到rebase之前的状态
git reset --hard ORIG_HEAD
1)squash合并多个提交:
在编辑 commit message 时 hash 值前的p(pick)表示 use commit,s(squash)表示 use commit, but meld into privious commit