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 theCommitCommand
class. TheCommitCommand
class has setters for all the arguments and options. TheCommitCommand
class also has acall
method 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 newGit
object which can interact with the specified git repository.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description AddCommand
add()
Return a command object to execute aAdd
commandApplyCommand
apply()
Return a command object to execute aapply
commandArchiveCommand
archive()
Return a command to create an archive from a treeBlameCommand
blame()
Return a command object to execute ablame
commandCreateBranchCommand
branchCreate()
Return a command object used to create branchesDeleteBranchCommand
branchDelete()
Return a command object used to delete branchesListBranchCommand
branchList()
Return a command object used to list branchesRenameBranchCommand
branchRename()
Return a command object used to rename branchesCheckoutCommand
checkout()
Return a command object to execute acheckout
commandCherryPickCommand
cherryPick()
Return a command object to execute acherry-pick
commandCleanCommand
clean()
Return a command object to execute aclean
commandstatic CloneCommand
cloneRepository()
Return a command object to execute aclone
commandvoid
close()
CommitCommand
commit()
Return a command object to execute aCommit
commandDescribeCommand
describe()
Return a command object to come up with a short name that describes a commit in terms of the nearest git tag.DiffCommand
diff()
Return a command object to execute adiff
commandFetchCommand
fetch()
Return a command object to execute aFetch
commandGarbageCollectCommand
gc()
Return a command object to execute agc
commandRepository
getRepository()
Get repositorystatic InitCommand
init()
Return a command object to execute ainit
commandLogCommand
log()
Return a command object to execute aLog
commandLsRemoteCommand
lsRemote()
Return a command object to execute als-remote
commandstatic LsRemoteCommand
lsRemoteRepository()
Return a command to list remote branches/tags without a local repository.MergeCommand
merge()
Return a command object to execute aMerge
commandNameRevCommand
nameRev()
Return a command object to find human-readable names of revisions.AddNoteCommand
notesAdd()
Return a command to add notes to an objectListNotesCommand
notesList()
Return a command to list all notesRemoveNoteCommand
notesRemove()
Return a command to remove notes on an objectShowNoteCommand
notesShow()
Return a command to show notes on an objectstatic Git
open(File dir)
Open repositorystatic Git
open(File dir, FS fs)
Open repositoryPullCommand
pull()
Return a command object to execute aPull
commandPushCommand
push()
Return a command object to execute aPush
commandRebaseCommand
rebase()
Return a command object to execute aRebase
commandReflogCommand
reflog()
Return a command object to execute areflog
commandRemoteAddCommand
remoteAdd()
Return a command used to add a new remote.RemoteListCommand
remoteList()
Return a command used to list the available remotes.RemoteRemoveCommand
remoteRemove()
Return a command used to remove an existing remote.RemoteSetUrlCommand
remoteSetUrl()
Return a command used to change the URL of an existing remote.ResetCommand
reset()
Return a command object to execute areset
commandRevertCommand
revert()
Return a command object to execute arevert
commandRmCommand
rm()
Return a command object to execute arm
commandstatic void
shutdown()
Shutdown JGit and release resources it holds like NLS and thread poolsStashApplyCommand
stashApply()
Returs a command object used to apply a stashed commitStashCreateCommand
stashCreate()
Return a command object used to create a stashed commitStashDropCommand
stashDrop()
Return a command object used to drop a stashed commitStashListCommand
stashList()
Return a command object used to list stashed commitsStatusCommand
status()
Return a command object to execute astatus
commandSubmoduleAddCommand
submoduleAdd()
Return a command object to execute asubmodule add
commandSubmoduleDeinitCommand
submoduleDeinit()
Returns a command object to execute asubmodule deinit
commandSubmoduleInitCommand
submoduleInit()
Return a command object to execute asubmodule init
commandSubmoduleStatusCommand
submoduleStatus()
Returns a command object to execute asubmodule status
commandSubmoduleSyncCommand
submoduleSync()
Return a command object to execute asubmodule sync
commandSubmoduleUpdateCommand
submoduleUpdate()
Return a command object to execute asubmodule update
commandTagCommand
tag()
Return a command object to execute aTag
commandDeleteTagCommand
tagDelete()
Return a command object used to delete tagsListTagCommand
tagList()
Return a command object used to list tagsString
toString()
VerifySignatureCommand
verifySignature()
Return a command to verify signatures of tags or commits.static Git
wrap(Repository repo)
Wrap repository
-
-
-
Constructor Detail
-
Git
public Git(Repository repo)
Construct a newGit
object 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;null
is 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
Git
object 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
Git
object 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; seeRepository
for 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
Git
instance anymore.- Specified by:
close
in interfaceAutoCloseable
- Since:
- 3.2
-
cloneRepository
public static CloneCommand cloneRepository()
Return a command object to execute aclone
command- Returns:
- a
CloneCommand
used to collect all optional parameters and to finally execute theclone
command - 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 ainit
command- Returns:
- a
InitCommand
used to collect all optional parameters and to finally execute theinit
command - 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 aCommit
command- Returns:
- a
CommitCommand
used to collect all optional parameters and to finally execute theCommit
command - See Also:
- Git documentation about Commit
-
log
public LogCommand log()
Return a command object to execute aLog
command- Returns:
- a
LogCommand
used to collect all optional parameters and to finally execute theLog
command - See Also:
- Git documentation about Log
-
merge
public MergeCommand merge()
Return a command object to execute aMerge
command- Returns:
- a
MergeCommand
used to collect all optional parameters and to finally execute theMerge
command - See Also:
- Git documentation about Merge
-
pull
public PullCommand pull()
Return a command object to execute aPull
command- 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 aAdd
command- Returns:
- a
AddCommand
used to collect all optional parameters and to finally execute theAdd
command - See Also:
- Git documentation about Add
-
tag
public TagCommand tag()
Return a command object to execute aTag
command- Returns:
- a
TagCommand
used to collect all optional parameters and to finally execute theTag
command - See Also:
- Git documentation about Tag
-
fetch
public FetchCommand fetch()
Return a command object to execute aFetch
command- Returns:
- a
FetchCommand
used to collect all optional parameters and to finally execute theFetch
command - See Also:
- Git documentation about Fetch
-
push
public PushCommand push()
Return a command object to execute aPush
command- Returns:
- a
PushCommand
used to collect all optional parameters and to finally execute thePush
command - See Also:
- Git documentation about Push
-
cherryPick
public CherryPickCommand cherryPick()
Return a command object to execute acherry-pick
command- Returns:
- a
CherryPickCommand
used to collect all optional parameters and to finally execute thecherry-pick
command - See Also:
- Git documentation about cherry-pick
-
revert
public RevertCommand revert()
Return a command object to execute arevert
command- Returns:
- a
RevertCommand
used to collect all optional parameters and to finally execute thecherry-pick
command - See Also:
- Git documentation about reverting changes
-
rebase
public RebaseCommand rebase()
Return a command object to execute aRebase
command- Returns:
- a
RebaseCommand
used to collect all optional parameters and to finally execute therebase
command - See Also:
- Git documentation about rebase
-
rm
public RmCommand rm()
Return a command object to execute arm
command- Returns:
- a
RmCommand
used to collect all optional parameters and to finally execute therm
command - See Also:
- Git documentation about rm
-
checkout
public CheckoutCommand checkout()
Return a command object to execute acheckout
command- Returns:
- a
CheckoutCommand
used to collect all optional parameters and to finally execute thecheckout
command - See Also:
- Git documentation about checkout
-
reset
public ResetCommand reset()
Return a command object to execute areset
command- Returns:
- a
ResetCommand
used to collect all optional parameters and to finally execute thereset
command - See Also:
- Git documentation about reset
-
status
public StatusCommand status()
Return a command object to execute astatus
command- Returns:
- a
StatusCommand
used to collect all optional parameters and to finally execute thestatus
command - 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-remote
command- Returns:
- a
LsRemoteCommand
used to collect all optional parameters and to finally execute thestatus
command - See Also:
- Git documentation about ls-remote
-
clean
public CleanCommand clean()
Return a command object to execute aclean
command- Returns:
- a
CleanCommand
used to collect all optional parameters and to finally execute theclean
command - See Also:
- Git documentation about Clean
-
blame
public BlameCommand blame()
Return a command object to execute ablame
command- Returns:
- a
BlameCommand
used to collect all optional parameters and to finally execute theblame
command - See Also:
- Git documentation about Blame
-
reflog
public ReflogCommand reflog()
Return a command object to execute areflog
command- Returns:
- a
ReflogCommand
used to collect all optional parameters and to finally execute thereflog
command - See Also:
- Git documentation about reflog
-
diff
public DiffCommand diff()
Return a command object to execute adiff
command- Returns:
- a
DiffCommand
used to collect all optional parameters and to finally execute thediff
command - 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 add
command- Returns:
- a
SubmoduleAddCommand
used to add a new submodule to a parent repository
-
submoduleInit
public SubmoduleInitCommand submoduleInit()
Return a command object to execute asubmodule init
command- Returns:
- a
SubmoduleInitCommand
used 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 deinit
command- Returns:
- a
SubmoduleDeinitCommand
used to remove a submodule's working tree manifestation - Since:
- 4.10
-
submoduleStatus
public SubmoduleStatusCommand submoduleStatus()
Returns a command object to execute asubmodule status
command- Returns:
- a
SubmoduleStatusCommand
used to report the status of a repository's configured submodules
-
submoduleSync
public SubmoduleSyncCommand submoduleSync()
Return a command object to execute asubmodule sync
command- Returns:
- a
SubmoduleSyncCommand
used 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 update
command- Returns:
- a
SubmoduleUpdateCommand
used 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 aapply
command- Returns:
- a
ApplyCommand
used to collect all optional parameters and to finally execute theapply
command - Since:
- 2.0
- See Also:
- Git documentation about apply
-
gc
public GarbageCollectCommand gc()
Return a command object to execute agc
command- Returns:
- a
GarbageCollectCommand
used to collect all optional parameters and to finally execute thegc
command - 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.
-
-