git使用笔记-基础篇

git使用手册:https://git-scm.com/book/zh/v1/

一、分支

  1、查看所有本地分支

    git branch

  2、查看所有本地分支和远程分支

     git branch -a

  3、查看本地分支和远程分支的对应关系

    git branch -vv

  4、查看远程分支对应远程库路径

         git remote -v

  5、创建/删除本地分支

    git branch local-name 以当前分支为基础创建名为local-name的本地分支

    git checkout -b local-name 以当前分支为基础创建本地分支local-name并切换到该分支

    git branch -d 如果有未合并的提交,不会删除

    git branch -D 强制删除,如果有未合并的提交也删除

  6、设置本地分支与远程分支的追踪关系

       git branch --set-upstream-to=远程库名/分支名

  7、以远程库为基础创建本地分支

    git checkout -b localbranch remotebranch 创建本地分支,以remotebranch为开始。同时也建立了本地分支和远程分支的关系。

    该方法与5相比更加方便,因为5是以当前分支为基础创建新分支,而远程分支是其他分支,则容易冲突。

    git checkout --track origin/master 创建名为master的本地分支,追踪origin/master。不能指定本地分支名称。

  8、增加远程库

    git remote add  local-name url 添加远程库url,并且在本地用local-name来指代它

  9、将本地分支提交到远程仓库作为一个新的远程分支

    git push [远程名] [本地分支]:[远程分支] 把本地分支推送到远程库,名为“远程分支”
    git push origin localbranch 把localbranch推送到origin远程库,在远程库里面,该分支名为 localbranch

    git push origin :remotebranch删除远程分支remotebranch

     10、切换分支

            git checkout local-branch

二、日志

  1、查看当前分支的log

     git log

  2、查看指定分支的log

    git log banch 

  3、查看提交内容差异

    git log -p -2 

    -p 选项展开显示每次提交的内容差异,用 -2 则仅显示最近的两次更新

  4、查看提交的简单统计

    git log --stat 

     --stat,仅显示简要的增改行数统计

  5、其他用法

    git log --oneline 把提交信息在一行显示,通常在提交很多时方便

    git log --pretty= 可以按照指定格式显示日志信息

    git log --graph= 显示 ASCII 图形表示的分支合并历史

    gitk命令使用gitk图形化工具显示log(linux和windows下都可以用)

 三、标签

  1、git分支和标签的区别:标签主要适用于发布,更像一个里程碑式;分支主要是向前进行。

  2、标签是打在一次提交上面,与commit-id挂钩

  3、标签有两种:轻量级的(lightweight)和含附注的(annotated)。轻量级标签就像是个不会变化的分支,实际上它就是个指向特定提交对象的引用。

    而含附注标签,实际上是存储在仓库中的一个独立对象,它有自身的校验和信息,包含着标签的名字,电子邮件地址和日期,以及标签说明,

    标签本身也允许使用 GNU Privacy Guard (GPG) 来签署或验证。一般我们都建议使用含附注型的标签,以便保留相关信息;当然,如果只是临

    时性加注标签,或者不需要旁注额外信息,用轻量级标签也没问题。

  4、创建标签

    git tag v1.4 在当前最新提交上面创建tag

    git tag commt-id 在指定commit-id上创建tag

  5、查看所有标签

    git tag

  6、筛选标签

$ git tag -l 'v1.4.2.*'
v1.4.2.1
v1.4.2.2
v1.4.2.3
v1.4.2.4

  7、查看对应标签版本信息

    git show v1.4.2.1

  8、推送(分享)标签

    git push origin v1.4.2.1 向远程库origin推送标签 v1.4.2.1

    git push origin --tags 向origin库推送所有本地新增标签

  9、以标签创建分支
    git checkout -b localbranch tag 创建本地分支,以tag为开始。这样就建立了本地分支和tag的关系。

四、分支拉取、推送、合并和变基

  1、拉取

    git pull [remote-name] [branch-name]

    git pull 从默认远程分支拉取;默认远程分支的意思是本地分支追踪的远程分支

    git pull origin master 从指定的origin库和master分支拉取;如果有冲突需要自己解决;

  2、推送

    git push 向默认分支推送

    git push remote-name branch-name向指代远程分支推送

  3、合并分支

    git merge branch 把branch分支合并到当前分支,冲突需要自己解决后提交

   4、变基

    git rebase master 以master分支为基底分支把当前分支的提交重新演绎一遍,从而改变当前分支的提交历史,

    使它成为master的直接下游,也使得分支结构关系更加清晰(从树形变为线性)

  变基前:  

  

  变基后:

  

  这时可以切换到master分支,来一次merge操作,git merge experiment:

   

  然后就可以把experiment分支删除了。这样master的提交历史会比直接merge experiment更清晰。

  git rebase还可以重写提交历史,参考https://git-scm.com/book/zh/v1/Git-%E5%B7%A5%E5%85%B7-%E9%87%8D%E5%86%99%E5%8E%86%E5%8F%B2

  5、抓取数据 

    git fetch [remote-name] [branch-name]从远程分支抓取本地仓库没有的数据,但是不会合并到本地分支,只是更新了本地远程仓库的索引。

    如果需要可以自己merge到本地分支。git pull即是git fetch 和 git merge 两步操作。

    FETCH-HEAD:

      1)如果没有显式的指定远程分支, 则远程分支的master将作为默认的FETCH_HEAD

        即git fetch [origin],FETCH_HEAD=origin/master

      2)如果指定了远程分支, 就将这个远程分支作为FETCH_HEAD

        即git fetch origin b1时, FETCH_HEAD=origin/b1

    比如 git fetch origin 那么从远程库origin抓取所有分支数据;

    1)git merge FETCH_HEAD(origin/master) 则把抓取到的origin/master分支数据合并到本地分支;

    2)git log origin/master查看拉取到的该分支log;

    3)git checkout -b newB FETCH_HEAD以origin/master创建新分支newB

参考:

FETCH_HEAD is a short-lived ref, to keep track of what has just been fetched from the remote repository. git pull first invokes git fetch, in normal cases fetching a branch from the remote;

FETCH_HEAD points to the tip of this branch (it stores the SHA1 of the commit, just as branches do). git pull then invokes git merge, merging FETCH_HEAD into the current branch.

The result is exactly what you'd expect: the commit at the tip of the appropriate remote branch is merged into the commit at the tip of your current branch.

This is a bit like doing git fetch without arguments (or git remote update), updating all your remote branches, then running git merge origin/<branch>, but using FETCH_HEAD internally

instead to refer to whatever single ref was fetched, instead of needing to name things.

原文地址:https://www.cnblogs.com/shihuvini/p/9258441.html