Click here to Skip to main content
15,890,043 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the requirement where there are 10 commits in JGIT. And among that I need to delete (discard) 5 commits. Remaining 5 commits are pushed.

I need a JGIT code to find which commits are not pushed and want to discard those.

Currently I am trying rebasing using JGIT. And could find all the commits which are not pushed. But if they are in between of pushed commits then how to find them, discard them?

What I have tried:

public void discardUnpublishedChanges(String userId, String levelToDiscard) throws IOException, GitAPIException 
	{
		System.out.println("In DAO : discardUnpublishedChanges()");
		
		String release = this.getReleaseByUserId(userId);
		String projectPath = getProjectPath(userId);
		Git git = getGit(projectPath);
		
		List<Ref> calls = git.branchList().call();
		for (Ref ref : calls) 
		{
			List<Integer> counts = getCounts(git.getRepository(), ref.getName());
			if(ref.getName().equals("refs/heads/master") && (counts.get(0) > 0))
			{
				System.out.println("branch is behind  by " + counts.get(1) + " commits");
				System.out.println("branch is ahead  by " + counts.get(0) + " commits");
				System.out.println("local workspace has " + counts.get(0) + " saved changes which are not yet published");
				logger.debug("branch is behind  by " + counts.get(1) + " commits");
				logger.debug("branch is ahead  by " + counts.get(0) + " commits");
				logger.debug("local workspace has " + counts.get(0) + " saved changes which are not yet published");
				
				RebaseResult res = git.rebase().setUpstream("origin/master").runInteractively(new InteractiveHandler() 
				{
					List<RebaseTodoLine> temps = new ArrayList<>();
				            	
					public void prepareSteps(List<RebaseTodoLine> steps) 
					{
						for(RebaseTodoLine step :steps)
						{
							System.out.println("original:"+step.getShortMessage());
							
						}
					}
					public String modifyCommitMessage(String commit) {
						return "rewritten commit message";
					}
				}).call();
			}
			
		}
	}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900