javagitgit-rebasejgit

How to rebase in Jgit


I want to rebase master onto origin/master. I am using this code:

private void rebase(Git git) throws GitAPIException {
    git.checkout().setName("master").call();
    List<Ref> branches = git.branchList().setListMode(ListBranchCommand.ListMode.ALL).call();
    for (Ref ref : branches) {
        if (ref.getName().equals("refs/remotes/origin/master")) {
            git.rebase().setUpstream("refs/remotes/origin/master").call();
            return;
        }
    }
}

After running git status I see rebase in progress and no rebase done.


Solution

  • As an alternative I tried pull with rebase

    private void pullWithRebase(Git git) throws GitAPIException {
        git.checkout().setName("master").call();
        List<Ref> branches = git.branchList().setListMode(ListBranchCommand.ListMode.ALL).call();
        String remoteMasterBranchName = "refs/remotes/origin/master";
        for (Ref ref : branches) {
            if (remoteMasterBranchName.equals(ref.getName())) {
                git.pull().setRemoteBranchName("master").setRebase(true).call();
                return;
            }
        }
    }
    

    At first it didn't work for me for the same reason. But the case was that I had merge conflicts. I ran this code again so that there were no conflicts and it worked.