Skip to content

sohndotcloud/so-you-think-you-know-git

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 

Repository files navigation

So You Think You Know Git

Highlights of git shortcuts by Scott Chacon (YouTube)/More Available in Videos: Part 1, Part 2

Table of contents

Git Config Tips

.gitconfig

[user]
  email = [email protected]

or add it with a command: git config --global user.email [email protected].

Conditional

# The config can be loaded based on working path
[IncludeIf "gitdir:~/projects/work/"]
    path = ~/projects/work/.gitconfig

Useful Commands

Common Commands

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

New Commands

Show Branches in Columns

# 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

Safe Version of Force Push

# This stops any changes overwriting the upstream repository
git push --force-with-lease

Restore

# 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.

Commits with SSH Key Signatures

# 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 Prefetching

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
Default configurations
strategy incremental
prefetch hourly
commit-graph hourly

Visualizing the Commit Tree

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

Useful Default Global Configurations

# 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

About

Notes on what's new in Git

Resources

Stars

Watchers

Forks