Git:如何检查本地存储库是否是最新的?
我想知道我的本地存储库是否是最新的(如果不是,理想情况下,我希望看到更改)。
我如何在不执行 git fetch 或 git pull 的情况下检查这一点?
I would like to know if my local repo is up to date (and if not, ideally, I would like to see the changes).
How could I check this without doing git fetch
or git pull
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
您必须运行 git fetch 才能将本地存储库与远程服务器上的文件进行比较。
此命令仅更新您的远程跟踪分支,不会影响您的工作树,直到您调用 git merge 或 git pull 。
要在获取后查看本地分支和远程跟踪分支之间的差异,可以使用 git diff 或 gitcherry 如此处所述。
You must run
git fetch
before you can compare your local repository against the files on your remote server.This command only updates your remote tracking branches and will not affect your worktree until you call
git merge
orgit pull
.To see the difference between your local branch and your remote tracking branch once you've fetched you can use git diff or git cherry as explained here.
另一种选择是使用查看远程分支的状态
git show-branch remote/branch
将其用作比较,您可以看到git show-branch *branch
来查看所有遥控器以及存储库中的分支!查看此答案以了解更多信息 https://stackoverflow.com/a/3278427/2711378Another alternative is to view the status of the remote branch using
git show-branch remote/branch
to use it as a comparison you could seegit show-branch *branch
to see the branch in all remotes as well as your repository! check out this answer for more https://stackoverflow.com/a/3278427/2711378主要推送到主要(最新)
两者都是最新的main 推送到 main(可快进)
远程可以用本地更新main 推送到 main(本地已过时)
本地可以通过远程更新main pushes to main (up-to-date)
Both are up to datemain pushes to main (fast-forwardable)
Remote can be updated with Localmain pushes to main (local out of date)
Local can be update with Remote如果您使用,
您将获得有关它是否是最新的反馈。所以基本上,您只需将“详细”选项添加到之前给出的答案中即可。
If you use
you'll get feedback about whether it is up-to-date. So basically, you just need to add the "verbose" option to the answer given before.
你会看到类似的结果
更新远程更改的提交
you'll see result like
to update to remote changes
如果不使用 git fetch 或 git pull ,这是不可能的。如果不去远程存储库查看“最新”的含义,如何知道存储库是否“最新”?
This is impossible without using
git fetch
orgit pull
. How can you know whether or not the repository is "up-to-date" without going to the remote repository to see what "up-to-date" even means?要在不使用 git fetch 的情况下完成此任务,您可以使用 git rev-list 命令来比较本地分支上最近提交的哈希值和最近提交的哈希值在相应的远程分支上。下面是执行此操作的 Bash 脚本:
该脚本首先使用 git rev-parse --abbrev-ref HEAD 获取当前分支的名称。然后,它使用 git rev-list --max-count=1 来获取本地和远程分支上最近提交的哈希值。最后,它使用 git rev-list --count 来计算两个哈希值之间的提交次数,即本地分支落后于远程分支的提交次数。
请注意,此脚本假定相应的远程分支名为
origin/branch_name
。如果您的遥控器名称不同,您需要相应地调整脚本。To accomplish this task without using
git fetch
, you can use thegit rev-list
command to compare the hashes of the most recent commit on your local branch and the most recent commit on the corresponding remote branch. Here's a Bash script that does this:This script first gets the name of the current branch using
git rev-parse --abbrev-ref HEAD
. It then usesgit rev-list --max-count=1
to get the hash of the most recent commit on both the local and remote branches. Finally, it usesgit rev-list --count
to count the number of commits between the two hashes, which is the number of commits the local branch is behind the remote branch.Note that this script assumes that the corresponding remote branch is named
origin/branch_name
. If your remote is named differently, you'll need to adjust the script accordingly.尝试 git fetch --dry-run
手册(
git help fetch
)说:这可能根本不显示任何输出(表明存储库是最新的);如果您想查看一些输出,请尝试 git fetch --dry-run --verbose
Try
git fetch --dry-run
The manual (
git help fetch
) says:This may show no output at all (indicating repo is up-to-date); if you prefer to see some output try
git fetch --dry-run --verbose
首先使用 git Remote update 来更新您的远程引用。然后你可以执行以下操作之一,例如:
领先、落后或偏离。如果什么也没说,当地和
远程都是一样的。结果示例:
git show-branch *master
将向您显示所有分支中的提交名称以“master”结尾的分支(例如 master 和 origin/master)。
如果您将
-v
与 git remote update (git remote -v update
) 一起使用,您可以看到哪些分支已更新,因此您实际上不需要任何其他命令。First use
git remote update
, to bring your remote refs up to date. Then you can do one of several things, such as:git status -uno
will tell you whether the branch you are trackingis ahead, behind or has diverged. If it says nothing, the local and
remote are the same. Sample result:
git show-branch *master
will show you the commits in all of thebranches whose names end in 'master' (eg master and origin/master).
If you use
-v
with git remote update (git remote -v update
) you can see which branches got updated, so you don't really need any further commands.结果:
Result:
您可以使用 git 远程更新; git status -uno 检查您的本地分支是否与原始分支保持同步。
you can use
git remote update; git status -uno
to check if your local branch is up-to-date with the origin one.不是真的 - 但我不知道 git fetch 会有什么伤害,因为它不会改变你的任何本地分支。
Not really - but I don't see how
git fetch
would hurt as it won't change any of your local branches.您需要发出两个命令:
You'll need to issue two commands: