Class: Git::Remote

Inherits:
Object
  • Object
show all
Defined in:
lib/git/remote.rb

Overview

A remote in a Git repository

Remote objects provide access to remote metadata and operations like fetch, merge, and remove. They should be obtained via Git::Repository#remote, not constructed directly.

Examples:

Getting a remote

git = Git.open('.')
remote = git.remote('origin')
remote.fetch

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, name) ⇒ Remote

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

Use Git::Repository#remote instead of constructing directly

Initialize a new Remote object

Parameters:

  • base (Git::Repository)

    the git repository

  • name (String)

    the remote name (e.g. 'origin')



49
50
51
52
53
54
55
# File 'lib/git/remote.rb', line 49

def initialize(base, name)
  @base = base
  config = remote_repository.config_remote(name)
  @name = name
  @url = config['url']
  @fetch_opts = config['fetch']
end

Instance Attribute Details

#fetch_optsString?

The fetch refspec for this remote

Returns:

  • (String, nil)

    the fetch options string



37
38
39
# File 'lib/git/remote.rb', line 37

def fetch_opts
  @fetch_opts
end

#nameString

The name of this remote (e.g. 'origin')

Returns:

  • (String)

    the remote name



25
26
27
# File 'lib/git/remote.rb', line 25

def name
  @name
end

#urlString?

The URL of this remote

Returns:

  • (String, nil)

    the remote URL



31
32
33
# File 'lib/git/remote.rb', line 31

def url
  @url
end

Instance Method Details

#branch(branch = nil) ⇒ Git::Branch

Returns a Branch object for the given branch on this remote

Examples:

Get the remote-tracking branch object

git.remote('origin').branch('main')  #=> #<Git::Branch 'origin/main'>

Parameters:

  • branch (String) (defaults to: nil)

    the branch name on this remote (defaults to current branch)

Returns:

  • (Git::Branch)

    a branch object representing <remote>/<branch>



98
99
100
101
102
103
# File 'lib/git/remote.rb', line 98

def branch(branch = nil)
  branch ||= remote_repository.current_branch
  remote_tracking_branch = "#{@name}/#{branch}"
  branch_info = build_branch_info(remote_tracking_branch)
  Git::Branch.new(@base, branch_info)
end

#fetch(opts = {}) ⇒ String

Fetches from this remote

Examples:

Fetch from origin

git.remote('origin').fetch

Parameters:

  • opts (Hash) (defaults to: {})

    fetch options (see Git::Repository#fetch)

Returns:

  • (String)

    git's stdout from the fetch

Raises:



68
69
70
# File 'lib/git/remote.rb', line 68

def fetch(opts = {})
  remote_repository.fetch(@name, opts)
end

#merge(branch = nil) ⇒ String

Merges this remote into the given (or current) local branch

Examples:

Merge origin/main into the current branch

git.remote('origin').merge('main')

Parameters:

  • branch (String) (defaults to: nil)

    the local branch to merge into (defaults to current branch)

Returns:

  • (String)

    git's stdout from the merge

Raises:



83
84
85
86
87
# File 'lib/git/remote.rb', line 83

def merge(branch = nil)
  branch ||= remote_repository.current_branch
  remote_tracking_branch = "#{@name}/#{branch}"
  remote_repository.merge(remote_tracking_branch)
end

#removeGit::CommandLineResult

Removes this remote from the repository

Examples:

Remove the upstream remote

git.remote('upstream').remove

Returns:

Raises:



114
115
116
# File 'lib/git/remote.rb', line 114

def remove
  remote_repository.remote_remove(@name)
end

#to_sString

Returns the name of this remote as a string

Examples:

Get the remote name as a string

git.remote('origin').to_s  #=> 'origin'

Returns:

  • (String)

    the remote name



125
126
127
# File 'lib/git/remote.rb', line 125

def to_s
  @name
end