@nemos
2017-05-06T03:15:44.000000Z
字数 3703
阅读 1224
tools
# 基本用户信息配置
git config --global user.name "NeverMoes"
git config --global user.email nevermoes@gmail.com
git config --global core.editor emacs
# 本地建立
git init
git add *.py
git status
git commit -m 'initial project version'
# 远程建立
git clone git://github.com/username/repo.git myrepo
# 添加远程仓库
git remote add origin git://github.com/username/repo.git
# 推送到远程仓库
git push origin master
# 生成ssh键
ssh-keygen -t rsa -b 4096 -C "nevermoes@gmail.com"
# 删除工作区文件,并且将这次删除放入暂存区
git rm [file1] [file2] ...
# 停止追踪指定文件,但该文件会保留在工作区
git rm --cached [file]
# 改名文件,并且将这个改名放入暂存区
git mv [file-original] [file-renamed]
# 提交工作区自上次 commit 之后的变化,直接到仓库区
git commit -a
# 提交时显示所有 diff 信息
git commit -v
# 使用一次新的 commit,替代上一次提交
# 如果代码没有任何新变化,则用来改写上一次 commit 的提交信息
git commit --amend -m [message]
# 重做上一次 commit,并包括指定文件的新变化
git commit --amend ...
# 恢复暂存区的指定文件到工作区
$ git checkout [file]
# 恢复某个 commit 的指定文件到工作区
$ git checkout [commit] [file]
# 恢复上一个 commit 的所有文件到工作区
$ git checkout .
# 重置暂存区的指定文件,与上一次 commit 保持一致,但工作区不变
$ git reset [file]
# 重置暂存区与工作区,与上一次 commit 保持一致
$ git reset --hard
# 重置当前分支的指针为指定 commit,同时重置暂存区,但工作区不变
$ git reset [commit]
# 重置当前分支的 HEAD 为指定 commit,同时重置暂存区和工作区,与指定 commit 一致
$ git reset --hard [commit]
# 重置当前 HEAD 为指定 commit,但保持暂存区和工作区不变
$ git reset --keep [commit]
# 新建一个 commit,用来撤销指定 commit
# 后者的所有变化都将被前者抵消,并且应用到当前分支
$ git revert [commit]
merge和rebase的区别
merge
rebase
# 列出所有本地分支
git branch
# 列出所有远程分支
git branch -r
# 列出所有本地分支和远程分支
git branch -a
# 新建一个分支,但依然停留在当前分支
git branch [branch-name]
# 新建一个分支,并切换到该分支
git checkout -b [branch]
# 新建一个分支,指向指定 commit
git branch [branch] [commit]
# 新建一个分支,与指定的远程分支建立追踪关系
git branch --track [branch] [remote-branch]
# 切换到指定分支,并更新工作区
git checkout [branch-name]
# 建立追踪关系,在现有分支与指定的远程分支之间
git branch --set-upstream [branch] [remote-branch]
# 合并指定分支到当前分支
git merge [branch]
# 选择一个 commit,合并进当前分支
git cherry-pick [commit]
# 删除分支
git branch -d [branch-name]
# 删除远程分支
git push origin --delete
git branch -dr
# 列出所有 tag
git tag
# 新建一个 tag 在当前 commit
git tag [tag]
# 新建一个 tag 在指定 commit
git tag [tag] [commit]
# 新建一个附注标签
git tag -a [tag] -m [msg]
# 查看 tag 信息
git show [tag]
# 切换到标签
git checkout [tagname]
# 提交指定 tag
git push [remote] [tag]
# 提交所有 tag
git push [remote] --tags
# 新建一个分支,指向某个 tag
git checkout -b [branch] [tag]
# 删除标签
git tag -d [tag]
# 下载远程仓库的所有变动
git fetch [remote]
# 显示所有远程仓库
git remote -v
# 显示远程仓列表
git remote show
# 显示某个远程仓库的信息
git remote show [remote]
# 增加一个新的远程仓库,并命名
git remote add [shortname] [url]
# 远程仓库重命名
git remote rename [name1] [name2]
# 移除仓库
git remote rm [shortname]
# 移除远程分支
git push origin --delete [branch]
# 取回远程仓库的变化,并与本地分支合并
git pull [remote] [branch]
# 上传本地指定分支到远程仓库
git push [remote] [branch]
# 强行推送当前分支到远程仓库,即使有冲突
git push [remote] --force
# 推送所有分支到远程仓库
git push [remote] --all
git stash #存储工作区
git stash list #查看stash列表
git stash apply #恢复工作区,但不删除stash
git stash drop #删除stash
git stash xxx #恢复同时删除
git status # 详细状态
git status --short/-s # 简短状态
示例
git status -s
M README # 右M表示修改但未放入暂存区
A lib/git.rb # A表示新添加到暂存区
M lib/simplegit.rb # 左M表示被修改且放入暂存区
?? LICENSE.txt # ??表示未跟踪
git diff # 比较工作目录与暂存区的差异
git diff --cached/staged # 比较暂存区与上次提交的差异
git log # 列出所有更新
git log -p -2 # 列出最近两次提交的差异
具体再参考 --help
优先级
1. .git/config
项目配置
2. ~\.gitconfig
用户配置
3. \ect\gitconfig
全局配置
git config --list #显示配置信息
git config --global alias.co checkout #设置指令别名
# 此为注释 – 将被 Git 忽略
# 忽略所有 .a 结尾的文件
*.a
# 但 lib.a 除外
!lib.a
# 仅仅忽略项目根目录下的 TODO 文件,不包括 subdir/TODO
/TODO
# 忽略 build/ 目录下的所有文件
build/
# 会忽略 doc/notes.txt 但不包括 doc/server/arch.txt
doc/*.txt
# ignore all .txt files in the doc/ directory
doc/**/*.txt
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
<iframe src="//ghbtns.com/github-btn.html?user=alloyteam&repo=alloytouch&type=watch&count=true" allowtransparency="true" frameborder="0" scrolling="0" width="110" height="20"></iframe>
改hosts
151.101.72.249 http://global-ssl.fastly.Net
192.30.253.112 http://github.com
学习使用
廖雪峰的git教程
猴子都能懂的git教程
pro git V2
githug
有关 git 的学习资料
Git 常用命令图解
命令参考
【开源必备】常用git命令
Git常用命令及使用心得
这些GIT经验够你用一年了
工作流
Git 工作流的一些经验分享
大话 Git 工作流
团队
团队开发Git分支管理策略
其他
盘点那些不知名却常用的 Git 操作