Highlights of git shortcuts by Scott Chacon (YouTube)/More Available in Videos: Part 1, Part 2
.gitconfig
[user]
email = [email protected]
or add it with a command: git config --global user.email [email protected]
.
# The config can be loaded based on working path
[IncludeIf "gitdir:~/projects/work/"]
path = ~/projects/work/.gitconfig
Command | Description |
---|---|
git blame -L :example:path/to/file |
Shows commit author for line number of :example: |
git blame -w |
Ignore changes to spacing in the file |
git log -w |
Ignore changes to spaces in the logs |
git log -p -S --debug |
Reduces the list to commits with message --debug |
git diff --word-diff |
Shows word count instead of lines changed |
# Saves the setting
git config --global column.ui auto #1
git config --global branch.sort -committerdate
git branch
OR
# Optional way
git branch --column --sort=-committerdate #2
# This stops any changes overwriting the upstream repository
git push --force-with-lease
# Start with a clean copy by `restoring` it or the usual `checkout`
git restore file.txt
git checkout file.txt
OR
# Restore from x time ago
git restore --source HEAD@{2.hours.ago} file.txt
It's very useful to jump to when the project was working with this command.
# create a new key with ssh-keygen
git config gpg.format ssh
git config user.signingkey ~/.ssh/
Remember to add the public key to your upstream account.
git maintenance start
OR
# .gitconfig
[maintenance]
auto = false
strategy = incremental
A cron job will be created to prefetch per hour by default.
The prefetched references will not be loaded into the project until the changes are merged with git fetch
.
# To get more info on what prefetching does in the background
git for-each-ref | grep prefetch
strategy |
incremental |
---|---|
prefetch | hourly |
commit-graph | hourly |
git log --graph --oneline -10 >/dev/null
It is a very expensive feature because objects have to traverse through each commit to draw the tree. Prefetching will cache the tree in a hash map and load it when there's a call to draw the graph.
git config --global fetch.writeCommitGraph true
# Reuse Recorded Resolution
# Git will remember how to resolve similar merge conflicts
git config --global rerere.enabled true
# Repository creates a new branch by default on push
git config --global push.default current
# Add an alias to stash all changes
git config --global alias.staash 'stash --all'
# Run a command as a git alias
git config --global alias.[alias] ![command]
# .gitconfig
[alias]
staash = 'stash --all'
[rerere]
enabled = true
[push]
default = current