Skip to content

Instantly share code, notes, and snippets.

@vitalybe
Created February 25, 2015 12:28
Show Gist options
  • Save vitalybe/9efa951898af8c0d0912 to your computer and use it in GitHub Desktop.
Save vitalybe/9efa951898af8c0d0912 to your computer and use it in GitHub Desktop.
Awesome git aliases
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 D if you would like to DISCARD these changes or W to commit them as WIP: \";\
read dirty_operation;\
if [ \"$dirty_operation\" == \"D\" ]; 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"
pr-list = "!\
f() { \
pr -l;\
}; \
f"
pr-new = "!f() { \
branch_name=$(git symbolic-ref HEAD 2>/dev/null); \
branch_name=${branch_name##refs/heads/};\
pr -c -b $branch_name;\
}; \
f"
pr-open = "!f() { \
[ -z \"$1\" ] && echo "PR id required, e.g 211" && exit 1; \
pr -o --pr-id $1;\
}; \
f"
pr-review = "!\
f() { \
echo ---- Looking for PR SHA ----; \
trap 'echo ERROR: Operation failed; return' ERR; \
[ -z \"$1\" ] && echo "PR id required, e.g 211" && exit 1; \
local sha=$(pr --sha --pr-id $1);\
echo Found SHA: $sha;\
git pr-review-sha $sha;\
}; \
f"
pr-review-sha = "!\
f() { \
trap 'echo ERROR: Operation failed; exit 1;' ERR; \
[ -z \"$1\" ] && echo "SHA/branch required, e.g 73bd32f" && exit 1; \
echo ---- Fetching if required ----; \
git log $1 -n 1 > /dev/null 2>&1 || { echo Fetching...; git fetch; };\
git log $1 -n 1 > /dev/null 2>&1 || { echo Commit does not exist; exit 1; };\
branch_name=$(git symbolic-ref HEAD 2>/dev/null || echo none); \
branch_name=${branch_name##refs/heads/};\
if [[ \"$branch_name\" == \"temp/pr\" ]]; then\
echo ---- Moving current temp/pr branch to new PR ----;\
git safereset;\
git reset --hard $1;\
else\
echo ---- Making WIP commit if required ----; \
git commit -a --message='WIP - PR' > /dev/null && echo WIP Comitted || echo No commit required; \
echo ---- Create temporary branch at $1 ----; \
git branch -D temp/pr > /dev/null 2>&1 || true; \
git checkout -b temp/pr $1; \
fi;\
echo ---- Merging with origin/master ----; \
git merge origin/master; \
\
echo ---- Changes since origin/master ----; \
git pr-open $1;\
git difftool -d origin/master..HEAD &\
}; \
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"
# Creates a new branch for bug on origin/master
# Usage: bug https://github.com/angular/angularjs-batarang/issues/210
bug = "!f() { \
trap 'echo ERROR: Operation failed; return' ERR; \
\
new_branch=bug/$(echo $1 | sed -r \"s/.+\\///\");\
\
echo ---- Creating branch: $new_branch ----;\
git work $new_branch;\
}; \
f"
# Creates a new branch on latest origin/master
# Usage: work feature1
work = "!f() { \
trap 'echo ERROR: Operation failed; return' ERR; \
\
[ -z \"$1\" ] && echo "Branch parameter required, e.g git work feature1" && exit 1; \
git safereset;\
echo ---- Fetching ----; \
git fetch;\
\
echo ---- Creating branch: $1 ----;\
git checkout --no-track -b $1 origin/master;\
}; \
f"
push-new = "!f() { \
branch_name=$(git symbolic-ref HEAD 2>/dev/null); \
branch_name=${branch_name##refs/heads/};\
echo ---- Pushing to origin: $branch_name ----;\
git push -u origin $branch_name;\
}; \
f"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment