@nemos
2017-05-06T03:15:44.000000Z
字数 3703
阅读 1289
tools
# 基本用户信息配置git config --global user.name "NeverMoes"git config --global user.email nevermoes@gmail.comgit config --global core.editor emacs# 本地建立git initgit add *.pygit statusgit 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]# 新建一个分支,指向指定 commitgit 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 --deletegit branch -dr
# 列出所有 taggit tag# 新建一个 tag 在当前 commitgit tag [tag]# 新建一个 tag 在指定 commitgit tag [tag] [commit]# 新建一个附注标签git tag -a [tag] -m [msg]# 查看 tag 信息git show [tag]# 切换到标签git checkout [tagname]# 提交指定 taggit push [remote] [tag]# 提交所有 taggit push [remote] --tags# 新建一个分支,指向某个 taggit 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 #恢复工作区,但不删除stashgit stash drop #删除stashgit stash xxx #恢复同时删除
git status # 详细状态git status --short/-s # 简短状态
示例
git status -sM 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.txtdoc/*.txt# ignore all .txt files in the doc/ directorydoc/**/*.txt
git config --global alias.co checkoutgit config --global alias.br branchgit 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.Net192.30.253.112 http://github.com
学习使用
廖雪峰的git教程
猴子都能懂的git教程
pro git V2
githug
有关 git 的学习资料
Git 常用命令图解
命令参考
【开源必备】常用git命令
Git常用命令及使用心得
这些GIT经验够你用一年了
工作流
Git 工作流的一些经验分享
大话 Git 工作流
团队
团队开发Git分支管理策略
其他
盘点那些不知名却常用的 Git 操作