0

Am trying to push my local directory to Github with java,getting below exception.but i can able to create the repository and can able to search the repository.but not able to push list of files(complete directory)

static void pushRepo() {
    try {
        String localPath = "D:\\path\\to\\directory";
        Repository localRepo = new FileRepository(localPath);
        Git git = new Git(localRepo);
        // add remote repo:
        RemoteAddCommand remoteAddCommand = git.remoteAdd();
        remoteAddCommand.setName("origin");
        //remoteAddCommand.setUri(new URIish("https://github.com/.git"));//url corretected
        remoteAddCommand.setUri(new URIish("git@github/api2.git"));//url corretected
        // you can add more settings here if needed
        remoteAddCommand.call();
        // push to remote:
        PushCommand pushCommand = git.push();
        pushCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider("abcd", "abcd123"));
        // you can add more settings here if needed
        pushCommand.call();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Exception:

org.eclipse.jgit.api.errors.TransportException: Nothing to push.
    at org.eclipse.jgit.api.PushCommand.call(PushCommand.java:180)
    at stash.StashClone.pushRepo(StashClone.java:71)
    at stash.StashClone.main(StashClone.java:30)
Caused by: org.eclipse.jgit.errors.TransportException: Nothing to push.
    at org.eclipse.jgit.transport.Transport.push(Transport.java:1298)
    at org.eclipse.jgit.api.PushCommand.call(PushCommand.java:169)
    ... 2 more
Doss
  • 31
  • 1
  • 14
  • I notice you haven't committed anything...if there's nothing committed, there's nothing to push. – Makoto Aug 02 '18 at 14:55

1 Answers1

2

You must first add and commit files in your local directory into local git repository before you can push them to remote.

If you want to do it in JGit use AddCommand and CommitCommand:

AddCommand add = git.add();
add.addFilepattern("."); // the directory of your files for recursive add
add.call();

CommitCommand commit = git.commit();
commit.setMessage("Initial import");
commit.call();
Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
  • Thanks lot karol,but now am getting Exception ,org.eclipse.jgit.errors.NoWorkTreeException: Bare Repository has neither a working tree, nor an index at org.eclipse.jgit.lib.Repository.getIndexFile(Repository.java:1147) – Doss Aug 02 '18 at 16:40
  • Your local directory is a bare git repo. See [What's the -practical- difference between a Bare and non-Bare repository?](https://stackoverflow.com/questions/5540883/whats-the-practical-difference-between-a-bare-and-non-bare-repository). – Karol Dowbecki Aug 02 '18 at 16:57
  • Thanks finally got solution,have to use 'Git git = Git.open(new File(localPath));' instead of Repository localRepo = new FileRepository(localPath); Git git = new Git(localRepo); – Doss Aug 03 '18 at 06:18