- Use Squash and Merge on Github, which uses rebase. Rebasing keeps history linear. Linear history is easy to read, understand, and makes operations easier (like reverts).
- Keep PRs small, merge early and often. Long running branches have to continually be rebased on a fast moving codebase.
- Avoid 3 levels of branching. 2 is ok, but can get confusing for yourself and reviewer.
$ git aliases
aa=add --all
co=checkout
br=branch
ci=commit
st=status
ca=commit --amend
can=commit --amend --no-edit
cp=cherry-pick
co-pr=!sh -c 'git fetch origin pull/$1/head:pr/$1 && git checkout pr/$1' -
dmb=delete-merged-branches
pr=!hub pull-request
aliases=!git config --list | grep alias | cut -c 7-
Set up hub
git co main
git pull
git co -b avimoondra/my-new-feature
git add . # adds all changes to staging area
git add -p # step through changes file by file
git ci -m "add button + modal for my new feature"
git push origin head
hub pull-request -b main
git co - # like cd -
git pull
(1) I want to keep my branch up to date with main
.
git co avimoondra/my-new-feature
git pull --rebase orgin main
Or...
git co main
git pull
git co avimoondra/my-new-feature
git rebase main # resolve conflicts
git push origin head -f # careful! this re-writes history. use reflog if something didn't go according to plan.
(2) I want to undo last commit (into staging area)
git reset head^ # ^^^^ for undoing more commits.
(3) I want to see what's changed, but not commit anything. Use --cached
if you want to see diffs for what's already been staged.
git diff
(4) I want to reset my branch to the origin (Github) version.
git reset --hard origin/avimoondra/my-new-feature
(5) I need to completely reset my branch (and start from scratch).
git co main
git br -D avimoondra/my-new-feature # deletes your branch locally.
git co pull
git co -b avimoondra/my-new-feature
(6) I want to see what's going on right now
git st
(7) I want to see what's happened in the past
git log
git-spinoff, usage git spinoff some/new-feature
#!/bin/bash
# This will probably cause me to lose work one day.
if ! [[ $# -eq 1 ]]
then
echo >&2 'Usage: `git spinoff <new branch>`'
fi
NEWBRANCH="$1"
UPSTREAM=$(git upstream)
if [[ -z "$UPSTREAM" ]]
then
echo >&2 "Current branch has no upstream"
exit 1
fi
echo "Attempting to move the following commits to '$NEWBRANCH':"
git --no-pager log --oneline "$UPSTREAM"..
if ! git branch "$NEWBRANCH"
then
echo >&2 "Failed to create branch '$NEWBRANCH'"
exit 1
fi
git reset --hard "$UPSTREAM"
git checkout "$NEWBRANCH"