- Arabic Git Cheat Sheet
- English Git Cheat Sheet
- Hindi Git Cheat Sheet
- Turkish Git Cheat Sheet
- Spanish Git Cheat Sheet
Git cheat sheet 让你不用再去记所有的git命令。
欢迎贡献内容、更新语法错误,也欢迎添加你母语版本的Git cheat sheet。
- Workspace:工作区
- Index / Stage:暂存区
- Repository:仓库区(或本地仓库)
- Remote:远程仓库
$ git config --list
$ git config --local --list
$ git config --global --list
$ git config --system --list
$ git config --global user.name “[firstname lastname]”
$ git config --global user.email “[valid-email]”
$ git config --global color.ui auto
$ git config --global core.editor vi
<repo>/.git/config
~/.gitconfig
/etc/gitconfig
# 通过SSH
$ git clone ssh://user@domain.com/repo.git
# 通过HTTP
$ git clone http://domain.com/user/repo.git
# 通过HTTP,包含子模块
$ git clone --recursive http://domain.com/user/repo.git
# 通过HTTP,获得裸版本库,裸版本库可用于搭建GIT服务器
$ git clone --bare http://domain.com/user/repo.git
# 通过HTTP ,获得裸版本库,并包含所有分支(镜像方式)
$ git clone --mirror http://domain.com/user/repo.git
# 创建新的本地仓库
$ git init
# 创建新的本地裸仓库
$ git init --bare
$ git status
$ git diff
$ git diff --staged
$ git diff --cached
$ git add <file1> <file2> ...
$ git add <dir>
$ git add .
$ git add -p
$ git commit -a
$ git rm <file1> <file2> ...
$ git rm --cached <file>
$ git mv <file-original> <file-renamed>
$ git commit
$ git commit -m 'message here'
$ git commit -am 'message here'
$ git commit --date="`date --date='n day ago'`" -am "Commit Message"
请勿修改已发布的提交记录!
$ git commit --amend
$ GIT_COMMITTER_DATE="date" git commit --amend
$ git commit --amend --date="date"
$ git stash
$ git checkout branch2
$ git stash pop
$ git stash apply
$ git stash drop
$ git grep "Hello"
$ git grep "Hello" v2.5
$ git log
$ git log --oneline
$ git log --author="username"
$ git log -p <file>
$ git log --oneline <origin/master>..<remote/master> --left-right
$ git blame <file>
$ git reflog show
$ git reflog delete
$ git branch
$ git branch -r
$ git branch -a
$ git checkout <branch>
$ git checkout -
$ git checkout -b <branch>
$ git checkout -b <branch> <remote-branch>
# 1.6.2 以上版本的 Git,还可以用 --track 选项简化
$ git checkout --track <remote-branch>
$ git branch <new-branch>
$ git branch <branch> <commit>
$ git branch --track <new-branch> <remote-branch>
$ git branch --set-upstream <branch> <remote-branch>
$ git branch -m <old_branch> <new_branch>
$ git branch -d <branch>
将会丢失未合并的修改!
$ git branch -D <branch>
$ git tag <tag-name>
$ git tag -a <tag-name>
$ git remote -v
$ git remote show <remote>
$ git remote add <remote> <url>
$ git fetch <remote>
$ git pull <remote> <branch>
$ git pull --rebase <remote> <branch>
$ git push <remote> <branch>
#有种方便记忆这条命令的方法:记住我们不久前见过的 git push [远程名] [本地分支]:[远程分支] 语法,如果省略 [本地分支],那就等于是在说“在这里提取空白然后把它变成[远程分支]”
$ git push <remote> :<branch> (since Git v1.5.0)
or
git push <remote> --delete <branch> (since Git v1.7.0)
$ git push --tags
$ git merge <branch>
$ git cherry-pick <commit>
请勿变基已发布的提交!
$ git rebase <branch>
$ git rebase --abort
$ git rebase --continue
在feature分支上发出合并指令git merge master:
这样会把master分支的提交合到feature自己身上,然后再创建一次合并提交,当前分支仍是feature
在master分支上发出合并指令git merge feature:
这样会把feature分支的提交合到master自己身上,然后再创建一次合并提交,当前分支仍是master
在master分支上发出变基指令git rebase feature:
这样会把master分支异于feature分支的提交接在feature之后,当前分支仍是master
在feature分支上发出变基指令git rebase master:
这样会把feature分支异于master分支的提交接在master之后,当前分支仍是feature
比较几种合代码的情况:
如果要进行merge操作的话,最好是在主分支上执行merge,把次分支的代码合进来;
如果要进行rebase操作的话,最好是在次分支上进行,把自己变基合入主分支。
次分支变基完成后,通常还需要转到主分支再次merge
$ git checkout master
$ git merge feature
$ git mergetool
$ git add <resolved-file>
$ git rm <resolved-file>
$ git rebase -i <commit-just-before-first>
把上面的内容替换为下面的内容:
原内容:
pick <commit_id>
pick <commit_id2>
pick <commit_id3>
替换为:
pick <commit_id>
squash <commit_id2>
squash <commit_id3>
$ git reset --hard HEAD
$ git reset HEAD
$ git checkout HEAD <file>
$ git checkout [commit] [file]
$ git revert <commit>
$ git reset --hard <commit>
git reset --hard <remote/branch> e.g., upstream/master, origin/my-feature
$ git reset <commit>
$ git reset --keep <commit>
$ git rm -r --cached .
$ git add .
$ git commit -m "remove xyz file"
- 你需要有一个可以工作的 git 作为前提。
- Git flow 可以工作在 OSX, Linux 和 Windows之下
$ brew install git-flow
$ port install git-flow
$ apt-get install git-flow
安装 git-flow, 你需要 wget 和 util-linux。
$ wget -q -O - --no-check-certificate https://github.com/nvie/gitflow/raw/develop/contrib/gitflow-installer.sh | bash
- 为了自定义你的项目,Git flow 需要初始化过程。
- 使用 git-flow,从初始化一个现有的 git 库内开始。
- 初始化,你必须回答几个关于分支的命名约定的问题。建议使用默认值。
git flow init
- 为即将发布的版本开发新功能特性。
- 这通常只存在开发者的库中。
下面操作创建了一个新的feature分支,并切换到该分支
git flow feature start MYFEATURE
完成开发新特性。这个动作执行下面的操作:
- 合并 MYFEATURE 分支到 'develop'
- 删除这个新特性分支
- 切换回 'develop' 分支
git flow feature finish MYFEATURE
你是否合作开发一项新特性? 发布新特性分支到远程服务器,所以,其它用户也可以使用这分支。
git flow feature publish MYFEATURE
取得其它用户发布的新特性分支。
git flow feature pull origin MYFEATURE
通过下面命令追溯远端上的特性
git flow feature track MYFEATURE
- 支持一个新的用于生产环境的发布版本。
- 允许修正小问题,并为发布版本准备元数据。
- 开始创建release版本,使用 git flow release 命令。
- 'release' 分支的创建基于 'develop' 分支。
- 你可以选择提供一个 [BASE]参数,即提交记录的 sha-1 hash 值,来开启动 release 分支。
- 这个提交记录的 sha-1 hash 值必须是'develop' 分支下的。
git flow release start RELEASE [BASE]
创建 release 分支之后立即发布允许其它用户向这个 release 分支提交内容是个明智的做法。命令十分类似发布新特性:
git flow release publish RELEASE
(你可以通过
git flow release track RELEASE 命令追溯远端的 release 版本)
完成 release 版本是一个大 git 分支操作。它执行下面几个动作:
- 归并 release 分支到 'master' 分支。
- 用 release 分支名打 Tag
- 归并 release 分支到 'develop'
- 移除 release 分支。
git flow release finish RELEASE
不要忘记使用git push --tags将tags推送到远端
紧急修复来自这样的需求:生产环境的版本处于一个不预期状态,需要立即修正。有可能是需要修正 master 分支上某个 TAG 标记的生产版本。
像其它 git flow 命令一样, 紧急修复分支开始自:
$ git flow hotfix start VERSION [BASENAME]
VERSION 参数标记着修正版本。你可以从 [BASENAME]开始,[BASENAME]`为finish release时填写的版本号
当完成紧急修复分支,代码归并回 develop 和 master 分支。相应地,master 分支打上修正版本的 TAG。
git flow hotfix finish VERSION



