Git 常用命令汇总

  集中式版本控制系统(SVN),版本库是集中存放在中央服务器的,而干活的时候,用的都是自己的电脑所以要先从中央服务器取得最新的版本然后开始干活干完活了再把自己的活推送给中央服务器集中式版本控制系统最大的毛病就是必须联网才能工作如果在局域网内还好带宽够大速度够快可如果在互联网上遇到网速慢的话可能提交一个10M的文件就需要5分钟
  分布式版本控制系统(Git)没有中央服务器的概念每个人的电脑上都是一个完整的版本库这样你工作的时候就不需要联网了因为版本库就在你自己的电脑上比方说你在自己电脑上改了文件A你的同事也在他的电脑上改了文件A这时你们俩之间只需把各自的修改推送给对方就可以互相看到对方的修改了和集中式版本控制系统相比分布式版本控制系统的安全性要高很多因为每个人电脑里都有完整的版本库某一个人的电脑坏掉了不要紧随便从其他人那里复制一个就可以了而集中式版本控制系统的中央服务器要是出了问题所有人都没法干活了
  在实际使用分布式版本控制系统的时候其实很少在两人之间的电脑上推送版本库的修改因为可能你们俩不在一个局域网内两台电脑互相访问不了也可能同事电脑压根没有开机因此分布式版本控制系统通常也有一台充当中央服务器的电脑但这个服务器的作用仅仅是用来方便交换大家的修改没有它大家也一样干活只是交换修改不方便而已当然Git相对于SVN还有其他诸多优势
 
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配置文件放在用户主目录下的一个隐藏文件.gitconfig中:”命令应该是:cat ~/.gitconfig

git config --list //查看git配置
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

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

+ 系统级配置存储在 /etc/gitconfig 文件中,可以使用 git config --system user.name “meng.chen" ,git config --sytem user.email "meng.chen@17zuoye.com" 来进行配置,该配置对系统上所有用户及他们所拥有的仓库都生效的配置值。

+ 用户级存储在每个用户的 ~/.gitconfig 中,可以使用 git config --global user.name "meng.chen" ,git config --global user.email "meng.chen@17zuoye.com"来进行配置,该配置对当前用户上所有的仓库有效。

+ 目录级存储在每个仓库下的 .git/config 中,可以使用 git config --local user.name "meng.chen" , git config --local user.email "meng.chen@17zuoye.com"来进行配置,只对当前仓库生效。

Basic Usage

  • 添加文件到暂存区(staged):git add filename / git stage filename

  • 将所有修改文件添加到暂存区(staged): git add --all / git add -A

  • 提交修改到暂存区(staged):git commit -m 'commit message' /git commit -a -m 'commit message' 注意理解 -a 参数的意义

  • 从Git仓库中删除文件:git rm filename ( -rf 移除有关的所有东西)

  • 从Git仓库中删除文件,但本地文件保留:git rm --cached filename

  • 重命名某个文件:git 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 

--no-ff指的是强行关闭fast-forward方式。

fast-forward方式就是当条件允许的时候,git直接把HEAD指针指向合并分支的头,完成合并。属于“快进方式”,不过这种情况如果删除分支,则会丢失分支信息。因为在这个过程中没有创建commit

git merge --squash 是用来把一些不必要commit进行压缩,比如说,你的feature在开发的时候写的commit很乱,那么我们合并的时候不希望把这些历史commit带过来,于是使用--squash进行合并,此时文件已经同合并后一样了,但不移动HEAD,不提交。需要进行一次额外的commit来“总结”一下,然后完成最终的合并。

总结:
--no-ff:不使用fast-forward方式合并,保留分支的commit历史
--squash:使用squash方式合并,把多次分支commit历史压缩为一次

  • git fetch

这一步其实是执行了两个关键操作: 创建并更新所有远程分支的本地远程分支,并设定当前分支的FETCH_HEAD远程服务器的master分支  

FETCH_HEAD指的是: 某个branch在服务器上的最新状态',每一个执行过fetch操作的项目'都会存在一个FETCH_HEAD列表, 这个列表保存在 .Git/FETCH_HEAD 文件中, 其中每一行对应于远程服务器的一个分支,当前分支指向的FETCH_HEAD, 就是这个文件第一行对应的那个分支。

  • git fetch —all -p

 —all   Fetch all remotes.

-f --force

When git fetch is used with <rbranch>:<lbranch> refspec, it refuses to update the local branch <lbranch> unless the remote branch <rbranch> it fetches is a descendant of <lbranch>. This option overrides that check. 

-p --prune

Before fetching, remove any remote-tracking references that no longer exist on the remote. Tags are not subject to pruning if they are fetched only because of the default tag auto-following or due to a --tags option. However, if tags are fetched due to an explicit refspec (either on the command line or in the remote configuration, for example if the remote was cloned with the --mirror option), then they are also subject to pruning.

 

Repository

  • 检出(clone)仓库代码:git clone repository-url / git clone repository-url local-directoryname

 例如,clone jquery 仓库到本地: git clone git://github.com/jquery/jquery.git
    clone jquery 仓库到本地,并且重命名为 my-jquery :git clone git://github.com/jquery/jquery.git my-jquery
  • 查看远程仓库:git remote -v

  • 添加远程仓库:git remote add [name] [repository-url]

  • 删除远程仓库:git remote rm [name]

  • 修改远程仓库地址:git remote set-url origin new-repository-url

  • 拉取远程仓库: git pull <远程主机名> <远程分支名>:<本地分支名> git pull [remoteName] [localBranchName]

  • 推送远程仓库: git push <远程主机名> <本地分支名>:<远程分支名> git push [remoteName] [localBranchName] 例:git push -u orgin master 将当前分支推送到远端master分支( -u 指定origin为默认主机,后面就可以不加任何参数使用git push了)

  • 将本地 test 分支提交到远程 master 分支: git push origin test:master (把本地的某个分支 test 提交到远程仓库,并作为远程仓库的 master 分支) 提交本地 test 分支作为远程的 test 分支 :git push origin test:test

Checkout

checkout命令用于从历史提交(或者暂存区域)中拷贝文件到工作目录,也可用于切换分支。

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

Log

  • 查看日志:git log

  • 查看日志,并查看每次的修改内容:git log -p

  • 查看日志,并查看每次文件的简单修改状态:git log --stat

  • 一行显示日志:git log --pretty=oneline / git log --pretty='format:"%h - %an, %ar : %s'

  • 查看日志范围:

    • 查看最近10条日志:git log -10

    • 查看2周前:git log --until=2week 或者指定2周的明确日期,比如:git log --until=2015-08-12

    • 查看最近2周内:git log --since=2week 或者指定2周明确日志,比如:git log --since=2015-08-12

    • 只查看某个用户的提交:git log --committer=user.name /git log --author=user.name

    • 只查看提交msg中包含某个信息的历史,比如包含'测试'两个字的:git 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'

    • 更多用法:Viewing the History -- 《Pro Git2》

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

  • Who changed what and when in a file : git blame $file

  • 查看一次 commit 中修改了哪些文件: git show --pretty="" --name-only <sha1-of-commit> 或者 git diff-tree --no-commit-id --name-only -r <sha1-of-commit> 

Undo things

  • 上次提交 msg 错误/有未提交的文件应该同上一次一起提交,需要重新提交备注:git commit --amend -m 'new msg’

  • git commit –amend或git commit --amend -m "Fixes bug #42”,实际上git push之前重写了先前的提交信息。

  • 一次git add -A后,需要将某个文件撤回到工作区,即:某个文件不应该在本次commit中:git reset HEAD filename

  • 撤销某些文件的修改内容:git checkout -- filename 注意:一旦执行,所有的改动都没有了,谨慎!谨慎!谨慎!

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

Reset

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

本地做了一些提交(还没push),想撤销最近的三次提交git reset或git reset --hard

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

  • git reset --hard HEAD^ reset index and working directory ,

     以来所有的变更全部丢弃,并将 HEAD 指向
  • git reset --soft HEAD^ nothing changed to index and working directory ,仅仅将 HEAD 指向

     ,所有变更显示在 “changed to be committed”中
  • git reset --mixed HEAD^ default,reset index ,nothing to working directory 默认选项,工作区代码不改动,添加变更到index区 

Revert

git revert将根据给定SHA的相反值,创建一个新的提交。这是Git最安全、也是最简单的“撤销”场景,因为这样不会修改历史记录再git push下刚刚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

你已经提交了一些内容,并使用git reset –hard撤销了这些更改,但想还原这些修改:git reflog和git reset, 或者git checkout

Git reflog 可以查看所有分支的所有操作记录(包括(包括commit和reset的操作),包括已经被删除的commit记录,git log则不能察看已经删除了的commit记录

Diff

  • 查看工作区(working directory)和暂存区(staged)之间差异:git diff

  • 查看工作区(working directory)与当前仓库版本(repository)HEAD版本差异:git diff HEAD /  git diff FILENAME

  • 查看暂存区(staged)与当前仓库版本(repository)差异:git diff --cached / git diff --staged

  • git diff 展示过多时会自动加载分页器。常用热键如下:
    • q:退出
    • h:现实帮助
    • 空格,b:下翻一页,上翻一页
    • /pattern,?pattern:向上,向下查找
    • n,N:继续寻找

Merge

  • 解决冲突后/获取远程最新代码后合并代码:git merge branchname ,将 branchname 分支上面的代码合并到当前分支

  • 保留该存在版本合并log:git merge --no-ff branchname 参数 --no-ff 防止 fast-forward 的提交。 

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

  • master:master永远是线上代码,最稳定的分支,存放的是随时可供在生产环境中部署的代码,当开发活动告一段落,产生了一份新的可供部署的代码时,发布成功之后,代码才会由 aone2 提交到 master,master 分支上的代码会被更新。应用上 aone2 后禁掉所有人的 master的写权限

  • develop:保存当前最新开发成果的分支。通常这个分支上的代码也是可进行每日夜间发布的代码,只对开发负责人开放develop权限。

  • feature: 功能特性分支,每个功能特性一个 feature/ 分支,开发完成自测通过后合并入 develop 分支。可以从 master 或者develop 中拉出来。

  • hotfix: 紧急bug分支修复分支。修复上线后,可以直接合并入master。 

Branch 命令

  • 查看分支:git branch 、git branch -vgit branch -vv (查看当前分支 tracking 哪个远端分支)、git branch --mergedgit branch --no-merged

  • 创建分支:git branch branchname

    • 例: 基于 master 分支新建 dev 分支 : git branch dev

  • 基于之前的某个 Commit 新开分支: git branch branchname <sha1-of-commit>

    • 例: 基于上线的的提交 a207a38d634cc10441636bc4359cd8a18c502dea 创建 hotfix 分支 : git branch hotfix a207a38

    • 例: 基于 remoteBranch、localBranch、commitId、tag 创建分支均可以 git checkout -b newbranch localBranch/remoteBranch/commitId/tag

  • 切换分支: git checkout branchname

    • 例: 由分支 master 切换到 dev 分支:git checkout dev

  • 创建新分支并切换到下面:git checkout -b branchname 或者 git branch branchname && git checkout branchname

  • 查看分支代码不同:git diff branchname 比较 branchname 分支与当前分支的差异点

  • 合并分支:git merge branchname 将 branchname 分支代码合并到当前分支

  • 删除分支:git branch -d branchname 强制删除未合并过的分支:git branch -D branchname

  • 查看远程分支:git branch -r 或 git branch -r -v

  • 获取远程分支到本地:git checkout -b local-branchname origin/remote-branchname

  • 推送本地分支到远程:git push origin remote-branchname 或 git push origin local-branchname:remote-branchname

    • 将本地 dev 代码推送到远程 dev 分支: git push (-u) origin dev 或 git push origin dev:dev

    • 将本地 dev 分支代码推送到远程 master 分支: git push origin dev:master

    • git push --force origin如果远程主机的版本比本地版本更新,推送时Git会报错,要求先在本地做git pull合并差异,然后再推送到远程主机。这时,如果你一定要推送,可以使用–force选项
  • 删除远程分支:git push origin :remote-branchname 或 git push origin --delete remote-branchname

  • 手动跟踪分支,master分支追踪origin/next分支 (there is no tracking information for the current branch): git branch --set-upstream master origin/next  /  git branch --set-upstream-to=origin/master master

  • TrackingBranch,可以通过 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

  • git branch -r 列出远程分支
  • git branch -a 列出本地分支和远程分支
  • git branch 不带参数:列出本地已经存在的分支
  • git branch -m | -M oldbranch newbranch 重命名分支,如果newbranch名字分支已经存在,则需要使用-M强制重命名,否则,使用-m进行重命名。等同于 git checkout -b twitter-experiment feature132 && git branch -d feature132
  • git branch -d | -D branchname 删除branchname分支
  • git branch -d -r branchname 删除远程branchname分支
Tag
  • 查看 tag:git tag

  • 查找指定 tag,比如查找 V1.0.* :git tag -l 'V1.0.*' 会列出匹配到的,比如 V1.0.1,V1.0.1.1,V1.0.2 等

  • 创建轻量级 tag(lightweight tags):git tag tag-name ,例如: git tag v1.0

  • 创建 tag(annotated tags):git tag -a tag-name -v 'msg' ,例如:git tag -a v1.0.0 -m '1.0.0版本上线完毕打tag'

    • annotated tags VS lightweight tags 可以通过命令真实查看下:git show v1.0 / git show v1.0.0

    • “A lightweight tag is very much like a branch that doesn’t change – it’s just a pointer to a specific commit. Annotated tags, however, are stored as full objects in the Git database. They’re checksummed; contain the tagger name, e-mail, and date; have a tagging message; and can be signed and verified with GNU Privacy Guard (GPG). ”

  • 查看指定 tag 信息:git show tag-name

  • 基于历史某次提交(commit)创建 tag :git tag -a tagname <sha1-of-commit>

    • 例:基于上线时的提交 a207a38d634cc10441636bc4359cd8a18c502dea 创建tag:git tag -a v1.0.0 a207a38

  • 删除 tag :git tag -d tagname

  • 拉取远程 tag 到本地:git pull remotename --tags 例如:git pull origin --tags

  • 推送 tag 到远程服务器:git push remotename tagname 例如:git push origin v1.0.0

  • 将本地所有 tag 推送到远程:git push remotename --tags 例如:git push origin --tags

  • 删除远程 tag :git push origin :tagname 或者 git push origin --delete tag name

Stash
切换分支前git stash将保存工作进度,切换回该分支后git stash pop恢复之前保存的工作进度
 
 
在本地构建项目环境
mkdir newproject_test   //新建newproject_test文件夹
cd newproject_test      //定位到newproject_test
git init     //初始化git
touch README  //shell命令,创建readme文件
git add README    //将readme文件添加到索引库
git commit -m 'first commit'   //提交的描述信息
git remote add origin git@gitlab.alibaba-inc.com:trip/test.git //在url创建名字为origin的仓库
git push -u origin master//提交本地分支作为master分支

检出、在本定审查和合并

1、获取并检出此合并请求的分支

git fetch origin
git checkout -b release origin/release

2、本地审查变更

3、合并分支并修复出现的冲突

git checkout localbranch
git merge --no-ff release

4、推送到gitlab

git push origin localbranch

npm常用命令

  • npm install xxx 安装模块
  • npm install gulp-cli@1.2.2 安装某特定版本
  • npm install 某个模块时,就会出现一个 loading 状态的指针,然后就长时间在那里转啊转,完全不知道干了什么。如果实在长时间不动可以通过 npm install --verbose 来看看日志。
  • npm install xxx -g 将模块安装到全局环境中
  • npm ls 查看安装的模块及依赖
  • npm ls -g 查看全局安装的模块及依赖
  • npm uninstall xxx  (-g) 卸载模块
  • npm cache clean 清理缓存
 
 
参考链接:
原文地址:https://www.cnblogs.com/chenlogin/p/6229434.html