Class Git
- java.lang.Object
-
- org.eclipse.jgit.api.Git
-
- All Implemented Interfaces:
AutoCloseable
public class Git extends Object implements AutoCloseable
Offers a "GitPorcelain"-like API to interact with a git repository.The GitPorcelain commands are described in the Git Documentation.
This class only offers methods to construct so-called command classes. Each GitPorcelain command is represented by one command class.
Example: this class offers acommit()method returning an instance of theCommitCommandclass. TheCommitCommandclass has setters for all the arguments and options. TheCommitCommandclass also has acallmethod to actually execute the commit. The following code show's how to do a simple commit:Git git = new Git(myRepo); git.commit().setMessage("Fix393").setAuthor(developerIdent).call();All mandatory parameters for commands have to be specified in the methods of this class, the optional parameters have to be specified by the setter-methods of the Command class.This class is intended to be used internally (e.g. by JGit tests) or by external components (EGit, third-party tools) when they need exactly the functionality of a GitPorcelain command. There are use-cases where this class is not optimal and where you should use the more low-level JGit classes. The methods in this class may for example offer too much functionality or they offer the functionality with the wrong arguments.
-
-
Constructor Summary
Constructors Constructor Description Git(Repository repo)Construct a newGitobject which can interact with the specified git repository.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description AddCommandadd()Return a command object to execute aAddcommandApplyCommandapply()Return a command object to execute aapplycommandArchiveCommandarchive()Return a command to create an archive from a treeBlameCommandblame()Return a command object to execute ablamecommandCreateBranchCommandbranchCreate()Return a command object used to create branchesDeleteBranchCommandbranchDelete()Return a command object used to delete branchesListBranchCommandbranchList()Return a command object used to list branchesRenameBranchCommandbranchRename()Return a command object used to rename branchesCheckoutCommandcheckout()Return a command object to execute acheckoutcommandCherryPickCommandcherryPick()Return a command object to execute acherry-pickcommandCleanCommandclean()Return a command object to execute acleancommandstatic CloneCommandcloneRepository()Return a command object to execute aclonecommandvoidclose()CommitCommandcommit()Return a command object to execute aCommitcommandDescribeCommanddescribe()Return a command object to come up with a short name that describes a commit in terms of the nearest git tag.DiffCommanddiff()Return a command object to execute adiffcommandFetchCommandfetch()Return a command object to execute aFetchcommandGarbageCollectCommandgc()Return a command object to execute agccommandRepositorygetRepository()Get repositorystatic InitCommandinit()Return a command object to execute ainitcommandLogCommandlog()Return a command object to execute aLogcommandLsRemoteCommandlsRemote()Return a command object to execute als-remotecommandstatic LsRemoteCommandlsRemoteRepository()Return a command to list remote branches/tags without a local repository.MergeCommandmerge()Return a command object to execute aMergecommandNameRevCommandnameRev()Return a command object to find human-readable names of revisions.AddNoteCommandnotesAdd()Return a command to add notes to an objectListNotesCommandnotesList()Return a command to list all notesRemoveNoteCommandnotesRemove()Return a command to remove notes on an objectShowNoteCommandnotesShow()Return a command to show notes on an objectstatic Gitopen(File dir)Open repositorystatic Gitopen(File dir, FS fs)Open repositoryPullCommandpull()Return a command object to execute aPullcommandPushCommandpush()Return a command object to execute aPushcommandRebaseCommandrebase()Return a command object to execute aRebasecommandReflogCommandreflog()Return a command object to execute areflogcommandRemoteAddCommandremoteAdd()Return a command used to add a new remote.RemoteListCommandremoteList()Return a command used to list the available remotes.RemoteRemoveCommandremoteRemove()Return a command used to remove an existing remote.RemoteSetUrlCommandremoteSetUrl()Return a command used to change the URL of an existing remote.ResetCommandreset()Return a command object to execute aresetcommandRevertCommandrevert()Return a command object to execute arevertcommandRmCommandrm()Return a command object to execute armcommandstatic voidshutdown()Shutdown JGit and release resources it holds like NLS and thread poolsStashApplyCommandstashApply()Returs a command object used to apply a stashed commitStashCreateCommandstashCreate()Return a command object used to create a stashed commitStashDropCommandstashDrop()Return a command object used to drop a stashed commitStashListCommandstashList()Return a command object used to list stashed commitsStatusCommandstatus()Return a command object to execute astatuscommandSubmoduleAddCommandsubmoduleAdd()Return a command object to execute asubmodule addcommandSubmoduleDeinitCommandsubmoduleDeinit()Returns a command object to execute asubmodule deinitcommandSubmoduleInitCommandsubmoduleInit()Return a command object to execute asubmodule initcommandSubmoduleStatusCommandsubmoduleStatus()Returns a command object to execute asubmodule statuscommandSubmoduleSyncCommandsubmoduleSync()Return a command object to execute asubmodule synccommandSubmoduleUpdateCommandsubmoduleUpdate()Return a command object to execute asubmodule updatecommandTagCommandtag()Return a command object to execute aTagcommandDeleteTagCommandtagDelete()Return a command object used to delete tagsListTagCommandtagList()Return a command object used to list tagsStringtoString()VerifySignatureCommandverifySignature()Return a command to verify signatures of tags or commits.static Gitwrap(Repository repo)Wrap repository
-
-
-
Constructor Detail
-
Git
public Git(Repository repo)
Construct a newGitobject which can interact with the specified git repository.All command classes returned by methods of this class will always interact with this git repository.
The caller is responsible for closing the repository;
close()on this instance does not close the repo.- Parameters:
repo- the git repository this class is interacting with;nullis not allowed.
-
-
Method Detail
-
open
public static Git open(File dir) throws IOException
Open repository- Parameters:
dir- the repository to open. May be either the GIT_DIR, or the working tree directory that contains.git.- Returns:
- a
Gitobject for the existing git repository - Throws:
IOException
-
open
public static Git open(File dir, FS fs) throws IOException
Open repository- Parameters:
dir- the repository to open. May be either the GIT_DIR, or the working tree directory that contains.git.fs- filesystem abstraction to use when accessing the repository.- Returns:
- a
Gitobject for the existing git repository. Closing this instance will close the repo. - Throws:
IOException
-
wrap
public static Git wrap(Repository repo)
Wrap repository
-
close
public void close()
Free resources associated with this instance.
If the repository was opened by a static factory method in this class, then this method calls
Repository.close()on the underlying repository instance. (Whether this actually releases underlying resources, such as file handles, may vary; seeRepositoryfor more details.)If the repository was created by a caller and passed into
Git(Repository)or a static factory method in this class, then this method does not call close on the underlying repository.In all cases, after calling this method you should not use this
Gitinstance anymore.- Specified by:
closein interfaceAutoCloseable- Since:
- 3.2
-
cloneRepository
public static CloneCommand cloneRepository()
Return a command object to execute aclonecommand- Returns:
- a
CloneCommandused to collect all optional parameters and to finally execute theclonecommand - See Also:
- Git documentation about clone
-
lsRemoteRepository
public static LsRemoteCommand lsRemoteRepository()
Return a command to list remote branches/tags without a local repository.- Returns:
- a
LsRemoteCommand - Since:
- 3.1
-
init
public static InitCommand init()
Return a command object to execute ainitcommand- Returns:
- a
InitCommandused to collect all optional parameters and to finally execute theinitcommand - See Also:
- Git documentation about init
-
shutdown
public static void shutdown()
Shutdown JGit and release resources it holds like NLS and thread pools- Since:
- 5.8
-
commit
public CommitCommand commit()
Return a command object to execute aCommitcommand- Returns:
- a
CommitCommandused to collect all optional parameters and to finally execute theCommitcommand - See Also:
- Git documentation about Commit
-
log
public LogCommand log()
Return a command object to execute aLogcommand- Returns:
- a
LogCommandused to collect all optional parameters and to finally execute theLogcommand - See Also:
- Git documentation about Log
-
merge
public MergeCommand merge()
Return a command object to execute aMergecommand- Returns:
- a
MergeCommandused to collect all optional parameters and to finally execute theMergecommand - See Also:
- Git documentation about Merge
-
pull
public PullCommand pull()
Return a command object to execute aPullcommand- Returns:
- a
PullCommand
-
branchCreate
public CreateBranchCommand branchCreate()
Return a command object used to create branches- Returns:
- a
CreateBranchCommand
-
branchDelete
public DeleteBranchCommand branchDelete()
Return a command object used to delete branches- Returns:
- a
DeleteBranchCommand
-
branchList
public ListBranchCommand branchList()
Return a command object used to list branches- Returns:
- a
ListBranchCommand
-
tagList
public ListTagCommand tagList()
Return a command object used to list tags- Returns:
- a
ListTagCommand
-
branchRename
public RenameBranchCommand branchRename()
Return a command object used to rename branches- Returns:
- a
RenameBranchCommand
-
add
public AddCommand add()
Return a command object to execute aAddcommand- Returns:
- a
AddCommandused to collect all optional parameters and to finally execute theAddcommand - See Also:
- Git documentation about Add
-
tag
public TagCommand tag()
Return a command object to execute aTagcommand- Returns:
- a
TagCommandused to collect all optional parameters and to finally execute theTagcommand - See Also:
- Git documentation about Tag
-
fetch
public FetchCommand fetch()
Return a command object to execute aFetchcommand- Returns:
- a
FetchCommandused to collect all optional parameters and to finally execute theFetchcommand - See Also:
- Git documentation about Fetch
-
push
public PushCommand push()
Return a command object to execute aPushcommand- Returns:
- a
PushCommandused to collect all optional parameters and to finally execute thePushcommand - See Also:
- Git documentation about Push
-
cherryPick
public CherryPickCommand cherryPick()
Return a command object to execute acherry-pickcommand- Returns:
- a
CherryPickCommandused to collect all optional parameters and to finally execute thecherry-pickcommand - See Also:
- Git documentation about cherry-pick
-
revert
public RevertCommand revert()
Return a command object to execute arevertcommand- Returns:
- a
RevertCommandused to collect all optional parameters and to finally execute thecherry-pickcommand - See Also:
- Git documentation about reverting changes
-
rebase
public RebaseCommand rebase()
Return a command object to execute aRebasecommand- Returns:
- a
RebaseCommandused to collect all optional parameters and to finally execute therebasecommand - See Also:
- Git documentation about rebase
-
rm
public RmCommand rm()
Return a command object to execute armcommand- Returns:
- a
RmCommandused to collect all optional parameters and to finally execute thermcommand - See Also:
- Git documentation about rm
-
checkout
public CheckoutCommand checkout()
Return a command object to execute acheckoutcommand- Returns:
- a
CheckoutCommandused to collect all optional parameters and to finally execute thecheckoutcommand - See Also:
- Git documentation about checkout
-
reset
public ResetCommand reset()
Return a command object to execute aresetcommand- Returns:
- a
ResetCommandused to collect all optional parameters and to finally execute theresetcommand - See Also:
- Git documentation about reset
-
status
public StatusCommand status()
Return a command object to execute astatuscommand- Returns:
- a
StatusCommandused to collect all optional parameters and to finally execute thestatuscommand - See Also:
- Git documentation about status
-
archive
public ArchiveCommand archive()
Return a command to create an archive from a tree- Returns:
- a
ArchiveCommand - Since:
- 3.1
-
notesAdd
public AddNoteCommand notesAdd()
Return a command to add notes to an object- Returns:
- a
AddNoteCommand
-
notesRemove
public RemoveNoteCommand notesRemove()
Return a command to remove notes on an object- Returns:
- a
RemoveNoteCommand
-
notesList
public ListNotesCommand notesList()
Return a command to list all notes- Returns:
- a
ListNotesCommand
-
notesShow
public ShowNoteCommand notesShow()
Return a command to show notes on an object- Returns:
- a
ShowNoteCommand
-
lsRemote
public LsRemoteCommand lsRemote()
Return a command object to execute als-remotecommand- Returns:
- a
LsRemoteCommandused to collect all optional parameters and to finally execute thestatuscommand - See Also:
- Git documentation about ls-remote
-
clean
public CleanCommand clean()
Return a command object to execute acleancommand- Returns:
- a
CleanCommandused to collect all optional parameters and to finally execute thecleancommand - See Also:
- Git documentation about Clean
-
blame
public BlameCommand blame()
Return a command object to execute ablamecommand- Returns:
- a
BlameCommandused to collect all optional parameters and to finally execute theblamecommand - See Also:
- Git documentation about Blame
-
reflog
public ReflogCommand reflog()
Return a command object to execute areflogcommand- Returns:
- a
ReflogCommandused to collect all optional parameters and to finally execute thereflogcommand - See Also:
- Git documentation about reflog
-
diff
public DiffCommand diff()
Return a command object to execute adiffcommand- Returns:
- a
DiffCommandused to collect all optional parameters and to finally execute thediffcommand - See Also:
- Git documentation about diff
-
tagDelete
public DeleteTagCommand tagDelete()
Return a command object used to delete tags- Returns:
- a
DeleteTagCommand
-
submoduleAdd
public SubmoduleAddCommand submoduleAdd()
Return a command object to execute asubmodule addcommand- Returns:
- a
SubmoduleAddCommandused to add a new submodule to a parent repository
-
submoduleInit
public SubmoduleInitCommand submoduleInit()
Return a command object to execute asubmodule initcommand- Returns:
- a
SubmoduleInitCommandused to initialize the repository's config with settings from the .gitmodules file in the working tree
-
submoduleDeinit
public SubmoduleDeinitCommand submoduleDeinit()
Returns a command object to execute asubmodule deinitcommand- Returns:
- a
SubmoduleDeinitCommandused to remove a submodule's working tree manifestation - Since:
- 4.10
-
submoduleStatus
public SubmoduleStatusCommand submoduleStatus()
Returns a command object to execute asubmodule statuscommand- Returns:
- a
SubmoduleStatusCommandused to report the status of a repository's configured submodules
-
submoduleSync
public SubmoduleSyncCommand submoduleSync()
Return a command object to execute asubmodule synccommand- Returns:
- a
SubmoduleSyncCommandused to update the URL of a submodule from the parent repository's .gitmodules file
-
submoduleUpdate
public SubmoduleUpdateCommand submoduleUpdate()
Return a command object to execute asubmodule updatecommand- Returns:
- a
SubmoduleUpdateCommandused to update the submodules in a repository to the configured revision
-
stashList
public StashListCommand stashList()
Return a command object used to list stashed commits- Returns:
- a
StashListCommand
-
stashCreate
public StashCreateCommand stashCreate()
Return a command object used to create a stashed commit- Returns:
- a
StashCreateCommand - Since:
- 2.0
-
stashApply
public StashApplyCommand stashApply()
Returs a command object used to apply a stashed commit- Returns:
- a
StashApplyCommand - Since:
- 2.0
-
stashDrop
public StashDropCommand stashDrop()
Return a command object used to drop a stashed commit- Returns:
- a
StashDropCommand - Since:
- 2.0
-
apply
public ApplyCommand apply()
Return a command object to execute aapplycommand- Returns:
- a
ApplyCommandused to collect all optional parameters and to finally execute theapplycommand - Since:
- 2.0
- See Also:
- Git documentation about apply
-
gc
public GarbageCollectCommand gc()
Return a command object to execute agccommand- Returns:
- a
GarbageCollectCommandused to collect all optional parameters and to finally execute thegccommand - Since:
- 2.2
- See Also:
- Git documentation about gc
-
nameRev
public NameRevCommand nameRev()
Return a command object to find human-readable names of revisions.- Returns:
- a
NameRevCommand. - Since:
- 3.0
-
describe
public DescribeCommand describe()
Return a command object to come up with a short name that describes a commit in terms of the nearest git tag.- Returns:
- a
DescribeCommand. - Since:
- 3.2
-
remoteList
public RemoteListCommand remoteList()
Return a command used to list the available remotes.- Returns:
- a
RemoteListCommand - Since:
- 4.2
-
remoteAdd
public RemoteAddCommand remoteAdd()
Return a command used to add a new remote.- Returns:
- a
RemoteAddCommand - Since:
- 4.2
-
remoteRemove
public RemoteRemoveCommand remoteRemove()
Return a command used to remove an existing remote.- Returns:
- a
RemoteRemoveCommand - Since:
- 4.2
-
remoteSetUrl
public RemoteSetUrlCommand remoteSetUrl()
Return a command used to change the URL of an existing remote.- Returns:
- a
RemoteSetUrlCommand - Since:
- 4.2
-
verifySignature
public VerifySignatureCommand verifySignature()
Return a command to verify signatures of tags or commits.- Returns:
- a
VerifySignatureCommand - Since:
- 5.11
-
getRepository
public Repository getRepository()
Get repository- Returns:
- the git repository this class is interacting with; see
close()for notes on closing this repository.
-
-