Skip to content

Instantly share code, notes, and snippets.

@alexxxnf
Last active September 6, 2022 10:15
Show Gist options
  • Save alexxxnf/e68f1a9e6af9b497c8952fb6d10d728f to your computer and use it in GitHub Desktop.
Save alexxxnf/e68f1a9e6af9b497c8952fb6d10d728f to your computer and use it in GitHub Desktop.
How I use Git
# Normalize line endings by default
* text=auto
# Don't normalize executable scripts
*.bat text eol=crlf
*.sh text eol=lf
# Don't try to automatically merge lock files
yarn.lock -diff
package-lock.json -diff
poetry.lock -diff
[i18n]
filesEncoding = utf-8
[merge]
tool = meld
[diff]
guitool = meld
[core]
editor = \"C:/Program Files (x86)/GitExtensions/GitExtensions.exe\" fileeditor
autocrlf = False
longpaths = true
excludesfile = ~/.gitignore
[difftool "meld"]
path = C:/Program Files (x86)/Meld/Meld.exe
cmd = \"C:/Program Files (x86)/Meld/Meld.exe\" \"$LOCAL\" \"$REMOTE\"
[mergetool "meld"]
path = C:/Program Files (x86)/Meld/Meld.exe
cmd = \"C:/Program Files (x86)/Meld/Meld.exe\" --diff \"$BASE\" \"$LOCAL\" \"$REMOTE\" --output \"$MERGED\"
[pull]
rebase = true
[fetch]
prune = true
[log]
date = iso
.idea/
node_modules/
env/
*.py[cod]

Git stuff I frequently use

Push all branches from one remote (old) to another (origin)

git push -f origin 'refs/remotes/old/*:refs/heads/*'

Get commit messages between two commits

Between current master and previous master

git log --no-merges --format="%s" master^..master

Between the latest and second to last tag

git log --no-merges --format="%s" $(git tag | sort -Vr | head -2 | tail -1)..$(git tag | sort -Vr | head -1)

Lint new, modified but not committed Python files

Useful to check your changes before commiting them.

git ls-files -mo --exclude-standard '*.py' | xargs -r flake8

Lint Python files modified since branching off of origin/master:

git diff --name-only HEAD $(git merge-base origin/master HEAD) | grep '\.py$' | xargs -r flake8

Create a worktree and a branch

git worktree add ../config -b conf

Create a worktree and an orphan branch

Create a worktree and a branch as usual.

git worktree add ../config -b conf

Go to the directory.

cd ../config

Reset the state of the branch to the very first commit.

git reset --hard $(git rev-list --max-parents=0 HEAD)

Remove old files and add new ones.

Rewrite the only comment in the branch.

git add --all
git commit --amend -m 'Add nginx config'

Count commits by authors email

git log --pretty="%ae" --no-merges | sort | uniq -c | sort -nr -k 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment