Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if your local git repository is up to date

Tags:

git

Basically am writing a crontab task that checks my remote git repository every 1 minute. If there is changes, it pulls them (to the local repo), go through each commit and does runs a grunt selenium task on each commit.

The part that am sort of stuck is how to find out if the remote git repository has new content and i need to pull!

like image 969
Abramovick Avatar asked Oct 11 '13 11:10

Abramovick


People also ask

How do you check if your local git is up-to-date?

you can use git remote update; git status -uno to check if your local branch is up-to-date with the origin one. It only gives the local status, not checking with the remote branch. Only gives local, but git remote update ; git status -uno did the trick!

How do I check for git updates?

You can check your current version of Git by running the git --version command in a terminal (Linux, macOS) or command prompt (Windows). If you don't see a supported version of Git, you'll need to either upgrade Git or perform a fresh install, as described below.


2 Answers

You can use git ls-remote to find that out:

git ls-remote origin master

This command will get the latest sha-1 id of the master branch, you can specify as many branches as you want, or none to get them all. You can use that to compare with the local branch.

However, it might be more efficient to fetch all the changes so you don't have to do the same network operation twice:

git fetch origin

This way you will get the updates in 'origin/master', and you can compare with 'master' to see if there are updates. This doesn't merge or rebase, so once you have detected there are updates, you can do git pull or git merge or git rebase, whatever it is that you want to do.

like image 168
FelipeC Avatar answered Sep 21 '22 05:09

FelipeC


git fetch
git log ..origin/master --oneline | wc -l

It outputs the number of revisions (commits) that would be applied when pulling.

If it's greater than 0 it means that there is new content.

like image 20
talles Avatar answered Sep 22 '22 05:09

talles