2014-12-19 74 views
0

我所做的到現在是 -JGit - 將一個特定的文件RemoteRepo

  1. 從遠程回購克隆。
  2. 添加文件到指定的目錄在我的項目,並取得了該文件
  3. 文件提交
  4. 試圖推動修改遠程回購 一些變化//它顯示我的消息在空和地位確定但該文件沒有被推送到遠程回購

    的另一個問題是,當我試圖打印推送消息是無限印刷 - MSG-空 - 狀態 - 確定 - 裁判/頭/主


private static final String REMOTE_URL = "http://abc.def.net/git/scm/nt/myprojectname.git" 

String srcPath = "D://MyFiles//fol//ErrorFile.txt"; 

// prepare a new folder for the cloned repository 
File localPath = File.createTempFile("TestGitRepository", ""); 
localPath.delete(); 

// then clone 
System.out.println("Cloning from " + REMOTE_URL + " to " + localPath); 
CredentialsProvider cp = new UsernamePasswordCredentialsProvider("myUserName","mypassword"); 

Git git = Git.cloneRepository().setURI(REMOTE_URL).setDirectory(localPath).setCredentialsProvider(cp).call(); 

String head = git.getRepository().getFullBranch(); 
if (head.startsWith("refs/heads/")) { 
     // Print branch name with "refs/heads/" stripped. 
     System.out.println("Current branch is " + git.getRepository().getBranch()); 
} 

String destPath = localPath+"\\myprojectName\\" ; 
File srcFile = new File(srcPath); 
File destFile = new File(destPath); 
FileUtils.copyFileToDirectory(srcFile, destFile, true) ; 

git.checkout(); // MISSED THIS LINE EARLIER 

// run the add-call 
git.add().setUpdate(true).addFilepattern(destFile.getAbsolutePath()).call(); 

RevCommit revCommit = git.commit().setMessage("My First Commit").call(); 
System.out.println(revCommit.getShortMessage()); 
Iterable<PushResult> resultIterable = git.push().setCredentialsProvider(cp).call(); 

while(resultIterable.iterator().hasNext()){    // LEADING TO INFINITE LOOP 
PushResult result = resultIterable.iterator().next(); 
Collection<RemoteRefUpdate> rs = result.getRemoteUpdates(); 
    for(RemoteRefUpdate rf : rs){ 
    System.out.println("Msg- "+rf.getMessage() + " - Status - "+rf.getStatus() + " - "+rf.getSrcRef()); 
    } 
} 

try { 
    System.out.println("Having repository: " + git.getRepository().getDirectory()); 
} finally { 
    git.getRepository().close(); 
} 
+0

檢查gitignore。該文件或其祖先文件夾中的一個? – 2014-12-19 08:01:34

+0

有了Git,你不會推送文件,你推送提交。你是如何確定剛剛創建的提交從未在目標git中結束的? – 2014-12-19 08:22:57

+0

@MagnusBäck,你可以推送文件,也就是說你創建一個文件,添加它,提交併推送它。 – 2014-12-19 08:31:55

回答

0

您的文件沒有提交,因爲你需要指定你想提交的文件。在你的情況

git.commit().setAll(true) 

應該工作,其相應的Git的承諾-a

我也得到了這個工作 -

CommitCommand commit = git.commit(); 
for (String file : files) { 
    commit.setOnly(file); 
} 
commit.setMessage(message).call() 
相關問題