-
-
Save sirhopcount/4230983 to your computer and use it in GitHub Desktop.
Git Workflow
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
### git aliases | |
# shortcut for git-add hunk staging | |
alias gap='git add --patch' | |
alias gmup='master_update' | |
alias gmpu='master_push' | |
alias gsyn='sync_repo' | |
alias gbcr='branch_create' | |
alias gbup='branch_update' | |
alias gblg='branch_log' | |
alias gbdf='branch_diff' | |
alias gbmg='branch_merge' | |
### git functions | |
master_update() { | |
local CURRENT_BRANCH="$(git symbolic-ref -q HEAD)" | |
CURRENT_BRANCH="${CURRENT_BRANCH##refs/heads/}" | |
local WORKING_DIR="$PWD" | |
cd $(git rev-parse --show-toplevel) | |
git checkout master | |
git pull --rebase | |
git checkout "$CURRENT_BRANCH" | |
cd "$WORKING_DIR" | |
} | |
master_push() { | |
local CURRENT_BRANCH="$(git symbolic-ref -q HEAD)" | |
CURRENT_BRANCH="${CURRENT_BRANCH##refs/heads/}" | |
local WORKING_DIR="$PWD" | |
cd $(git rev-parse --show-toplevel) | |
git checkout master | |
git push | |
git checkout "$CURRENT_BRANCH" | |
cd "$WORKING_DIR" | |
} | |
sync_repo() { | |
master_update | |
master_push | |
} | |
branch_create() { | |
if [[ -z $1 ]]; then | |
echo 'Usage: branch_create <branchname>' | |
echo 'Creates a new branch head named <branchname> which points to master' | |
else | |
git checkout -b "$1" master | |
fi | |
} | |
branch_update() { | |
master_update | |
git rebase master | |
} | |
# most commonly used; squash all branch commits into one | |
branch_merge() { | |
local CURRENT_BRANCH="$(git symbolic-ref -q HEAD)" | |
CURRENT_BRANCH="${CURRENT_BRANCH##refs/heads/}" | |
local WORKING_DIR="$PWD" | |
if branch_update; then | |
cd $(git rev-parse --show-toplevel) | |
git checkout master | |
# validate that the merge happens since we have to forcefully delete the branch | |
if git merge --ff-only --squash "$CURRENT_BRANCH"; then | |
git commit -v -e -m "$(echo "Squashed commit of the following:\n\n$(git log master..${CURRENT_BRANCH} --pretty=format:' * %ad %h %s' --date=short)")" | |
git branch -D "$CURRENT_BRANCH" | |
cd "$WORKING_DIR" | |
fi | |
fi | |
} | |
# used for bigger branches; allows you to manually squash/fixup | |
# branch commits into smaller, more logical/manageable chunks | |
big_branch_merge() { | |
local CURRENT_BRANCH="$(git symbolic-ref -q HEAD)" | |
CURRENT_BRANCH="${CURRENT_BRANCH##refs/heads/}" | |
local WORKING_DIR="$PWD" | |
if branch_update; then | |
git rebase --interactive --autosquash master | |
cd $(git rev-parse --show-toplevel) | |
git checkout master | |
git merge --ff-only "$CURRENT_BRANCH" | |
git branch -d "$CURRENT_BRANCH" | |
cd "$WORKING_DIR" | |
fi | |
} | |
branch_log() { | |
local CURRENT_BRANCH="$(git symbolic-ref -q HEAD)" | |
CURRENT_BRANCH="${CURRENT_BRANCH##refs/heads/}" | |
echo "Commits in branch \"${CURRENT_BRANCH}\", but not \"master\":" | |
git log master..${CURRENT_BRANCH} --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset' --abbrev-commit --date=relative | |
} | |
branch_diff() { | |
local CURRENT_BRANCH="$(git symbolic-ref -q HEAD)" | |
CURRENT_BRANCH="${CURRENT_BRANCH##refs/heads/}" | |
echo "Commits in branch \"${CURRENT_BRANCH}\", but not \"master\":" | |
git diff master..${CURRENT_BRANCH} | |
} |
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
[alias] | |
br = branch | |
ci = commit --verbose | |
co = checkout | |
dstat = diff --stat | |
fpatch = format-patch --patience --full-index -M -C | |
lo = log --all --date-order --oneline --graph --decorate -M -C | |
st = status --short --branch | |
staged = diff --cached | |
ribbon = tag --force _ribbon origin/master | |
catchup = log --patch --reverse --topo-order _ribbon..origin/master |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment