139

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 ?

Misha Moroshko
  • 166,356
  • 226
  • 505
  • 746

13 Answers13

134

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

--dry-run
Show what would be done, without making any changes.
Philip Oakley
  • 13,333
  • 9
  • 48
  • 71
  • 3
    Thanks! Though, it's hard to understand from the output which files were added/modified/removed. – Misha Moroshko Oct 30 '11 at 01:56
  • 1
    You get to see what tags are updated and the start..end commit range for the various branches. If this isn't sufficient, then do it as a proper fetch (not pull) which will give you a proper, separate, local copy of the remote, without affecting your own branch work. A pull would attempt to merge the two, which isn't what you want. The data transfer is the same whether you --dry-run or not. – Philip Oakley Oct 30 '11 at 20:09
  • 10
    why is it that when I run `git fetch --dry-run` nothing shows up? – Paramvir Singh Karwal Aug 20 '18 at 18:58
  • 5
    @ParamvirSinghKarwal Git is sparse in what it reports. If there is nothing to say, it says nothing, as if nothing happened. Maybe you are up to date for your regular fetch's refspec. Maybe add `--all`? – Philip Oakley Aug 21 '18 at 13:57
  • I don't know if git changed or I'm missing something, but `git fetch --dry-run` gives no output when `git pull` is going to pull a bunch of updates. So in other words it doesn't tell me if my branch is behind or not. – Aaron Beall Sep 26 '19 at 21:47
  • 2
    @AaronBeall If that's the case, then it most likely means you have already fetched those changes locally (so nothing to fetch), but haven't yet merged them to your branch. `git pull` is roughly equivalent to a `git fetch && git merge`. If you at any point ran the fetch without the `--dry-run`, then you already have fetched things locally. – DuckPuppy Oct 22 '19 at 14:20
  • 3
    @DuckPuppy Yes, so it seems `git fetch` is not sufficient to "know if my local repo is up to date". It's not clear to me if the OP didn't understand this when this answer was accepted or the question isn't clearly worded. – Aaron Beall Oct 22 '19 at 16:32
  • 2
    @AaronBeall Well, if OP meant checking local repository state, then that's right solution. Checking if actual working copy is up to date is different story. There are some open topics and all point that there is no such option. there is no `git merge --dry-run` nor `git pull --dry-run` which is kind of pain. Always proposed solution is to fetch, `git merge --no-commit --no-ff` and abort but it does not answer the question as fetch was already made. Not even mentioning not staged changes. – Maciej Załucki Nov 16 '19 at 00:57
72

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.

Piyush Agarwal
  • 731
  • 5
  • 4
  • 7
    Why is this so far down? `git remote update ; git status -uno` solved it. `git fetch --dry-run` gave no output even in cases where the local was behind remote. – Aaron Beall Sep 26 '19 at 21:58
57
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) <-------
jim smith
  • 2,394
  • 5
  • 29
  • 35
26

you can use git remote update; git status -uno to check if your local branch is up-to-date with the origin one.

Lodewijk Bogaards
  • 19,777
  • 3
  • 28
  • 52
flowdee
  • 593
  • 6
  • 11
  • 26
    It only gives the local status, not checking with the remote branch. – Ishan Liyanage Oct 01 '14 at 05:08
  • 7
    Only gives local, but `git remote update ; git status -uno` did the trick! The `git fetch --dry-run` gave no output when I expected it to (and `git pull` would pull things). – Aaron Beall Sep 26 '19 at 21:59
  • @HashimAziz - Can you expand on why this answer is wrong and what myth it perpetuates? This is otherwise not illuminating for someone reading this question and looking to solve the same problem as OP – Lou Nov 24 '22 at 10:47
  • 1
    @Lou It has very recently been edited to include `git remote update`, so my previous comment no longer applies, although now it's just a duplicate of this answer: https://stackoverflow.com/a/52307619/1191147. – Hashim Aziz Nov 24 '22 at 16:07
13

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

  • 2
    I completely agree with this. Git pull can be damaging, overwriting files. But git fetch pulls down only the meta data, allowing commands like git status to to tell you whether your local repo is up to date or not without overwriting any files. It may not answer the letter of the question, but it answers the spirit of the question, giving you the tool you want. Git fetch, then git status will tell you where your local repo is in relation to the remote without overwriting files. – merlit64 Sep 15 '20 at 12:31
10

You'll need to issue two commands:

  1. git fetch origin
  2. git status
user3695833
  • 125
  • 1
  • 2
  • 2
    This works but the question clearly says without 'fetch'. There are many cases where you want to know how your local repo is different from the remote repo without actually fetching or merging the changes. – pedram bashiri Sep 07 '19 at 17:20
  • 1
    @pedrambashiri I'm genuinely curious, like what? To my knowledge a fetch alone can never harm anything. – Hashim Aziz Jul 28 '20 at 19:31
  • 1
    @Prometheus first thing, I just wanted to point out that when a question clearly states 'without fetch' you simply cannot use fetch in your answer, if there's no other way to do it, or no real case for doing it without, that needs to be explained in the answer. But to answer your question, a fetch updates your local copy of the remote repo. Look at the diagram in this post https://blog.osteele.com/2008/05/my-git-workflow/ – pedram bashiri Aug 03 '20 at 20:54
6

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

Community
  • 1
  • 1
Amanuel Nega
  • 1,899
  • 1
  • 24
  • 42
6

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.

Community
  • 1
  • 1
braitsch
  • 14,906
  • 5
  • 42
  • 37
5
git remote show origin


Enter passphrase for key ....ssh/id_rsa:
* remote origin
  Fetch URL: git@github.com:mamaque/systems.git
  Push  URL: git@github.com: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

Jean-Marc
  • 740
  • 8
  • 9
4

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.

Das_Geek
  • 2,775
  • 7
  • 20
  • 26
Don Davis
  • 105
  • 6
  • This answer is way underrated. Running "git fetch --dry-run -v" is the simplest answer and provides more than enough info about the status of the local and remote repositories. – waykiki Mar 31 '22 at 07:34
3
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
 
Barry
  • 3,303
  • 7
  • 23
  • 42
adarsh
  • 135
  • 7
  • please note your answer does not meet the question where OP asks to not use `git fetch`, if this is not possible your answer should explain why before using it (if at all) – Barry Nov 05 '21 at 11:30
2

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.

ifthenelse
  • 647
  • 1
  • 11
  • 21
2

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?

count0
  • 2,537
  • 2
  • 22
  • 30
Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
  • 5
    Please confirm that! You just answered what u think! You should be more careful as newbies will tumble with it! – Amanuel Nega Nov 11 '14 at 08:29
  • 4
    @AmanuelNega: This is just basic logic. If you want to know whether your local repo is in the same state as the remote repo, you need to know the state of the remote repo. Period. If you don't know the state of the remote repo, you cannot possibly know whether the local repo is in the same state. Note that the highest voted and accepted answer uses `git pull`, which the OP explicitly forbids in his question. – Jörg W Mittag Nov 11 '14 at 08:47
  • 2
    Be informed! `git status -uno` this works and one can also use `git show-branch *master` to see the status of all the master branches! Are you still saying it is impossible? You can see the status of any branch as long as u have access to the remote! – Amanuel Nega Nov 11 '14 at 08:51
  • 2
    @AmanuelNega: `git status` only tells you the status of your local refs, it does *not* tell you whether your local refs are up-to-date with the remote refs. Again: it is simply logically impossible to know what the state of the remote repo is without getting the state of the remote repo. Period. This is just the basic laws of spacetime. – Jörg W Mittag Nov 11 '14 at 08:53
  • I don't know how this answer got downvoted. Jorg's answer is completely logical and correct. – Powers Aug 25 '15 at 21:28
  • 2
    This is technically correct, because the OP said "without doing `git fetch` or `git pull`". The OP didn't actually *mean* (as far as I can tell) "without using the `git pull` or `git fetch` commands at all" but rather "without fetching the remote repo. I think Jörg interpreted it in the first way, which is understandable. The blame could just as easily be placed on the OP—it is silly to downvote this answer. – iconoclast May 18 '16 at 19:08
  • It depends on the definition of "up to date". If OP is simply interested in knowing whether his local branch is ahead of the remote or not (ie do I have anything to push?), he can find out without a fetch. – ealfonso May 07 '17 at 19:26
  • 6
    It isn't "logically" impossible since self-evidently one could ring someone in your server room and say, "what is the hash of your HEAD on master branch" they could tell you and then you could check local and see that you don't have that hash. Now you know they are out of sync. – James Robinson Nov 18 '17 at 18:39
  • Maybe there are some command like `git status --check-remote`. If the author want to check if the repo is up-to-date without request the remote repo, then it is logically impossible. – ramwin Apr 25 '19 at 09:47