zoukankan      html  css  js  c++  java
  • git 的 origin 的含义

    我们从progit 一书中可以看到:

    远程仓库名字 “origin” 与分支名字 “master” 一样,在 Git 中并没有任何特别的含义一样。 同时“master”是当你运行git init时默认的起始分支名字,原因仅仅是它的广泛使用,“origin” 是当你运行 git clone 时默认的远程仓库名字。 如果你运行 git clone -o booyah,那么你默认的远程分支名字将会是 booyah/master。

    我们使用 git remote -v 或者 查看.git/config 可以看到 origin 的含义。

    当我们通过使用 git remote -v 命令的时候我们可以看到如下:

    origin https://github.com/yaowenxu/yaowenxu.github.io.git (fetch)

    origin https://github.com/yaowenxu/yaowenxu.github.io.git (push)

    origin 这两种形式。

    但是

    origin 并不是指得是远程的仓库,而是指得是远程仓库在本地的一个指针(这个指针有可能过时的)。当我们使用使用merge 的时候,我们进行合并的时候只是上一次fetch 从远程拿到的版本。不是远程仓库的最新版本。

    比如命令:

    git merge origin master

    指的就是将本地的远端分支与本地的master 分支进行合并。

    这里 git merge origin master 可以和 git merge origin/master 进行对比

    或者可以对比一下 git pull origin master 对比一下,你是不是会有新得体会。

    我们或许可以再看一下 progit 上面对 远程仓库的最新的解释:

    远程分支(remote branch)是对远程仓库中的分支的索引。它们是一些无法移动的本地分支;只有在 Git 进行网络交互时才会更新。远程分支就像是书签,提醒着你上次连接远程仓库时上面各分支的位置。

    其实:

    在clone完成之后,Git 会自动为你将此远程仓库命名为origin(origin只相当于一个别名,运行git remote –v或者查看.git/config可以看到origin的含义),并下载其中所有的数据,建立一个指向它的master 分支的指针,我们用(远程仓库名)/(分支名) 这样的形式表示远程分支,所以origin/master指向的是一个remote branch(从那个branch我们clone数据到本地),但你无法在本地更改其数据

    同时,Git 会建立一个属于你自己的本地master 分支,它指向的是你刚刚从remote server传到你本地的副本。随着你不断的改动文件,git add, git commit,master的指向会自动移动,你也可以通过merge(fast forward)来移动master的指向。

    我们通过 git branch -a 可以看到所有分支:

    * master

      remotes/origin/HEAD -> origin/master

      remotes/origin/dev

      remotes/origin/master

    可以发现:master 就是本地分支, origin/master 指得就是远程分支。origin/master 指得是最近一次fetch 拿下来的最新版本。

    上面这个图就可以很好得讲解,我们git 所合并得是commit. git merge origin master 指得是将本地库所关联的远程仓库对应的commit id 来和本地master进行合并。

    而: 本地远程仓库记录文件是:.git efs emotesoriginmaster 本地仓库记录的文件是:.git efsheadsmaster

    根据 sean-zou 的博客我们可以知道:

    1、git fetch只会将本地库所关联的远程库的commit id更新至最新

    2、git pull会将本地库更新至远程库的最新状态

    所以 git fetch 和 git merge 共同效果并不完全等同于 git pull

    或者用英文来定义一下 fetch 和 pull 更为明白:

    • git fetch is the command that says “bring my local copy of the remote repository up to date.”
    • git pull says “bring the changes in the remote repository where I keep my own code.”

    所以可以这么 理解orgin 或者 orgin/master 这个只是远程仓库在本地仓库的一个指针。我们可以使用 git fetch 命令来进行更新。

    所以在进行merge 的时候 不要忘了先进行git fetch 进行更新到最新的远程仓库。

    我们借用OoBa的一个图,他详细得对比了pull 和 fetch,merge 。当然更为详细的内容可以参考《ProGit》一书。

    参考文献:

    1. http://www.zhanglian2010.cn/2014/07/git-pull-vs-fetch-and-merge/

    2. https://git-scm.com/book/zh/v1/Git-分支

    3. https://blog.csdn.net/a19881029/article/details/42245955

    4. 《ProGit》- v2.1.8

  • 相关阅读:
    NiosII软处理器快速入门- 10分钟学会NiosII(2)
    FFT算法的一种FPGA实现
    32个最热CPLDFPGA论坛
    NiosII软处理器快速入门- 10分钟学会NiosII(3)
    基于FPGA/CPLD设计与实现UART
    NiosII软处理器快速入门- 10分钟学会NiosII(1)
    LCD 的分类和显示原理
    iis6.0重写成html设置
    p标签之间的行距问题
    ie6 png图片透明方法
  • 原文地址:https://www.cnblogs.com/xuyaowen/p/git-origin.html
Copyright © 2011-2022 走看看