Skip to content
Chrisman Brown edited this page Jul 3, 2019 · 14 revisions

git

Get good at git

shortcuts

I use the shortcuts provided by the oh-my-zsh git plugin.

commit messages

Don't be this guy: https://xkcd.com/1296/

Use emoji

Use good formatting

Pre-commit checklist:

  • Only related/necessary changes/files are included.
  • Unrelated/unnecessary changes/files are removed.
  • There are no typos. (Run a spell checker.)
  • All comments are still up to date.
  • Remove all console logs/debugging code.
  • Double check that all requirements are met.
  • All tests are passing.
  • There are no linter errors.
  • All edge cases are covered.
  • Have I verified that the code meets the requirements?

situations and use-cases

Often and not-often used git commands.

interactive add

use: approve/stage each hunk. This will prevent acidentally committing console.logs, etc.

  • ga -i for interactive staging/patching.
  • ga -p to drop straight into patch mode.

interactive rebase

use: squash, reorder, and edit commits. general rewriting of time

to change last N commits:

git rebase -i HEAD~N

made changes to the wrong branch

Haven't committed yet:

  1. git stash - stash your changes
  2. gco -b feature/branch - branch off of the current branch
  3. git stash pop - pop your changes off the stack into the current branch

Have committed:

  1. git branch feature/branch - branch off the current branch
  2. git reset --hard HEAD^ - undo the last commit
  3. gco feature/branch

Edit the last commit msg

gc --amend

Added wrong files to last commit

  1. git reset --soft HEAD~1
  2. look at gd --staged and git reset the files you did not want to include in your commit.
  3. gc -c ORIG_HEAD will commit with the original commit message.
  4. You now have uncommited changes to either include in the next commit, or to discard with gco <file>.

Move a commit to another branch

e.g., committed to development, meant to commit to feature/xyz.

  1. Get the sha of the commit from git log on development.
  2. gco feature/xyz and git cherry-pick <sha>. You may have to resolve conflicts here.
  3. gco development and git reset --hard HEAD^
  4. Finally, gco feature/xyz and continue working.

Excluding local files

Sometimes I want to keep a notes file in a project, but I don't want to commit that file to the .gitignore or anything.

echo "notes" >> ./.git/info/exclude

Resources:

Clone this wiki locally