资讯详情

彻底理解git 本地分支与远程分支,以及git pull与git fetch的区别, 以及git status输出的“Your branch is ...

git 本地分支:

当clone一个远程repo之后默认会新建一个master或main本地分支。

比如:

$ git branch -a * main   remotes/origin/HEAD -> origin/main   remotes/origin/develop   remotes/origin/main 

可见当地默认只建了一个main其余的是远程分支。

在远程分支的基础上checkout例如,执行命令:

$ git checkout develop Branch 'develop' set up to track remote branch 'develop' from 'origin'. Switched to a new branch 'develop'

这是本地分支develop被创造出来。

Git远程分支:

以上输出的名称为remotes/origin/开始分支。虽然叫远程分支,但对应于这些分支commits它也在我们当地。严格地说,它应该被称为当地的远程分支(有点尴尬)。以下远程分支是指该分支,而不是远程仓库上的分支。

同时,我们也可以远程分支checkout出来,只是这个远程分支只读,也就是我们在‘’detached HEAD'状态。

$ git checkout remotes/origin/develop Note: switching to 'remotes/origin/develop'.  You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by switching back to a branch.  If you want to create a new branch to retain commits you create, you may do so (now or later) by using -c with the switch command. Example:    git switch -c <new-branch-name>  Or undo this operation with:    git switch -  Turn off this advice by setting config variable advice.detachedHead to false  HEAD is now at 098c616 Create file-in-develop

我们想在本地修改,提交,然后同步到远程repo,必须在当地分支机构工作,而不是远程分支机构。

我们在执行git fetch操作时,远程仓库将进行操作commits,files,ref等待信息下载到本地并保存remotes/origin/在最初的分支中(这不准确,严格来说是让remotes/origin/最初的分支指向这些新的commits),底层的原理如下图所示:

我们所有的commits类似于链表,所有分支commits无论是本地分支(上图中),它们都相互连接main指向的)还是远程分支(上图中名字以Orgin/开始的分支),他们都指向这个commits链条的某一点(是的,分支名称是指针,指向某一点)。我们执行git fetch在命令时,将是远程的commits下载,让名字remotes/origin最初的分支指向他们。但是这些remote的commits,我们不能直接在上面工作(修改操作)。我们可以先把这些包括新的commits的远程分支checkout然后在此基础上建立一个新的分支,然后进行修改。

或者我们可以直接分支这些远程分支merge例如:

$ git fetch origin develop remote: Enumerating objects: 3, done. remote: Counting objects: 100% (3/3), done. remote: Compressing objects: 100% (2/2), done. remote: Total 2 (delta 1), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (2/2), 609 bytes | 304.00 KiB/s, done. From https://github.com/YiyiSmile/test06087  * branch            develop    -> FETCH_HEAD    90b6713..311a291  develop    -> origin/develop $  $  $ git status On branch develop Your branch is behind 'origin/develop' by 1 commit, and can be fast-forwarded.   (use "git pull" to update your local branch)  nothing to commit, working tree clean $ ls README.md a  file-in-develop file2  file3  hello  world $ git merge origin/develop Updating 90b6713..311a291 Fast-forward  file4 | 1    1 file changed, 1 insertion( )  create mode 100644 file4 $ ls README.md file-in-develop file3  hello a  file2  file4  world

我们先用git fetch将远程repo上的develop新的分支提交(file4)下载到本地origin/develop分支,此时本地develop工作区完全不受影响,stage区域和提交区域没有变化。然后执行merge命令,将origin/develop分支 merge到本地分支develop。

除了用merge命令,也可以简单使用git pull完成快进(fast forward),将origin/develop里有的commits 本地合并develop分支。

git pull操作:

git pull与git fetch具体差异可参考文件:

Git Fetch | Atlassian Git Tutorial

git fetch是将远程repo数据下载到本地,但对本地仓库完全没有影响。而git pull将远程仓库数据下载到本地并自动合并,更新工作区和stage区(索引区)。

git status输出理解:

例如,我们在远程仓库develop闻分支新建文件file然后执行命令git status,发现没有提示:Your branch is behind 'origin/develop' by 1 commit

$ git status On branch develop Your branch is up to date with 'origin/develop'.  nothing to commit, working tree clean $ ls README.md file-in-develop file3  hello a  file2  file4  world

这时,我们可以通过两种方式让消息出现:

方法一:可以在其他分支下,比如main分支下,执行git pull,后面没有参数。此时程仓库外main下载分支数据,拉下所有其他分支数据,但只有当前的本地分支main自动与远程同步,完成merge、工作区、stage区的同步。还有其他分支,比如这里develop他们不会同步分支。但对应的远程分支(orgin/develop)但同步已经完成。

方法二:执行git fetch命令。

当orgin/develop指向远程下载的新提交和本地提交develop还是指向老的提交,这时运行git status当命令时,你会看到Your branch is behind 'origin/develop' by 1 commit”消息。

$ git fetch remote: Enumerating objects: 4, done. remote: Counting objects: 100% (4/4), done. remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), 646 bytes | 323.00 KiB/s, done. From https://github.com/YiyiSmile/test06087    311a291..634ba9e  develop    -> origin/develop $ git status On branch develop Your branch is behind 'origin/develop' by 1 commit,and can be fast-forwarded.
  (use "git pull" to update your local branch)

nothing to commit, working tree clean

 总结:

 也就是说并不是只要远程仓库有更新了,git status就会报告你的本地分支落后于远程分支。而是只有当远程仓库的数据同步到本地的名字以origin/开始的分支之后,git status才会比较本地与远程分支的差异。这个比较过程都是在本地完成的,不存在网络通信过程。

标签: t311a系列压力变送器

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台