Home > Back-end >  Switching git branch with ruby-git does not switch the files?
Switching git branch with ruby-git does not switch the files?

Time:12-09

I am cloning a Github repo that has multiple branches, like for example:

root_dir    = Dir.mktmpdir(nil, Rails.root.join("repos").to_s)
root_folder = "ruby_git"
Git.clone("https://github.com/ruby-git/ruby-git.git", root_folder, :path => root_dir)

I then try to switch the branch to "test":

g = Git.open(full_dir)
if g.current_branch != "test"
  puts g.branch("test").checkout # => "Switched to branch 'test'"
end

The branch switch is confirmed. I even test myself if the branch is correct:

puts g.current_branch # => "test"

But when I finally check the files like this:

puts Dir["#{full_dir}/tests/*"] # => Wrong files that belongs to the default branch...

I see that the folder still contains the files of the default branch, not the branch that I switched to.

What am I doing wrong, and how can I fix it?

CodePudding user response:

Found the answer myself.

I need to pull the branch that I switched to:

g.pull("origin", "test")

The pull method is referenced here.

  • Related