You are currently viewing Useful Git Configuration

Useful Git Configuration

Introduction

The Git configuration file contains a number of variables that affect the Git commands’ behavior.
By default this file is located in the own user folder ~/.gitconfig.
VRequirements
Git Installed on Windows or Linux

Aliases explanation

In this section, we will provide a complete and exhaustive list of aliases to get a production boost in your code.

Basic Shortcuts

Very basic alias replacement of commonly used git commands
cl = clone
ci = commit
pl = pull
co = checkout
br = branch
cp = cherry-pick
r = reset
Show status concisely
st = status -s
Fetch branches and tags removing any remote-tracking references that no longer exist on the remote
f = fetch -apt
Incorporates changes from a remote repository into the current branch. Remove any remote-tracking references that no longer exist on the remote
pl = pull --prune
Create and checkout a new branch
cob = checkout -b
Checkout to previous branch
cop = checkout -
Add changes from all tracked and untracked files
ad = add -A
Commit with a message
cm = commit -m
Amend the previous commit
amend = commit --amend
ca = commit --amend
Amend the previous commit and leave the previous message unaltered
can = commit --amend --no-edit
Push commits from the local git repository to the origin or upstream remotes
ps = !git push origin HEAD
Push commits from the local git repository to the origin or upstream remotes and rewrite history
psf = !git push -f origin HEAD
Initial empty commit
empty = "!git commit -am\"[empty] Initial commit\" --allow-empty"
Blame a file
bl = blame
Clean working directory
cc = clean -dfx
Shelve files
shelve = stash --include-untracked
Unshelve files
unshelve = stash pop

Differences Between Files

Shows the changes between the working directory and the index
diff = diff --word-diff
Shows the changes between the index and the HEAD
dc = diff --cached
Shows the changes between the index and the last commit
dlc = diff --cached HEAD^
Shows changes from a revision ignoring whitespace when comparing lines
dr = "!f() { git diff -w "$1"^.."$1"; }; f"
Shows changes from a revision
diffr = "!f() { git diff "$1"^.."$1"; }; f"

Finding Files and Content Inside Files

Find a file path in codebase
ff = "!git ls-files | grep -i"
Find a pattern with ignore-case avoiding binary files
grep = grep -Ii
gr = grep -Ii

Show Metadata

Shows .gitconfig file
ec = config --global -e
List all your Aliases
la = "!git config -l | grep alias | cut -c 7-"

Reset Commands

Going back to the commit before HEAD. Leave changes in the staging area
r1 = reset HEAD^
Going back two commit before HEAD. Leave changes in the staging area
r2 = reset HEAD^^
Hard reset shortcut
rh = reset --hard
Going back to the commit before HEAD
rh1 = reset HEAD^ --hard
Going back two commit before HEAD
rh2 = reset HEAD^^ --hard

Branch Operations

Delete local branch
del = branch -D
Delete remote branch
delr = push origin --delete
List all local branches and sort them by commit date, showing the most recent git branch first, based on commits made to it
brls = branch --format='%(HEAD) %(color:yellow)%(refname:short)%(color:reset) - %(contents:subject) %(color:green)(%(committerdate:relative)) [%(authorname)]' --sort=-committerdate
List all remote branches and sort them by commit date, showing the most recent git branch first, based on commits made to it
brlsr = branch -r --format='%(HEAD) %(color:yellow)%(refname:short)%(color:reset) - %(contents:subject) %(color:green)(%(committerdate:relative)) [%(authorname)]' --sort=-committerdate

Show the History of Commits and Branches

List commits in short form, with colors and branch/tag annotations
ls = log --pretty=format:"%C(yellow)%h%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate
List commits showing changed files
ll = log --pretty=format:"%C(yellow)%h%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --numstat
List oneline commits showing relative dates
ld = log --pretty=format:"%C(yellow)%h\\ %ad%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --date=relative
A short git log
le = log --oneline --decorate
Shows the branches graph with date and contributor
lag = log --all --graph --pretty=format:'%C(yellow)%h%Creset -%C(bold blue)%d%Creset %s %Cgreen(%cr) %Cred<%an>%Creset'
Shows the branches graph
tree = !git log --graph --decorate --all --oneline

Show the History of a File, with Diffs

Show all the commits related to a file, with the diff of the changes
filelog = log -u
fl = log -u

References

You can check out the complete .gitconfig file into my Github profile.

Leave a Reply