Misha Moroshko
Misha Moroshko

Reputation: 171351

Git: How to check if a local repo is up to date?

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 ?

Upvotes: 153

Views: 254492

Answers (13)

Philip Oakley
Philip Oakley

Reputation: 14061

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

Upvotes: 145

ifthenelse
ifthenelse

Reputation: 717

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.

Upvotes: 2

flowdee
flowdee

Reputation: 633

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

Upvotes: 29

adarsh
adarsh

Reputation: 165

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
 

Upvotes: 4

Jean-Marc
Jean-Marc

Reputation: 909

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

Upvotes: 5

Piyush Agarwal
Piyush Agarwal

Reputation: 801

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.

Upvotes: 79

Jörg W Mittag
Jörg W Mittag

Reputation: 369458

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?

Upvotes: 2

Don Davis
Don Davis

Reputation: 105

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.

Upvotes: 4

jim smith
jim smith

Reputation: 2454

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) <-------

Upvotes: 59

user3695833
user3695833

Reputation: 135

You'll need to issue two commands:

  1. git fetch origin
  2. git status

Upvotes: 11

Amanuel Nega
Amanuel Nega

Reputation: 1977

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

Upvotes: 6

braitsch
braitsch

Reputation: 15344

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.

Upvotes: 6

Niclas Kirschmeier
Niclas Kirschmeier

Reputation: 157

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

Upvotes: 14

Related Questions