Abramovick
Abramovick

Reputation: 53

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

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!

Upvotes: 4

Views: 10932

Answers (3)

FelipeC
FelipeC

Reputation: 9488

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.

Upvotes: 13

talles
talles

Reputation: 15096

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.

Upvotes: 7

vkulkarni
vkulkarni

Reputation: 897

As per my knowledge, two commands might help. Sorry if I am wrong. I appologies.

Git has two commands to update itself from a remote repository. git fetch will synchronize you with another repo, pulling down any data that you do not have locally and giving you bookmarks to where each branch on that remote was when you synchronized. These are called "remote branches" and are identical to local branches except that Git will not allow you to check them out - however, you can merge from them, diff them to other branches, run history logs on them, etc. You do all of that stuff locally after you synchronize.

The second command that will fetch down new data from a remote server is git pull. This command will basically run a git fetch immediately followed by a git merge of the branch on that remote that is tracked by whatever branch you are currently in. Running the fetch and merge commands separately involves less magic and less problems, but if you like the idea of pull,

Assuming you have a remote all set up and you want to pull in updates, you would first run git fetch [alias] to tell Git to fetch down all the data it has that you do not, then you would run git merge [alias]/[branch] to merge into your current branch anything new you see on the server (like if someone else has pushed in the meantime). So, if you were working on a Hello World project with several other people and wanted to bring in any changes that had been pushed since we last connected, we would do something like this:

$ git fetch github
remote: Counting objects: 4006, done.
remote: Compressing objects: 100% (1322/1322), done.
remote: Total 2783 (delta 1526), reused 2587 (delta 1387)
Receiving objects: 100% (2783/2783), 1.23 MiB | 10 KiB/s, done.
Resolving deltas: 100% (1526/1526), completed with 387 local objects.
From github.com:schacon/hw
   8e29b09..c7c5a10  master     -> github/master
   0709fdc..d4ccf73  c-langs    -> github/c-langs
   6684f82..ae06d2b  java       -> github/java
 * [new branch]      ada        -> github/ada
 * [new branch]      lisp       -> github/lisp

Here we can see that since we last synchronized with this remote, five branches have been added or updated. The 'ada' and 'lisp' branches are new, where the 'master', 'c-langs' and 'java' branches have been updated. In our example case, other developers are pushing proposed updates to remote branches for review before they're merged into 'master'.

You can see the mapping that Git makes. The 'master' branch on the remote repository becomes a branch named 'github/master' locally. That way you can merge the 'master' branch on that remote into the local 'master' branch by running git merge github/master. Or, you can see what new commits are on that branch by running git log github/master ^master. If your remote is named 'origin' it would be origin/master instead. Almost any command you would run using local branches you can use remote branches with too.

If you have more than one remote repository, you can either fetch from specific ones by running git fetch [alias] or you can tell Git to synchronize with all of your remotes by running git fetch --all.

Also The git status Command

The git status command displays the state of the working directory and the staging area. It lets you see which changes have been staged, which haven’t, and which files aren’t being tracked by Git. Status output does not show you any information regarding the committed project history. For this, you need to use git log.

git status

List which files are staged, unstaged, and untracked.

Upvotes: 0

Related Questions