简体   繁体   English

如何在git中检测本地repo和远程repo之间的差异?

[英]How to detect differences between local repo and remote repo in git?

Say I did a git clone from a url for a repository. 说我从存储库的url做了一个git clone。 I made some changes to a file, did a git commit. 我对文件做了一些更改,做了一个git提交。

When I do a git pull, I see that it says "Already up-to-date" 当我做一个git pull,我看到它说“已经是最新的”

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. 说我做了上面的改变到我的本地回购,但是没有提交2天,但是在2天之前,其他人已经改变了远程回购。 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 . 我的第一个建议是不要git pull Do a git fetch followed by a git merge . 执行git fetch后跟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 merge 1 . 因此,没有什么可以获取或合并1

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. git commit :在您自己的分支上提交您的更改,与远程存储库中发生的事情完全无关。
  2. git fetch origin : get the contents of the remote repository ( origin ), but keep them under origin/branch branches. git fetch origin :获取远程存储库( origin )的内容,但将它们保存在origin/branch分支下。 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. git merge origin/master :合并origin/master哪个是master远程仓库的分支origin与当前分支(你刚才取)。
  4. git push origin : push back the commit and the merge to the remote repository git push origin :将提交和合并推回到远程存储库

To answer your second question: 回答你的第二个问题:

  1. git fetch origin : update origin/branch branches. git fetch origin :更新origin/branch分支。
  2. git diff origin/master : get the difference between your current branch and the branch origin/master . git diff origin/master :获取当前分支与分支origin/master之间的差异。

1 Suppose this is what the commits in your repository initially look like, on branch master : 1假设这是您的存储库中的提交最初的样子,在分支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 : 现在你说你在本地分支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. 假设远程origin其他人没有活动,则没有什么新东西可以获取。 So git fetch origin master tells you there is nothing new. 所以git fetch origin master告诉你没什么新东西。

  2. If you do git merge origin/master , again, there is nothing to merge. 如果你再次执行git merge origin/master ,则无需合并。 origin/master is a prefix of master . origin/master是的前缀 master In other words, master already contains all the commits that origin/master has, so there is nothing new to merge. 换句话说, master已经包含origin/master拥有的所有提交,因此没有什么新东西可以合并。

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. 如果您使用了fetchmerge而不是pull ,则可以很容易地理解双命令( pull )的哪一部分是导致意外(在您看来)行为的部分。

Of course, after a git push origin master , you will get: 当然,在git push origin master ,你会得到:

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

When you pull, it pulls in the history from the server and automatically tries to merge anything you have changed, with things that other people have changed. 拉动时,它会从服务器中提取历史记录,并自动尝试将您已更改的内容与其他人更改的内容合并。 If it can do it automatically, then it will succeed and you won't see anything, and if it fails then you will be asked to fix any conflicts. 如果它可以自动执行,那么它将成功,您将看不到任何内容,如果它失败,那么您将被要求修复任何冲突。 You will never be able to silently override changes that other people have made. 您永远无法以静默方式覆盖其他人所做的更改。

If you do want to see if someone has changed something, you can run this git fetch and then git status . 如果你想看看是否有人改变了某些东西,你可以运行这个git fetch ,然后运行git status Status will print out what files have changes locally, but it will also say something like "Your branch is 2 commits ahead of origin/master", meaning you have 2 commits that have not been pushed to the server. 状态将打印出本地更改了哪些文件,但它也会说“您的分支是在原始/主服务器之前提交2个提交”,这意味着您有2个未提交到服务器的提交。 If someone else has pushed to the server too, then it will say something like "Your local branch and the origin/master have diverged. This means both you and someone else have committed to master, and when you pull it will try to merge them, as described above. 如果其他人也推送到服务器,那么它会说“你的本地分支和原点/主人有分歧。这意味着你和其他人已经承诺掌握,当你拉它时会尝试合并它们, 如上所述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM