[关闭]
@nemos 2017-05-06T03:15:44.000000Z 字数 3703 阅读 1224

Git

tools


基本操作

  1. # 基本用户信息配置
  2. git config --global user.name "NeverMoes"
  3. git config --global user.email nevermoes@gmail.com
  4. git config --global core.editor emacs
  5. # 本地建立
  6. git init
  7. git add *.py
  8. git status
  9. git commit -m 'initial project version'
  10. # 远程建立
  11. git clone git://github.com/username/repo.git myrepo
  12. # 添加远程仓库
  13. git remote add origin git://github.com/username/repo.git
  14. # 推送到远程仓库
  15. git push origin master
  16. # 生成ssh键
  17. ssh-keygen -t rsa -b 4096 -C "nevermoes@gmail.com"

ch


进阶参考

增删

  1. # 删除工作区文件,并且将这次删除放入暂存区
  2. git rm [file1] [file2] ...
  3. # 停止追踪指定文件,但该文件会保留在工作区
  4. git rm --cached [file]
  5. # 改名文件,并且将这个改名放入暂存区
  6. git mv [file-original] [file-renamed]

提交

  1. # 提交工作区自上次 commit 之后的变化,直接到仓库区
  2. git commit -a
  3. # 提交时显示所有 diff 信息
  4. git commit -v
  5. # 使用一次新的 commit,替代上一次提交
  6. # 如果代码没有任何新变化,则用来改写上一次 commit 的提交信息
  7. git commit --amend -m [message]
  8. # 重做上一次 commit,并包括指定文件的新变化
  9. git commit --amend ...

撤销

  1. # 恢复暂存区的指定文件到工作区
  2. $ git checkout [file]
  3. # 恢复某个 commit 的指定文件到工作区
  4. $ git checkout [commit] [file]
  5. # 恢复上一个 commit 的所有文件到工作区
  6. $ git checkout .
  7. # 重置暂存区的指定文件,与上一次 commit 保持一致,但工作区不变
  8. $ git reset [file]
  9. # 重置暂存区与工作区,与上一次 commit 保持一致
  10. $ git reset --hard
  11. # 重置当前分支的指针为指定 commit,同时重置暂存区,但工作区不变
  12. $ git reset [commit]
  13. # 重置当前分支的 HEAD 为指定 commit,同时重置暂存区和工作区,与指定 commit 一致
  14. $ git reset --hard [commit]
  15. # 重置当前 HEAD 为指定 commit,但保持暂存区和工作区不变
  16. $ git reset --keep [commit]
  17. # 新建一个 commit,用来撤销指定 commit
  18. # 后者的所有变化都将被前者抵消,并且应用到当前分支
  19. $ git revert [commit]

分支

merge和rebase的区别

merge
git merge

rebase
git rebase

  1. # 列出所有本地分支
  2. git branch
  3. # 列出所有远程分支
  4. git branch -r
  5. # 列出所有本地分支和远程分支
  6. git branch -a
  7. # 新建一个分支,但依然停留在当前分支
  8. git branch [branch-name]
  9. # 新建一个分支,并切换到该分支
  10. git checkout -b [branch]
  11. # 新建一个分支,指向指定 commit
  12. git branch [branch] [commit]
  13. # 新建一个分支,与指定的远程分支建立追踪关系
  14. git branch --track [branch] [remote-branch]
  15. # 切换到指定分支,并更新工作区
  16. git checkout [branch-name]
  17. # 建立追踪关系,在现有分支与指定的远程分支之间
  18. git branch --set-upstream [branch] [remote-branch]
  19. # 合并指定分支到当前分支
  20. git merge [branch]
  21. # 选择一个 commit,合并进当前分支
  22. git cherry-pick [commit]
  23. # 删除分支
  24. git branch -d [branch-name]
  25. # 删除远程分支
  26. git push origin --delete
  27. git branch -dr

标签

  1. # 列出所有 tag
  2. git tag
  3. # 新建一个 tag 在当前 commit
  4. git tag [tag]
  5. # 新建一个 tag 在指定 commit
  6. git tag [tag] [commit]
  7. # 新建一个附注标签
  8. git tag -a [tag] -m [msg]
  9. # 查看 tag 信息
  10. git show [tag]
  11. # 切换到标签
  12. git checkout [tagname]
  13. # 提交指定 tag
  14. git push [remote] [tag]
  15. # 提交所有 tag
  16. git push [remote] --tags
  17. # 新建一个分支,指向某个 tag
  18. git checkout -b [branch] [tag]
  19. # 删除标签
  20. git tag -d [tag]

远程

  1. # 下载远程仓库的所有变动
  2. git fetch [remote]
  3. # 显示所有远程仓库
  4. git remote -v
  5. # 显示远程仓列表
  6. git remote show
  7. # 显示某个远程仓库的信息
  8. git remote show [remote]
  9. # 增加一个新的远程仓库,并命名
  10. git remote add [shortname] [url]
  11. # 远程仓库重命名
  12. git remote rename [name1] [name2]
  13. # 移除仓库
  14. git remote rm [shortname]
  15. # 移除远程分支
  16. git push origin --delete [branch]
  17. # 取回远程仓库的变化,并与本地分支合并
  18. git pull [remote] [branch]
  19. # 上传本地指定分支到远程仓库
  20. git push [remote] [branch]
  21. # 强行推送当前分支到远程仓库,即使有冲突
  22. git push [remote] --force
  23. # 推送所有分支到远程仓库
  24. git push [remote] --all

stash

  1. git stash #存储工作区
  2. git stash list #查看stash列表
  3. git stash apply #恢复工作区,但不删除stash
  4. git stash drop #删除stash
  5. git stash xxx #恢复同时删除

信息查看

status

  1. git status # 详细状态
  2. git status --short/-s # 简短状态

示例

  1. git status -s
  2. M README # 右M表示修改但未放入暂存区
  3. A lib/git.rb # A表示新添加到暂存区
  4. M lib/simplegit.rb # 左M表示被修改且放入暂存区
  5. ?? LICENSE.txt # ??表示未跟踪

diff

  1. git diff # 比较工作目录与暂存区的差异
  2. git diff --cached/staged # 比较暂存区与上次提交的差异

log

  1. git log # 列出所有更新
  2. git log -p -2 # 列出最近两次提交的差异
  3. 具体再参考 --help

配置

配置文件

优先级
1. .git/config 项目配置
2. ~\.gitconfig 用户配置
3. \ect\gitconfig 全局配置

  1. git config --list #显示配置信息
  1. git config --global alias.co checkout #设置指令别名

gitignore

  1. # 此为注释 – 将被 Git 忽略
  2. # 忽略所有 .a 结尾的文件
  3. *.a
  4. # 但 lib.a 除外
  5. !lib.a
  6. # 仅仅忽略项目根目录下的 TODO 文件,不包括 subdir/TODO
  7. /TODO
  8. # 忽略 build/ 目录下的所有文件
  9. build/
  10. # 会忽略 doc/notes.txt 但不包括 doc/server/arch.txt
  11. doc/*.txt
  12. # ignore all .txt files in the doc/ directory
  13. doc/**/*.txt

gitignore参考配置

别名

  1. git config --global alias.co checkout
  2. git config --global alias.br branch
  3. git config --global alias.ci commit

其他

嵌入图标

  1. <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>

git clone速度过慢

改hosts

  1. 151.101.72.249 http://global-ssl.fastly.Net
  2. 192.30.253.112 http://github.com

参考

学习使用
廖雪峰的git教程
猴子都能懂的git教程
pro git V2
githug
有关 git 的学习资料
Git 常用命令图解
命令参考
【开源必备】常用git命令
Git常用命令及使用心得
这些GIT经验够你用一年了
工作流
Git 工作流的一些经验分享
大话 Git 工作流
团队
团队开发Git分支管理策略
其他
盘点那些不知名却常用的 Git 操作


添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注