Skip to content

Instantly share code, notes, and snippets.

@vitalybe
Last active September 12, 2018 17:46
Show Gist options
  • Save vitalybe/b895723a5e4b4f6ddfa2 to your computer and use it in GitHub Desktop.
Save vitalybe/b895723a5e4b4f6ddfa2 to your computer and use it in GitHub Desktop.
safereset = "!f() { \
trap 'echo ERROR: Operation failed; return' ERR; \
echo Making sure there are no changes...; \
last_status=$(git status --porcelain);\
if [[ $last_status != \"\" ]]; then\
echo There are dirty files:;\
echo \"$last_status\";\
echo;\
echo -n \"Enter Y if you would like to DISCARD these changes or W to commit them as WIP: \";\
read dirty_operation;\
if [ \"$dirty_operation\" == \"Y\" ]; then \
echo Resetting...;\
git reset --hard;\
elif [ \"$dirty_operation\" == \"W\" ]; then\
echo Comitting WIP...;\
git commit -a --message='WIP' > /dev/null && echo WIP Comitted;\
else\
echo Operation cancelled;\
exit 1;\
fi;\
fi;\
}; \
f"
switch = "!f() { \
trap 'echo ERROR: Operation failed; return' ERR; \
\
[ -z \"$1\" ] && echo "SHA/branch required, e.g 73bd32f" && exit 1; \
git safereset;\
echo ---- Checking out $1 ----; \
git checkout $1;\
echo ---- Restoring WIP if exists ----; \
last_commit=$(git log -1 HEAD --pretty=format:%s);\
[ \"$last_commit\" == \"WIP\" ] && { echo Reverting WIP commit... && git reset --soft HEAD~1; } || echo No WIP commit;\
}; \
f"
switch-back = "!f() { \
trap 'echo ERROR: Operation failed; return' ERR; \
\
echo ---- Checking out previous branch ----; \
git switch @{-1};\
}; \
f"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment