Skip to content

Instantly share code, notes, and snippets.

@zaverden
Last active March 26, 2018 10:24
Show Gist options
  • Save zaverden/5104c3c38fa71871f4524db8808bece9 to your computer and use it in GitHub Desktop.
Save zaverden/5104c3c38fa71871f4524db8808bece9 to your computer and use it in GitHub Desktop.
remove a local copy of a remote branch and checkout the latest version
#!/bin/sh
#source "$(git --exec-path)/git-sh-setup"
targetBranch="$1"
# get current branch to return to it
branch=$(git branch | sed -n -e 's/^\* \(.*\)/\1/p')
# if current branch is target
if [ "$branch" = "$targetBranch" ]; then
if [ "--force" != "$2" ]; then
echo "Error: you try to recheckout current branch. Use --force"
exit 1
fi
# move to lates commit of origin (detached head)
commit=$(git rev-parse --verify origin/$targetBranch)
echo checkout $commit
git checkout $commit --quiet
fi
git branch -D $targetBranch
git fetch --prune
git checkout $targetBranch
@zaverden
Copy link
Author

usage:

git recheckout master --force

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment