Skip to content

Instantly share code, notes, and snippets.

@Zoybean
Last active March 4, 2026 15:27
Show Gist options
  • Select an option

  • Save Zoybean/8db78966abea5d974934bb0e8e5f4e42 to your computer and use it in GitHub Desktop.

Select an option

Save Zoybean/8db78966abea5d974934bb0e8e5f4e42 to your computer and use it in GitHub Desktop.

Revisions

  1. Zoybean revised this gist Dec 30, 2017. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions fix-git
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,7 @@
    # Author: Zoey Llewellyn "Zobean" Hewll
    #
    # Usage: fix-git [REMOTE-URL]
    # Must be run from the root directory of the repository.
    # If a remote is not supplied, it will be read from .git/config
    #
    # For when you have a corrupted local repo, but a trusted remote.
    @@ -11,9 +12,9 @@
    # This does not affect your working tree.
    #
    # This does not currently work with submodules!
    # This will abort if a suspected submodule is found.
    # You will have to delete them first
    # and re-clone them after (with `git submodule update --init`)
    # This will abort if a suspected submodule is found
    #
    # Error codes:
    # 1: If a url is not supplied, and one cannot be read from .git/config
    @@ -61,7 +62,7 @@ then
    # print the piped input
    echo "${confirm}"
    fi
    if echo "${confirm}"|grep -Eq "[Yy]+([Ee]+[Ss]+)*" ; # it looks like a yes
    if echo "${confirm}"|grep -Eq "[Yy]+[EeSs]*" ; # it looks like a yes
    then
    if [[ -e .git ]] ;
    then
  2. Zoybean revised this gist Oct 6, 2017. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions fix-git
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@
    #
    # For when you have a corrupted local repo, but a trusted remote.
    # This script replaces all your history with that of the remote.
    # The old .git is backed up as .git_old, removing any old backups
    # If there is a .git, it is backed up as .git_old, removing the last backup.
    # This does not affect your working tree.
    #
    # This does not currently work with submodules!
    @@ -25,7 +25,7 @@ if [[ "$(find -name .git -not -path ./.git | wc -l)" -gt 0 ]] ;
    then
    echo "It looks like this repo uses submodules" >&2
    echo "You will need to remove them before this script can safely execute" >&2
    echo "Use \`git submodule update --init\` to re-clone them" >&2
    echo "Then use \`git submodule update --init\` to re-clone them" >&2
    exit 5
    fi

    @@ -35,7 +35,7 @@ then
    else
    if ! url="$(git config --local --get remote.origin.url)" ;
    then
    echo "Unable to find remote/origin: missing in '.git/config'" >&2
    echo "Unable to find remote 'origin': missing in '.git/config'" >&2
    exit 1
    fi
    fi
  3. Zoybean created this gist Oct 6, 2017.
    80 changes: 80 additions & 0 deletions fix-git
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,80 @@
    #!/bin/bash

    # Author: Zoey Llewellyn "Zobean" Hewll
    #
    # Usage: fix-git [REMOTE-URL]
    # If a remote is not supplied, it will be read from .git/config
    #
    # For when you have a corrupted local repo, but a trusted remote.
    # This script replaces all your history with that of the remote.
    # The old .git is backed up as .git_old, removing any old backups
    # This does not affect your working tree.
    #
    # This does not currently work with submodules!
    # You will have to delete them first
    # and re-clone them after (with `git submodule update --init`)
    # This will abort if a suspected submodule is found
    #
    # Error codes:
    # 1: If a url is not supplied, and one cannot be read from .git/config
    # 4: If the url cannot be reached
    # 5: If a git submodule is detected


    if [[ "$(find -name .git -not -path ./.git | wc -l)" -gt 0 ]] ;
    then
    echo "It looks like this repo uses submodules" >&2
    echo "You will need to remove them before this script can safely execute" >&2
    echo "Use \`git submodule update --init\` to re-clone them" >&2
    exit 5
    fi

    if [[ $# -ge 1 ]] ;
    then
    url="$1"
    else
    if ! url="$(git config --local --get remote.origin.url)" ;
    then
    echo "Unable to find remote/origin: missing in '.git/config'" >&2
    exit 1
    fi
    fi
    url_base="$(echo "${url}" | sed -E 's;^([^/]*://)?([^/]*)(/.*)?$;\2;')"
    echo "Attempting to access ${url_base} before continuing"
    if ! wget -p "${url_base}" -O /dev/null -q --dns-timeout=5 --connect-timeout=5 ;
    then
    echo "Unable to reach ${url_base}: Aborting before any damage is done" >&2
    exit 4
    fi

    echo
    echo "This operation will replace the local repo with the remote at:"
    echo "${url}"
    echo
    echo "This will completely rewrite history,"
    echo "but will leave your working tree intact"
    echo -n "Are you sure? (y/N): "

    read confirm
    if ! [ -t 0 ] ; # i'm open in a pipe
    then
    # print the piped input
    echo "${confirm}"
    fi
    if echo "${confirm}"|grep -Eq "[Yy]+([Ee]+[Ss]+)*" ; # it looks like a yes
    then
    if [[ -e .git ]] ;
    then
    # remove old backup
    rm -vrf .git_old | tail -n 1 &&
    # backup .git iff it exists
    mv -v .git .git_old
    fi &&
    git init &&
    git remote add origin "${url}" &&
    git config --local --get remote.origin.url | sed 's/^/Added remote origin at /' &&
    git fetch &&
    git reset origin/master --mixed
    else
    echo "Aborting without doing anything"
    fi