Git:如何检查本地存储库是否是最新的?

发布于 2024-12-12 18:17:38 字数 93 浏览 3 评论 0原文

我想知道我的本地存储库是否是最新的(如果不是,理想情况下,我希望看到更改)。

我如何在不执行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

时光与爱终年不遇 2024-12-19 18:17:39

您必须运行 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 or git 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.

枕花眠 2024-12-19 18:17:39

另一种选择是使用查看远程分支的状态
git show-branch remote/branch 将其用作比较,您可以看到 git show-branch *branch 来查看所有遥控器以及存储库中的分支!查看此答案以了解更多信息 https://stackoverflow.com/a/3278427/2711378

Another alternative is to view the status of the remote branch using
git show-branch remote/branch to use it as a comparison you could see git 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

三生池水覆流年 2024-12-19 18:17:39
git remote show origin


Enter passphrase for key ....ssh/id_rsa:
* remote origin
  Fetch URL: [email protected]:mamaque/systems.git
  Push  URL: [email protected]:mamaque/systems.git 

  HEAD branch: main
  Remote branch:
    main tracked
   Local ref configured for 'git push':

主要推送到主要(最新)两者都是最新的
main 推送到 main(可快进)远程可以用本地更新
main 推送到 main(本地已过时)本地可以通过远程更新

git remote show origin


Enter passphrase for key ....ssh/id_rsa:
* remote origin
  Fetch URL: [email protected]:mamaque/systems.git
  Push  URL: [email protected]:mamaque/systems.git 

  HEAD branch: main
  Remote branch:
    main tracked
   Local ref configured for 'git push':

main pushes to main (up-to-date)Both are up to date
main pushes to main (fast-forwardable)Remote can be updated with Local
main pushes to main (local out of date)Local can be update with Remote

慢慢从新开始 2024-12-19 18:17:39

如果您使用,

git fetch --dry-run -v <link/to/remote/git/repo>

您将获得有关它是否是最新的反馈。所以基本上,您只需将“详细”选项添加到之前给出的答案中即可。

If you use

git fetch --dry-run -v <link/to/remote/git/repo>

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.

冧九 2024-12-19 18:17:39
git fetch origin
git status

你会看到类似的结果

您的分支比“origin/master”落后 9 次提交

更新远程更改的提交

git pull
 
git fetch origin
git status

you'll see result like

Your branch is behind 'origin/master' by 9 commits

to update to remote changes

git pull
 
红墙和绿瓦 2024-12-19 18:17:39

如果不使用 git fetch 或 git pull ,这是不可能的。如果不去远程存储库查看“最新”的含义,如何知道存储库是否“最新”?

This is impossible without using git fetch or git 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?

尝蛊 2024-12-19 18:17:39

要在不使用 git fetch 的情况下完成此任务,您可以使用 git rev-list 命令来比较本地分支上最近提交的哈希值和最近提交的哈希值在相应的远程分支上。下面是执行此操作的 Bash 脚本:

#!/bin/sh

# Get the name of the current branch
branch=$(git rev-parse --abbrev-ref HEAD)

# Get the hash of the most recent commit on the local branch
local_commit=$(git rev-list --max-count=1 $branch)

# Get the hash of the most recent commit on the corresponding remote branch
remote_commit=$(git rev-list --max-count=1 origin/$branch)

# Count the number of commits between the local and remote branches
commits_behind=$(git rev-list --count $local_commit..$remote_commit)

# Print the result
echo "The local branch is $commits_behind commits behind the corresponding remote branch."

该脚本首先使用 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 the git 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:

#!/bin/sh

# Get the name of the current branch
branch=$(git rev-parse --abbrev-ref HEAD)

# Get the hash of the most recent commit on the local branch
local_commit=$(git rev-list --max-count=1 $branch)

# Get the hash of the most recent commit on the corresponding remote branch
remote_commit=$(git rev-list --max-count=1 origin/$branch)

# Count the number of commits between the local and remote branches
commits_behind=$(git rev-list --count $local_commit..$remote_commit)

# Print the result
echo "The local branch is $commits_behind commits behind the corresponding remote branch."

This script first gets the name of the current branch using git rev-parse --abbrev-ref HEAD. It then uses git rev-list --max-count=1 to get the hash of the most recent commit on both the local and remote branches. Finally, it uses git 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.

倥絔 2024-12-19 18:17:38

尝试 git fetch --dry-run
手册(git help fetch)说:

--dry-run
Show what would be done, without making any changes.

这可能根本不显示任何输出(表明存储库是最新的);如果您想查看一些输出,请尝试 git fetch --dry-run --verbose

Try git fetch --dry-run
The manual (git help fetch) says:

--dry-run
Show what would be done, without making any changes.

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

黒涩兲箜 2024-12-19 18:17:38

首先使用 git Remote update 来更新您的远程引用。然后你可以执行以下操作之一,例如:

  1. git status -uno 会告诉你是否正在跟踪的分支
    领先、落后或偏离。如果什么也没说,当地和
    远程都是一样的。结果示例:

在分支 DEV 上

您的分支比“origin/DEV”落后 7 个提交,并且可以快进。

(使用“git pull”更新本地分支)

  1. 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:

  1. git status -uno will tell you whether the branch you are tracking
    is ahead, behind or has diverged. If it says nothing, the local and
    remote are the same. Sample result:

On branch DEV

Your branch is behind 'origin/DEV' by 7 commits, and can be fast-forwarded.

(use "git pull" to update your local branch)

  1. git show-branch *master will show you the commits in all of the
    branches 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.

╰沐子 2024-12-19 18:17:38
git remote show origin

结果:

HEAD branch: master
  Remote branch:
    master tracked
  Local branch configured for 'git pull':
    master merges with remote master
  Local ref configured for 'git push':
    master pushes to master (local out of date) <-------
git remote show origin

Result:

HEAD branch: master
  Remote branch:
    master tracked
  Local branch configured for 'git pull':
    master merges with remote master
  Local ref configured for 'git push':
    master pushes to master (local out of date) <-------
耳钉梦 2024-12-19 18:17:38

您可以使用 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.

维持三分热 2024-12-19 18:17:38

不是真的 - 但我不知道 git fetch 会有什么伤害,因为它不会改变你的任何本地分支。

Not really - but I don't see how git fetch would hurt as it won't change any of your local branches.

汹涌人海 2024-12-19 18:17:38

您需要发出两个命令:

  1. git fetch origin
  2. git status

You'll need to issue two commands:

  1. git fetch origin
  2. git status
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文