I want to synchronize changes with a remote Git repository (at GitHub) using the Git Api in Java (Egit and JGit). How can I do this? Is this possible by knowing the HEAD ID between two revisions, or some other way?
Just knowing the HEAD
of each of the two repositories is enough to tell you if they're out of sync but it is not enough to tell you which one is ahead of the other. In the general case, more than one person can push to a repository so assuming your local repository is always ahead of your remote would not be wise. You would have to use the API to find the first common SHA and using that determine how to handle synchronizing your repositories. You'll have to handle three (really more) cases:
The first two cases are trivial, the third is far more complex. You will always have to pull before you can synchronize your local changes with the remote and pulling could result in merge conflicts. If you are not careful you could break some code by trying to force a commit when a conflict has arisen.