如何在git中检测本地回购和远程回购之间的差异? [英] How to detect differences between local repo and remote repo in git?

查看:155
本文介绍了如何在git中检测本地回购和远程回购之间的差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我从一个仓库的URL做了一个git克隆。我对文件做了一些修改,做了一次git commit。



当我做一个git pull的时候,我看到它说已经是最新的 p>

它不应该显示说我不是最新的东西吗?



我的问题是:


  1. 到我当地的回购协议,但不要承诺2天,但在2天之前,其他人已经改变了远程回购协议。我必须采取哪些措施来确保我不覆盖远程回购中的更改,或者至少能够在提交之前提交最新更改?


  2. 有没有一些方式来区分我的本地回购和远程回购之间有什么区别? (如果我只想回忆一下我以前的话)?

  3. >我的第一个建议是不是 git pull 。做一个 git fetch ,后跟一个 git merge



    回答你的第零个问题:事实上,你是最新的。您拥有远程存储库具有的所有提交。所以,没有什么可以提取或合并的。

    回答你的第一个问题:


    1. git commit :在您自己的分支上提交您的更改,与远程存储库中发生的情况完全无关。

    2. git fetch origin :获取远程仓库的内容( origin ),但保留他们在 origin / branch 分支下。 / code>,它是远程仓库 origin (您刚刚获取的)的 master 分支与您的当前分支。

    3. git push origin :将提交和合并推送回远程存储库


      回答第二个问题:


      1. git fetch origin :update origin / branch branches

      2. git diff origin / master :获取当前分支和分支之间的区别 origin / master






      1 假设这是您的存储库中的提交最初看起来像分支 master

        A  - > B  - > C  - > D  - > E 
      |
      | \- master
      |
      \- origin / master

      这是在克隆存储库之后。现在你说你已经在你的本地分支上做了一个新的提交 master

        A  - > B  - > C  - > D  - > E  - > F 
      | |
      | \ - master
      |
      \ origin / master

      所以有两件事要注意。 p>


      1. 假设远程 origin 中的其他人没有任何活动,新获取。所以 git fetch origin master 告诉你没有什么新东西。

      2. c> git merge origin / master
        ,再次,没有什么可以合并的。 origin / master master 前缀。换句话说, master 已经包含了 origin / master 所有的提交,所以没有什么新的合并。 c> merge 而不是 pull ,您可以轻松理解双命令的哪一部分( pull $ b

        当然,在 git push origin master 之后, code>,你将得到:

          A  - > B  - > C  - > D  - > E  - > F 
        |
        | \- master
        |
        \- origin / master


        Say I did a git clone from a url for a repository. I made some changes to a file, did a git commit.

        When I do a git pull, I see that it says "Already up-to-date"

        Shouldn't it show something that says I am not up to date?

        My question is:

        1. say I did the change above to my local repo, but do not commit for 2 days, but before the 2 days are up, someone else had made a change to the remote repo. What steps must I do to ensure I am not overriding changes in the remote repo or at least be able to pull the latest changes before committing?

        2. Is there some way to diff between my local repo and the remote repo to check what differences there are? (in case I just want to recall what I had before?)

        解决方案

        My first advice is to not git pull. Do a git fetch followed by a git merge.

        To answer your zero'th question: In fact, you are up-to-date. You have all the commits that the remote repository has. So, there is nothing left to fetch or merge1.

        To answer your first question:

        1. git commit: commit your changes on your own branch, totally unrelated to what's going on in remote repositories.
        2. git fetch origin: get the contents of the remote repository (origin), but keep them under origin/branch branches. Your own code is unaffected at this point.
        3. git merge origin/master: merge origin/master which is the master branch of the remote repository origin (which you fetched just now) with your current branch.
        4. git push origin: push back the commit and the merge to the remote repository

        To answer your second question:

        1. git fetch origin: update origin/branch branches.
        2. git diff origin/master: get the difference between your current branch and the branch origin/master.


        1 Suppose this is what the commits in your repository initially look like, on branch master:

        A -> B -> C -> D -> E
                            |
                            |\- master
                            |
                             \- origin/master
        

        This is right after you cloned the repository. Now you say you have made a new commit on your local branch master:

        A -> B -> C -> D -> E -> F
                            |    |
                            |     \- master
                            |
                             \- origin/master
        

        So there are two things to observe here.

        1. Assuming no activity by somebody else in the remote origin, there is nothing new to fetch. So git fetch origin master tells you there is nothing new.

        2. If you do git merge origin/master, again, there is nothing to merge. origin/master is a prefix of master. In other words, master already contains all the commits that origin/master has, so there is nothing new to merge.

        If you had used fetch and merge instead of pull, you could easily understand which part of the double-command (pull) is the one that results in unexpected (in your opinion) behavior.

        Of course, after a git push origin master, you will get:

        A -> B -> C -> D -> E -> F
                                 |
                                 |\- master
                                 |
                                  \- origin/master
        

        这篇关于如何在git中检测本地回购和远程回购之间的差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆