10 things you didn't know you could do with git (but should!)
git log
comes in many flavours
1. git log --graph
git log --oneline
combine them! git log --graph --oneline
co-author
your commits
2. take the stress out of pairing: git commit -m "Refactor component
Co-authored-by: name <name@example.com>"
git add --patch
3. stage that hunk with short version: git add -p
The options for each hunk:
y - stage this hunk
n - do not stage this hunk
q - quit; do not stage this hunk or any of the remaining ones
a - stage this hunk and all later hunks in the file
d - do not stage this hunk or any of the later hunks in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
k - leave this hunk undecided, see previous undecided hunk
K - leave this hunk undecided, see previous hunk
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print help
git reset
4. don't panic! hit undo with git reset HEAD <filename> // unstages specified file from your staging index
git reset // unstages all files from your staging index
git checkout
does more than switch branches
5. git checkout master // switch to master branch
rm -f index.js // delete a file in your working tree - oops!
git checkout index.js // restore that file from the branch index
git checkout mybranch // switch to new branch
// make some changes to index.js
// commit those changes
// think better of it and decide to undo all those changes
git checkout master index.js // restore that file from master branch
git bisect
6. get to the bottom of bugs with Try it out here:
https://github.com/salcode/git-bisect-example
https://salferrarello.com/how-to-use-git-bisect
git rebase
7. expand the possibilities: understand git flow options:
- Merge master into your branch
- Rebase master onto your branch
8. keep your commit history slick with interactive rebasing
git commit --amend
which lets you edit the last commit on a branch
Before trying out interactive rebase, learn to use Warning: Never rebase a branch someone else has pulled and is also working on. Only safe to use in "one branch, one owner" situations
git rebase -i HEAD~[number]
- can go back as many commits as you like, but start small!
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# . create a merge commit using the original merge commit's
# . message (or the oneline, if no original merge commit was
# . specified). Use -c <commit> to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
git reflog
9. don’t panic part 2! there is always eg. the ctrl-Z
of git on your local machine
10. hate the command line? git travels!
just set and use your favourite IDE's GUI interface for version control
awesome git resources
- git cheatsheet
http://ndpsoftware.com/git-cheatsheet.html#loc=workspace;
- Getting more from Git / Alice Bartlett / ffconf 2019
https://www.youtube.com/watch?v=FQ4IdcrOUz0&list=PLXmT1r4krsTo5KtThq4dATD_ctsV8mdJQ&index=4&t=0s