Question

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?

Was it helpful?

Solution

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:

  1. Your local branch is ahead of your remote
  2. Your remote is ahead of your local
  3. Your local and remote both have sets of commits not on the other, e.g., you pushed from one computer but didn't pull here or someone else pushed commits recently and you didn't notice, etc.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top