Skip to content

Instantly share code, notes, and snippets.

@avimoondra
Last active August 18, 2021 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avimoondra/5c7c7763200f6a4ba5df00119e065a03 to your computer and use it in GitHub Desktop.
Save avimoondra/5c7c7763200f6a4ba5df00119e065a03 to your computer and use it in GitHub Desktop.

Git ops

  1. 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).
  2. Keep PRs small, merge early and often. Long running branches have to continually be rebased on a fast moving codebase.
  3. Avoid 3 levels of branching. 2 is ok, but can get confusing for yourself and reviewer.

Useful aliases

$ 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

Basic flow...

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

FAQ

(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

Scripts

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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment