my-git

English 中文

说明

Git 常用、不常用、实用的命令,这些命令都是在日常使用过程中遇到过整理下来的,留作备忘,如果对这些命令还不明白什么意思,请参考学习:Git 新手入门

在日常开发中,由于采用 Git 作为版本控制,经常跟命令行打交道,记录整理下使用到的一些命令,方便回顾。

[TOC]

Concept

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

Alias

下面的只是例子,想改成什么跟随自己的意愿即可。

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 Config

Git 配置文件分为三级,系统级(–system)、用户级(–global)和目录级(–local),三者的使用优先级以离目录 (repository)最近为原则,如果三者的配置不一样,则生效优先级 目录级>用户级>系统级,可以通过 git config --help 查看更多内容。

Basic Usage

Repository

Checkout

checkout命令用于从历史提交(或者暂存区域)中拷贝文件到工作目录,也可用于切换分支。 匿名分支:如果既没有指定文件名,也没有指定分支名,而是一个标签、远程分支、SHA-1值或者是像 master~3 类似的东西,就得到一个匿名分支,称作 detached HEAD(被分离的 HEAD 标识)。

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

Log

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]

常用命令整理如下:

log 的目的就是为了查看改动点来排查问题,除了 ` git log 还可以使用 git showgit blame` 来查看文件的改动。

Undo things

Reset

reset命令把当前分支指向另一个位置,并且有选择的变动工作目录和索引,也用来在从历史仓库中复制文件到索引,而不动工作目录。

将工作区内容回退到远端的某个版本:git reset --hard <sha1-of-commit>

Revert

git revert will 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

Restore

分离 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.

Revert VS Reset VS Restore

想象一个简单的场景,分支 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 可以说是就是为了这种场景而产生的,也应该是我们日常工作中经常使用的命令。

Diff

Merge

Rebase

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

cherry-pick 命令”复制”一个提交节点并在当前分支做一次完全一样的新提交。

Branch workflow

Aone2 Git 分支开发部署模型详细解读 http://docs.alibaba-inc.com:8090/pages/viewpage.action?pageId=194872297

分支情况 origin

Git-Develop 分支模式是基于 Git 代码库设计的一种需要严格控制发布质量和发布节奏的开发模式。develop 作为固定的持续集成和发布分支,并且分支上的代码必须经过 CodeReview 后才可以提交到 Develop 分支。它的基本流程如下:

Branch 命令

“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:”

Tag

Submodule

添加子模块:$ 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) 手动删除子模块残留的目录

Stash

经常有这样的事情发生,当你正在进行项目中某一部分的工作,里面的东西处于一个比较杂乱的状态,而你想转到其他分支上进行一些工作。问题是,你不想提交进行了一半的工作,否则以后你无法回到这个工作点。解决这个问题的办法就是 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 中所有内容

oh-my-zsh 常用命令

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'