Ever spent neglected to update your git files because there were too many to deal with? My cool coworker Tom Czarniecki showed me this cool script that allows him to update all his git files at once. Here’s how it works!

Step 1: the Ruby script

Open your text editor of choice, create a new file, and save it as gitupdatescript.rb (or another filename of your choice) in the root of your folder containing git files. Copy and paste the following code into the file:

#!/usr/bin/env ruby
require 'fileutils'

Dir.glob('**/.git').each do |path|
  next if File.file?(path) # don't process submodules
  start_time = Time.now
  project_base = File.expand_path("#{path}/..")
  puts "In #{project_base}"
  FileUtils.cd(project_base) do
    system('git stash')
    puts "Update failed" unless system('git pull --rebase')
    system('git stash pop')
  end
  end_time = Time.now
  puts "Done in #{end_time - start_time} sec"
  puts
end

Step 2: turn the file into an executable

We’ll now be using the command line to allow this file to modify our computer’s files. I use a Mac, so please be sure to translate to other languages as needed (and let me know if it works!). cd to the folder containing the ruby file we created and enter the following code:

chmod +x gitupdatescript.rb

If your ruby file is located elsewhere, include the path to the file in your command.

Step 3: execute your new script

To run your script, just type ./gitupdatescript.rb and it should start updating git files!

Thanks for reading! If you liked this piece, please share it on your favorite social media platforms or send it to a friend. You can also buy me a cup of tea on Ko-fi.

Know when the next post drops

Sign up for my email newsletter to get updates from me, directly in your inbox. I'll contact you a couple times per month at maximum!