Last active
March 26, 2018 10:24
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
usage: