Git - a few tricks

A few git Aliases

When/What to commit

  • Atomic - shouldn't break your branch
  • Small enough to review on its own
  • git add -p is your friend!

 

If you did some changes that would fit better in a previous commit:

  • Stash or commit your current changes
  • git rebase -i <the commit before the one to change>
  • Edit the commit you wnat to change, then git commit --amend
  • finally git rebase --continue

Quick side notes

Did you ever had to fix some indentation? Like, a lot of it?

Appending ?w=1 to a change in GitHub will discard spacing changes.

If you create a separate commit for spacing, the reviewer shouldn't see any changes with ?w=1 on, which is handy as GitHub won't let you comment with it on anyway

You might have heard about git bisect.
In a nutshell it's a tool to help you find at what commit a bug was introduced.

It obviously works best on small atomic commits

Hooks!

There are a lot of possible places to hook:

ls -la .git/hooks/
applypatch-msg.sample   fsmonitor-watchman.sample  pre-applypatch.sample
pre-push.sample         pre-receive.sample         update.sample
commit-msg.sample       post-update.sample         pre-commit.sample          pre-rebase.sample       prepare-commit-msg.sample

You could use commit-msg to auto prepend your branch name.

You could use pre-commit to verify that the modify files compile.

(I didn't write my own yet - sorry...)

You can create global hooks for all your repos:

git config --global init.templatedir 'path/to/the/folder/you/created'
git init // on all projects you want it enabled

Various tricks

Git stash

here are a few commands for stash:

 

pop

drop
save "some message"

list

show stash@{#}

Index cheatsheet

Reflog

... Or the "Oooops I screwed up" saver

It basically gives you the history of where your local copy was

It allows you to revert whatever Git changes you've made, including reverts.

git reset --hard HEAD@{4}

Rebase workflow

Thanks to Ghislain Rodriguez - most of this is based on his previous work.

 

You can find his slides here:

https://slides.com/ghislainrodrigues-1/

Git - a few tricks

By fguery

Git - a few tricks

A few advanced tricks about GIt like stash, reflog, hooks, ...

  • 282