git 如何检测git中本地repo和远程repo之间的差异?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13364996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to detect differences between local repo and remote repo in git?
提问by Rolando
Say I did a git clone from a url for a repository. I made some changes to a file, did a git commit.
假设我从存储库的 url 进行了 git clone。我对文件做了一些更改,做了一个 git commit。
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:
我的问题是:
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?
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?)
说我对我的本地 repo 做了上面的更改,但没有提交 2 天,但是在 2 天结束之前,其他人对远程 repo 进行了更改。我必须采取哪些步骤来确保我不会覆盖远程存储库中的更改,或者至少能够在提交之前提取最新的更改?
有什么方法可以区分我的本地 repo 和远程 repo 以检查有什么区别?(以防万一我只想回忆我以前拥有的东西?)
回答by Shahbaz
My first advice is to not git pull
. Do a git fetch
followed by a git merge
.
我的第一个建议是不要git pull
。执行 agit fetch
后跟 a git merge
。
To answer your zero'th question: In fact, you areup-to-date. You have all the commits that the remote repository has. So, there is nothing left to fetch or merge1.
回答您的第零个问题:事实上,您是最新的。您拥有远程存储库的所有提交。因此,没有什么可以获取或合并1 了。
To answer your first question:
回答你的第一个问题:
git commit
: commit your changes on your own branch, totally unrelated to what's going on in remote repositories.git fetch origin
: get the contents of the remote repository (origin
), but keep them underorigin/branch
branches. Your own code is unaffected at this point.git merge origin/master
: mergeorigin/master
which is themaster
branch of the remote repositoryorigin
(which you fetched just now) with your current branch.git push origin
: push back the commit and the merge to the remote repository
git commit
:在您自己的分支上提交您的更改,与远程存储库中发生的事情完全无关。git fetch origin
: 获取远程存储库 (origin
)的内容,但将它们保存在origin/branch
分支下。此时您自己的代码不受影响。git merge origin/master
:合并origin/master
这是master
远程仓库的分支origin
(你现在只是获取)与当前分支。git push origin
: 将提交和合并推回远程存储库
To answer your second question:
回答你的第二个问题:
git fetch origin
: updateorigin/branch
branches.git diff origin/master
: get the difference between your current branch and the branchorigin/master
.
git fetch origin
: 更新origin/branch
分支。git diff origin/master
: 获取当前分支和分支之间的差异origin/master
。
1Suppose 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.
所以这里有两件事需要注意。
Assuming no activity by somebody else in the remote
origin
, there is nothing new to fetch. Sogit fetch origin master
tells you there is nothing new.If you do
git merge origin/master
, again, there is nothing to merge.origin/master
is a prefixofmaster
. In other words,master
already contains all the commits thatorigin/master
has, so there is nothing new to merge.
假设远程中的其他人没有活动
origin
,就没有什么新东西可以获取。所以git fetch origin master
告诉你没有什么新鲜事。如果你这样做
git merge origin/master
,再一次,没有什么可以合并的。origin/master
是一种前缀的master
。换句话说,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.
如果您使用fetch
andmerge
而不是pull
,您可以轻松理解双命令 ( pull
) 的哪一部分导致意外(在您看来)行为。
Of course, after a git push origin master
, you will get:
当然,在 a 之后git push origin master
,你会得到:
A -> B -> C -> D -> E -> F
|
|\- master
|
\- origin/master
回答by loganfsmyth
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
. 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. 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.
如果您确实想查看是否有人更改了某些内容,则可以运行此命令git fetch
,然后运行git status
. Status 会打印出哪些文件在本地发生了变化,但它也会说“你的分支比 origin/master 有 2 次提交”,这意味着你有 2 次提交尚未推送到服务器。如果其他人也推送到服务器,那么它会说“你的本地分支和源/主节点已经分歧。这意味着你和其他人都已经承诺主节点,当你拉它时会尝试合并它们, 如上所述。