| English | 中文 |
说明
Git 常用、不常用、实用的命令,这些命令都是在日常使用过程中遇到过整理下来的,留作备忘,如果对这些命令还不明白什么意思,请参考学习:Git 新手入门 。
在日常开发中,由于采用 Git 作为版本控制,经常跟命令行打交道,记录整理下使用到的一些命令,方便回顾。
[TOC]
master : default development branch
origin : default upstream repository
HEAD : current branch and commit
HEAD^ : parent of HEAD
HEAD~4 : the great-great grandparent of HEAD
下面的只是例子,想改成什么跟随自己的意愿即可。
git config --global alias.st status //status 缩写成 st
git config --global alias.co checkout //checkout 缩写成 co
git config --global alias.br branch //branch 缩写成 br
git config --global alias.ci commit //commit 缩写成 ci
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"
如果不想使用了,删除掉的话,直接删除 conf 配置文件中的行即可,global 的在当前用户下vim ~/.gitconfig 删除 alias下你配置的内容接口;若是当前仓库则在 .git/config 中。
Git 配置文件分为三级,系统级(–system)、用户级(–global)和目录级(–local),三者的使用优先级以离目录 (repository)最近为原则,如果三者的配置不一样,则生效优先级 目录级>用户级>系统级,可以通过 git config --help 查看更多内容。
/etc/gitconfig 文件中,可以使用 git config --system user.name "jim" ,git config --sytem user.email "jim.jim@gmail.com" 来进行配置,该配置对系统上所有用户及他们所拥有的仓库都生效的配置值。~/.gitconfig 中,可以使用 git config --global user.name "jim" ,git config --global user.email "jim.jim@gmail.com" 来进行配置,该配置对当前用户上所有的仓库有效。.git/config 中,可以使用 git config --local user.name "jim" , git config --local user.email "jim.jim@gmail.com" 来进行配置,只对当前仓库生效。git add filename / git stage filenamegit add --all / git add -Agit commit -m 'commit message' / git commit -a -m 'commit message' 注意理解 -a 参数的意义git rm filenamegit rm --cached filenamegit mv filename newfilename 或者直接修改完毕文件名 ,进行git add -A && git commit -m 'commit message' Git会自动识别是重命名了文件git pull (origin branchname) 可以指定分支名,也可以忽略。pull 命令自动 fetch 远程代码并且 merge,如果有冲突,会显示在状态栏,需要手动处理。更推荐使用:git fetch 之后 git merge --no-ff origin branchname 拉取最新的代码到本地仓库,并手动 merge 。git clone repository-url / git clone repository-url local-directoryname
git clone git://github.com/jquery/jquery.gitgit clone git://github.com/jquery/jquery.git my-jquerygit remote -vgit remote add [name] [repository-url]git remote rm [name]git remote set-url origin new-repository-urlgit pull [remoteName] [localBranchName]git push [remoteName] [localBranchName] 例: git push -u orgin master 将当前分支推送到远端master分支git push origin test:master (把本地的某个分支 test 提交到远程仓库,并作为远程仓库的 master 分支) 提交本地 test 分支作为远程的 test 分支 :git push origin test:test checkout命令用于从历史提交(或者暂存区域)中拷贝文件到工作目录,也可用于切换分支。
匿名分支:如果既没有指定文件名,也没有指定分支名,而是一个标签、远程分支、SHA-1值或者是像 master~3 类似的东西,就得到一个匿名分支,称作 detached HEAD(被分离的 HEAD 标识)。

当HEAD处于分离状态(不依附于任一分支)时,提交操作可以正常进行,但是不会更新任何已命名的分支。(你可以认为这是在更新一个匿名分支。)一旦此后你切换到别的分支,比如说 master,那么这个提交节点(可能)再也不会被引用到,然后就会被丢弃掉了。注意这个命令之后就不会有东西引用 2eecb。详细查看:visual-git-guide#detached
但是,如果你想保存这个状态,可以用命令 git checkout -b name 来创建一个新的分支。

Description : Shows the commit logs. The command takes options applicable to the git rev-list command to control what is shown and how, and options applicable to the git diff-* commands to control how the changes each commit introduces are shown. git log [options] [revision range] [path]
常用命令整理如下:
git loggit log -pgit log --statgit log --pretty=oneline / git log --pretty='format:"%h - %an, %ar : %s'git log -10git log --until=2week 或者指定2周的明确日期,比如:git log --until=2015-08-12git log --since=2week 或者指定2周明确日志,比如:git log --since=2015-08-12git log --committer=user.name / git log --author=user.namegit log --grep '测试'git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit 感觉好用就加成 alias ,方便日后用,方法:git config --global alias.aliasname 'alias-content'log 的目的就是为了查看改动点来排查问题,除了 ` git log 还可以使用 git show 、git blame` 来查看文件的改动。
git blame $filegit show --pretty="" --name-only <sha1-of-commit> 或者 git diff-tree --no-commit-id --name-only -r <sha1-of-commit>git commit --amend -m 'new msg'git commit --amend --author="newName <newEmail>"git rebase 或者 git filter-branch
# git rebase 模式
git rebase -i -p 76892625a7b126f4772f8d7e331ada3552c11ce1
# 弹出编辑器,在需要修改的 commit 处 由 picked 改变为 edit ,然后 wq 退出 vim;
git commit --amend --author 'newName <newEmail>'
# 执行后即变更了相应的 author 和 email
git rebase --continue
################################################################
# git filter-branch 模式 https://help.github.com/articles/changing-author-info/
git filter-branch --env-filter '
OLD_EMAIL="your-old-email@example.com"
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="your-correct-email@example.com"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_COMMITTER_NAME="$CORRECT_NAME"
export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_AUTHOR_NAME="$CORRECT_NAME"
export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags

git add -A后,需要将某个文件撤回到工作区,即:某个文件不应该在本次commit中:git reset HEAD filenamegit checkout -- filename 注意:一旦执行,所有的改动都没有了,谨慎!谨慎!谨慎!git reset --hard <sha1-of-commit>reset命令把当前分支指向另一个位置,并且有选择的变动工作目录和索引,也用来在从历史仓库中复制文件到索引,而不动工作目录。
将工作区内容回退到远端的某个版本:git reset --hard <sha1-of-commit>
git reset --hard HEAD^ reset index and working directory ,git reset --soft HEAD^ nothing changed to index and working directory ,仅仅将 HEAD 指向git reset --mixed HEAD^ default,reset index ,nothing to working directory 默认选项,工作区代码不改动,添加变更到index区
git revertwill create a new commit that’s the opposite (or inverse) of the given SHA. If the old commit is “matter”, the new commit is “anti-matter”—anything removed in the old commit will be added in the new commit and anything added in the old commit will be removed in the new commit. This is Git’s safest, most basic “undo” scenario, because it doesn’t alter history—so you can now git push the new “inverse” commit to undo your mistaken commit.
git revert [--[no-]edit] [-n] [-m parent-number] [-s] [-S[<keyid>]] <commit>…
git revert --continue
git revert --quit
git revert --abort
回滚撤销一系列 commit 中的一条,并且不影响后续提交的功能 ,git revert c2
(https://blog.csdn.net/u013066244/article/details/79920012) https://blog.csdn.net/u013066244/article/details/79920012
分离 checkout 的功能,之前 checkout 可以切换分支,也可以恢复工作区的修改内容,现在这部分恢复修改内容由命令 restore来实现; git-restore[1] is about restoring files in the working tree from either the index or another commit. This command does not update your branch. The command can also be used to restore files in the index from another commit.
想象一个简单的场景,分支 A,有 c1,c2,c3,c4,c5 五次 commit 提交,后来发现 c2 提交是有问题的,需要回滚,这个时候怎么解决? 方案一 使用 reset 命令 拉个新分支 A_bak ,复制现有分支, git reset –hard c2 , c3,c4,c5 都没有了。 git cherry-pick c3,c4,c5 到 A 分支。
方案二 使用 revert 命令
git revert c2 可能会出现冲突,解决冲突即可。 也可能 revert 的 commit 是个合并 merge 的分支,则需要多做个操作,即 git revert c2 -m 1 , git revert c2 -m 2 ,详情参考:https://blog.csdn.net/u013066244/article/details/79920012
现象:会创建一个新的 commit,即 c6 ,在 c6 的提交中,将 c2 里面的内容 反转掉 ,若有冲突,会展示冲突解决的文件内容。
revert 可以说是就是为了这种场景而产生的,也应该是我们日常工作中经常使用的命令。
git diffgit diff HEADgit diff --cached / git diff --stagedgit diff --stat
git merge branchname ,将 branchname 分支上面的代码合并到当前分支git merge --no-ff branchname 参数 --no-ff 防止 fast-forward 的提交,详情参考:the difference,fast-forward:分支内容一致,指针直接移动,并未能看出分支信息Rebase 同 Merge 的结果是一样的,就是合并本地、远程的改动,但过程中还有区别。
git checkout mywork
git rebase origin
这些命令会把你的”mywork”分支里的每个提交(commit)取消掉,并且把它们临时 保存为补丁(patch)(这些补丁 放到”.git/rebase”目录中),然后把”mywork”分支更新 到最新的”origin”分支,最后把保存的这些补丁应用 到”mywork”分支上。 一张图分清 rebase 和 merge 的区别
在rebase的过程中,也许会出现冲突(conflict). 在这种情况,Git会停止rebase并会让你去解决冲突;在解决完冲突后,用 git-add 命令去更新这些内容的索引(index), 然后,你无需执行 git-commit,只要执行: git rebase --continue 这样git会继续应用(apply)余下的补丁。在任何时候,你可以用 –abort 参数来终止rebase的行动,并且”mywork” 分支会回到rebase开始前的状态。 git rebase --abort
cherry-pick 命令”复制”一个提交节点并在当前分支做一次完全一样的新提交。

Aone2 Git 分支开发部署模型详细解读 http://docs.alibaba-inc.com:8090/pages/viewpage.action?pageId=194872297
分支情况 origin
git checkout -b feature/20161129_163448_newfeature_1

Git-Develop 分支模式是基于 Git 代码库设计的一种需要严格控制发布质量和发布节奏的开发模式。develop 作为固定的持续集成和发布分支,并且分支上的代码必须经过 CodeReview 后才可以提交到 Develop 分支。它的基本流程如下:
git branch 、git branch -v、git branch -vv (查看当前分支 tracking 哪个远端分支)、git branch --merged、git branch --no-merged创建分支:git branch branchname
git branch devgit branch branchname <sha1-of-commit>
git branch hotfix a207a38git checkout -b newbranch localBranch/remoteBranch/commitId/tagbash
git checkout --orphan gh-pages # 创建一个orphan的分支,这个分支是独立的
Switched to a new branch \'gh-pages\'
git rm -rf . # 删除原来代码树下的所有文件
rm \'.gitignore\'
#注意这个时候你用git branch命令是看不见当前分支的名字的,除非你进行了第一次commit。添加新的文件,并且 commit 掉,就能看到分支了。
`切换分支: git checkout branchname
git checkout dev创建新分支并切换到下面:git checkout -b branchname 或者 git branch branchname && git checkout branchname
git checkout -b dev 或 git branch dev && git checkout dev git diff branchname 比较 branchname 分支与当前分支的差异点,若只看文件差异,不看差异内容:git diff branchName --statgit merge branchname 将 branchname 分支代码合并到当前分支git branch -d branchname 强制删除未合并过的分支:git branch -D branchnamegit branch -m dev development 将分支 dev 重命名为 developmentgit branch -r 或 git branch -r -vgit checkout -b local-branchname origin/remote-branchnamegit push origin remote-branchname 或 git push origin local-branchname:remote-branchname
git push (-u) origin dev 或 git push origin dev:devgit push origin dev:mastergit push origin :remote-branchname 或 git push origin --delete remote-branchnamegit branch --track master origin/next 或者 git branch --set-upstream-to=origin/master 看 git 的版本是否支持。git branch -vv 来查看当前 track 的分支情况。新建立分支时会自动 track 相应远程分支,git checkout -b sf origin/serverfix (Branch sf set up to track remote branch serverfix from origin. Switched to a new branch ‘sf’). 也可以手动 track: git branch -u origin/serverfix (Branch serverfix set up to track remote branch serverfix from origin). 等同于命令 git checkout --track origin/serverfix“Checking out a local branch from a remote branch automatically creates what is called a “tracking branch” (or sometimes an “upstream branch”). Tracking branches are local branches that have a direct relationship to a remote branch. If you’re on a tracking branch and type git pull, Git automatically knows which server to fetch from and branch to merge into. When you clone a repository, it generally automatically creates a master branch that tracks origin/master. However, you can set up other tracking branches if you wish – ones that track branches on other remotes, or don’t track the master branch. The simple case is the example you just saw, running git checkout -b [branch] [remotename]/[branch]. This is a common enough operation that git provides the –track shorthand:”
git taggit tag -l 'V1.0.*' 会列出匹配到的,比如 V1.0.1,V1.0.1.1,V1.0.2 等git tag tag-name ,例如: git tag v1.0git tag -a tag-name -m 'msg' ,例如:git tag -a v1.0.0 -m '1.0.0版本上线完毕打tag'
git show v1.0 / git show v1.0.0git show tag-namegit tag -a tagname <sha1-of-commit>
git tag -a v1.0.0 a207a38git tag -d tagnamegit pull remotename --tags 例如:git pull origin --tagsgit push remotename tagname 例如:git push origin v1.0.0git push remotename --tags 例如:git push origin --tagsgit push origin :tagname 或者 git push origin --delete tagname 或者 git push origin :refs/tags/v0.9添加子模块:$ git submodule add [url] [path] 如:$ git submodule add git://github.com/soberh/ui-libs.git src/main/webapp/ui-libs 初始化子模块:$ git submodule init —-只在首次检出仓库时运行一次就行 更新子模块:$ git submodule update —-每次更新或切换分支后都需要运行一下 删除子模块:(分4步走哦) 1) $ git rm –cached [path] 2) 编辑”.gitmodules”文件,将子模块的相关配置节点删除掉 3) 编辑” .git/config”文件,将子模块的相关配置节点删除掉 4) 手动删除子模块残留的目录
经常有这样的事情发生,当你正在进行项目中某一部分的工作,里面的东西处于一个比较杂乱的状态,而你想转到其他分支上进行一些工作。问题是,你不想提交进行了一半的工作,否则以后你无法回到这个工作点。解决这个问题的办法就是 git stash 命令。
stash 可以获取你工作目录的中间状态,也就是你修改过的被追踪的文件和暂存的变更,并将它保存到一个未完结变更的堆栈中,随时可以重新应用。
usage: git stash list [<options>] 查看当前 stash 的列表
or: git stash show [<stash>] 查看某一个版本的详细内容
or: git stash drop [-q|--quiet] [<stash>] 删除 stash 中内容
or: git stash ( pop | apply ) [--index] [-q|--quiet] [<stash>] 将 stash 中的代码应用到工作区中
or: git stash branch <branchname> [<stash>]
or: git stash [save [--patch] [-k|--[no-]keep-index] [-q|--quiet]
[-u|--include-untracked] [-a|--all] [<message>]]
or: git stash clear 清空 stash 中所有内容
alias g='git'
alias ga='git add'
alias gco='git checkout'
alias gcb='git checkout -b'
alias gcm='git checkout master'
alias gcd='git checkout develop'
alias gd='git diff'
alias gf='git fetch'
alias gfo='git fetch origin'
alias gl='git pull'
alias gp='git push'