Skip to content
way-to-code edited this page Oct 1, 2019 · 1 revision

Initial configuration

By the default git path is hard-coded to "/usr/bin/git". If you want to use a different path, you may call the following method as soon as possible until any work with git is made:

// Installs a custom path to `git` executable instead of the default "/usr/bin/git"
GitRepository.installExecutablePath("/path/to/git/on/disk")

Initialisation

You may initialise a new repository with two ways:

  • Clone an existing repository from a remote like GitHub;
  • Specify a path to an existing local repository on the disk.

Remote repository

let url: URL = URL(string: "https://github.com/way-to-code/git-macOS.git")
let repository = GitRepository(from: url!)

Local repository

let localPath = "/Users/example/projects/github/git-macOS.git"
let repository = try GitRepository(at: localPath)

Credentials

As for now Git.framework uses simple username/password authentication. You may provide a username/password like this:

let credentials = GitCredentialsProvider(username: "user", password: "password")
let repository = GitRepository(from: url, using: credentials)

However, this approach is not recommended as the specified username and password will be shown in a remote url. Better approach is use git-credential-osxkeychain. You may read about it here.

Git.framework can use no credentials at all, but to rely on the integrated git mechanism for using credentials from Mac Keychain. In this case you do not need to specify any credentials at all, just to set credentials in Keychain like described above.

When on a single Mac different users are used for the same domain, you may want to specify a username to let git know for which username to ask password in keychain.

// Do not specify a password at all, to let `git-credential-osxkeychain` know which user is intended to be used
let credentials = GitCredentialsProvider(username: "user")
let repository = GitRepository(from: url, using: credentials)
Clone this wiki locally