Skip to content

Instantly share code, notes, and snippets.

@YuriyGuts
Last active August 29, 2015 14:24
Show Gist options
  • Save YuriyGuts/037b57bd7239f61d0824 to your computer and use it in GitHub Desktop.
Save YuriyGuts/037b57bd7239f61d0824 to your computer and use it in GitHub Desktop.
Update all Git repositories in the current folder, undoing all local changes if needed
#!/usr/bin/env python
import os
import subprocess
import sys
confirmation = raw_input("This script may DISCARD YOUR UNCOMMITTED CHANGES. Are you sure (Y/N)? ")
if confirmation.lower() != "y":
sys.exit(1)
workdir = os.getcwd()
repos = [os.path.join(workdir, directory)
for directory in os.listdir(workdir)
if os.path.isdir(directory) and os.path.isdir(os.path.join(workdir, directory, ".git"))]
for repo in repos:
print "-" * 30
print "Updating {0}...".format(os.path.basename(repo))
os.chdir(repo)
exitcode = subprocess.call("git pull --ff-only || (git reset --hard ; git checkout -- . ; git ls-files -o | xargs rm ; git pull -f)", shell=True)
os.chdir("..")
print ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment