Skip to content

Instantly share code, notes, and snippets.

@loncarales
Last active April 9, 2024 11:57
Show Gist options
  • Save loncarales/0086bd078965056d0a75775c8bdaf266 to your computer and use it in GitHub Desktop.
Save loncarales/0086bd078965056d0a75775c8bdaf266 to your computer and use it in GitHub Desktop.
A bunch of essential snippets for Git
[user]
name = user_name
email = user_email
[apply]
# Detect whitespace errors when applying a patch
whitespace = fix
[core]
editor = nvim
autocrlf = input
safecrlf = true
pager = diff-so-fancy | less --tabs=4 -RFX
[interactive]
diffFilter = diff-so-fancy --patch
[color]
ui = true
[color "branch"]
current = yellow reverse
local = yellow
remote = green
[color "diff"]
meta = 11
frag = magenta bold
old = red bold
new = green bold
func = 146 bold
commit = yellow bold
whitespace = red reverse
[color "diff-highlight"]
oldNormal = red bold
oldHighlight = red bold 52
newNormal = green bold
newHighlight = green bold 22
[color "status"]
added = yellow
changed = green
untracked = cyan
[merge]
keepBackup = false
tool = p4merge
[mergetool]
prompt = false
[mergetool "p4merge"]
cmd = p4merge "$BASE" "$LOCAL" "$REMOTE" "$MERGED"
keepTemporaries = false
trustExitCode = false
keepBackup = false
[diff]
tool = p4merge
[difftool]
prompt = false
[difftool "p4merge"]
cmd = p4merge "$LOCAL" "$REMOTE"
keepTemporaries = false
trustExitCode = false
keepBackup = false
[push]
default = current
[branch]
autoSetupMerge = inherit
[pull]
rebase = true
[rebase]
autoStash = true
[commit]
template = ~/.git-commit-template.txt
#!/bin/bash
protected_branch='master'
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
if [ $protected_branch = $current_branch ]
then
read -p "You're about to push master, is that what you intended? [y|n] " -n 1 -r < /dev/tty
echo
if echo $REPLY | grep -E '^[Yy]$' > /dev/null
then
exit 0 # push will execute
fi
exit 1 # push will not execute
else
exit 0 # push will execute
fi
# Make sure you're adding under the [alias] block.
[alias]
st = status
s = status -s -b
a = add
ap = add -p
c = commit --verbose
ca = commit -a --verbose
cm = commit -m
cam = commit -a -m
m = commit --amend --verbose
d = diff
ds = diff --staged
dch = diff --name-status -r
dca = diff --cached
co = checkout
cob = checkout -b
fo = fetch origin
po = push origin
m = merge
mstop = merge --no-commit
mff = merge --no-ff
mffstop = merge --no-ff --no-commit
pi = cherry-pick -x
pigo = cherry-pick --continue
pistop = cherry-pick -x --no-commit
# push all
pushitgood = !echo 'Ah, push it' && git push -u origin --all && echo 'P-push it real good'
# Undo clashes with git-extras
rh = reset --hard
unstage = reset HEAD --
# delete merged branch
cleanmerged = !git branch --merged | grep -v \"\\*\" | xargs -n 1 git branch -d
# When used without any argument it will print all changes since the last tag (changelog clashes with git-extras)
changes = "!f() { r=${1:-`git describe --tags --abbrev=0`..HEAD}; echo Changelog for $r; git log --reverse --no-merges --format='* %s' $r; }; f"
## Logs ##
##########
# one-line log
l = log --pretty=format:"%C(yellow)%h\\ %ad%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --date=short
hist = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative
tree = log --oneline --decorate --graph
last = log -1 HEAD
new = !sh -c 'git log $1@{1}..$1@{0} "$@"'
whois = "!sh -c 'git log -i -1 --pretty=\"format:%an <%ae>\n\" --author=\"$1\"' -"
whatis = show -s --pretty='tformat:%h (%s, %ad)' --date=short
# list branches sorted by last modified
b = "!git for-each-ref --sort='-authordate' --format='%(authordate)%09%(objectname:short)%09%(refname)' refs/heads | sed -e 's-refs/heads/--'"
# <type>[optional scope]: <description>
#
# [optional body]
#
# [optional footer(s)]
# Keep each commit line under 50 characters
# Following conventional commits 1.0.0
#
# Use the prefix naming convention:
# fix: patches a bug in the codebase
# feat: introduces a new feature to the codebase
# refactor: only used when refactoring code
# docs: used when modifying/creating docs
# build: when preparing for a build
# style: formatting, missing semi colons, etc; no code change
# test adding missing tests, refactoring tests; no production code change
# chore: updating grunt tasks etc; no production code change
#
# Commits should follow the format below:
#
# [Title]
# <type>[optional scope]: In under 50 characters, description of the commit
# Scope is just the scope of the change. Something like (admin) or (teacher).
#
# [Body]
# Explain in detail what was done in this commit and
# what this is supposed to achieve. Keep this section
# under 72 characters per line.
#
# [Footer]
# Use this section to add any PR/Ticket numbers that this
# commit is solving or any co-authors. If none of these apply, # leave it empty.

Git-Flow Cheatsheet

References

Initialize a Repository for git-flow

git flow init -d

(Omit -d if you want to select values other than the defaults.)

Features

Start a New Feature

This creates a new branch based on develop and switches to it:

git flow feature start FEATURENAME

Finish a Feature

This merges the feature into develop, removes the feature branch, and switches to develop:

git flow feature finish FEATURENAME

Publish a Feature

Push a feature branch to remote repository:

git flow feature publish FEATURENAME

Get a feature published by another user from remote repository:

git flow feature pull origin FEATURENAME

Releases

Start a Release

Create release branch from develop:

git flow release start RELEASENAME

Publish release branch:

git flow release publish RELEASENAME

Create a local tracking branch for a remote release:

git flow release track RELEASENAME

Finish a Release

Merge release branch into master, tag it, merge back into develop, and remove the release branch:

git flow release finish RELEASENAME
git push --tags

Hotfixes

Start a Hotfix

Create hotfix branch from master:

git flow hotfix start VERSIONNAME

Create hotfix branch from some other commit:

git flow hotfix start VERSIONNAME BASENAME

Finish a Hotfix

Merge hotfix back into develop and master, and tag:

git flow hotfix finish VERSIONNAME

Git Housekeeping: clean-up outdated branches in local and remote repositories

Local branches

At first, list all local branches:

$ git branch

We need to know what branches are already merged in “development” and can be easily removed:

$ git checkout development
$ git branch --merged

Now, remove all outdated branches with:

$ git branch -d old-merged-feature

Next, decide what to do with not merged branches:

$ git branch --no-merged

If some of them is just abandoned stuff that you don’t need anymore, remove it with “-D” option:

$ git branch -D old-abandoned-feature

References to remote branches

After each git pull or git fetch command Git creates references to remote branches in local repository, but doesn’t clean up stale references. List referenced remote branches:

$ git branch -r

Clean-up outdated references:

$ git remote prune origin

Tip

Update repository with (and Git automatically prunes all stale references):

$ git fetch -p

Remote branches

Usually, remote repository is a big garbage heap of stale branches, if there is no responsible housekeeping person. After previous git remote prune origin we should have synched list of remote branches.

At first, we can find branches which are already merged in “development””:

$ git checkout development
$ git branch -r --merged

But this command does not provide much information. What if this branch is merged, but still used for feature development. Would be cool to know last commit date and author.

This magic snippet provides all required information:

$ for branch in `git branch -r --merged | grep -v HEAD`; do echo -e `git show --format="%ci %cr %an" $branch | head -n 1` \\t$branch; done | sort -r

Now, you can delete own remote branches, and ask other authors to clean-up theirs:

$ git push origin --delete branch-name

Similar snippet for not merged branches:

$ for branch in `git branch -r --no-merged | grep -v HEAD`; do echo -e `git show --format="%ci %cr %an" $branch | head -n 1` \\t$branch; done | sort -r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment