Yannick Weiss

Git

Clone with submodules

git clone --recursive giturl

Git sync

One of the alias I use most nowadays is git sync. It adds the remote changes to your current repo and pushes your changes to the remote, without having you to worry about the dirty worktree.

git config --global rebase.autoStash true
git config --global alias.sync '!git pull --rebase && git push'

If shit hits the fan abort with git rebase --abort and do a normal merge.

Reduce branch to one commit

One of the cleanest way to merge a feature branch.

git merge --squash <feature branch>
git commit

Before squashing I like to print out a reverse list of the changes, so I can write a summary for the squash commit.

git log --reverse --pretty=format:'* %s' master..branch-x

Ignore changes to a file

I use this to tell git when it should ignore a file that contains sensitive data. For example the file with the API keys.

git update-index --assume-unchanged <dir>
git update-index --no-assume-unchanged <dir>

Better diff

git config --global diff.compactionHeuristic true

Sign a release

Note: always use SemVer 2.0.0

git tag -s v0.1.1 -m "Release 0.1"
git push --follow-tags

Update folder from remote repo

git rev-parse HEAD > previous.hash
git fetch --all
git reset --hard origin/master

Reset repository

git fetch origin
git reset --hard origin

Aliases

I really love git undo it reverses your last commit, without losing the changes.

nuke = "!f(){ git branch -D \"$1\"; git push origin --delete \"$1\"; };f"
undo = reset --soft HEAD^
lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
st = status -sb
assume = update-index --assume-unchanged
unassume = update-index --no-assume-unchanged
# Show the files that are currently assume-unchanged
assumed = "!git ls-files -v | grep ^h | cut -c 3-"

Code Statistics

git log --since="6am" --author="Yannick Weiss" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -

Reset everything to the last commit

git reset --hard HEAD && git clean -xfd

Add relation between small fix and original commit

Commit the fix and mention the original hash. git commit --fixup HASH

Automatically fixing an older commit

You can use the command git fixup COMMIT to merge both changes, but it will rewrite the history.

[alias]
    fixup = "!f() { TARGET=$(git rev-parse "$1"); git commit --fixup=$TARGET ${@:2} && EDITOR=true git rebase -i --autostash --autosquash $TARGET^; }; f"

Restore deleted branch

Find the SHA1 for the commit at the tip of your deleted branch with git reflog. Create a new branch with that hash git checkout -b <branch> <sha>.

Git Bisect

git bisect start
git bisect good {{some-commit-hash}}
git bisect bad {{some-commit-hash}}
git bisect good/bad
git bisect reset
git bisect log

Migrate SVN to Git

This snippet is older and not up to date.

# migrate from svn to git
git svn clone URL --no-minimize-url --no-metadata --authors-file users.txt FOLDER
cd FOLDER
# remove svn remains
rm -r .git/svn
rm .git/refs/remotes/git-svn
git branch -d -r git-svn
git config --remove-section svn-remote.svn
git config --remove-section svn
# run garbage collection and tag the commit
git gc --aggressive --prune=now
git tag -a svn -m 'svn2git'