credit: Kerryn Wood
My own git notes. See also the more universally acclaimed classic git tips.
See the fastai masterclass for many more handy helpful tips/links/scripts/recommendations.
Handy git commands
Remove a file from versioning without deleting:
echo .DS_Store >> .gitignore_global git config --global core.excludesfile $HOME/.gitignore_global
delete remote branch
git push <remote_name> --delete <branch_name>
legit simplifies feature branch workflows.
Filters: smudge and clean,
Long story. A useful one is stripping crap from jupyter notebooks.
Sub-projects inside other projects? External projects? The simplest way of integrating external projects is as subtrees. Once this is set up you can mostly ignore them.
Splicing a subtree onto a project
Con: Rebasin’ despite ’em is slow and involved.
Pruning off a sub-project
subtree split to prise out one chunk. It has a few wrinkles but is fast and easy.
Alternatively, to comprehensively rewrite history to exclude everything outside a subdir:
Include external projects as separate repositories within a repository is also possible, but I won’t document it here, since it’s well documented elsewhere and anyway I’m not a fan it always seems to cause many things to go wrong for me if I am not highly disciplined.
Download a sub-directory from a git tree
This works for github at least. I think anything running
- replace tree/master => trunk
- svn co the new url
svn co https://github.com/buckyroberts/Source-Code-from-Tutorials/trunk/Python
Deleting all tags
git tag -l | xargs -I %% git push -v origin :refs/tags/%% git tag -l | xargs git tag -d && git fetch -t
Not repeating yourself during merges
git rerere automates this:
git config --global rerere.enabled true git config --global rerere.autoupdate true
Importing some files across a branch
In brief, this will purge a lot of stuff from a constipated repo in emergencies:
Cleaning out all big files
bfg does that:
Deleting specific things
bfg also does this. There is also native support:
Making it work with a broken-permissioned FS
e.g. you are editing a git repo on NTFS via Linux and things are silly.
Detecting if there are changes to commit
if output=$(git status --porcelain) && [ -z "$output" ]; then # Working directory clean else # Uncommitted changes fi
Oh crap I’m leaving the office in a hurry and I just need to get my work into git ASAP for continuing on another computer. I don’t care about sensible commit messages because I am on my own private branch.
I put this little script in a file called
gitbang to automate the most common case.
#!/usr/bin/env bash if output=$(git status --porcelain) && [ -z "$output" ]; then echo "nothing to commit" else git add --all && git commit -m bang fi git pull && git push
Tools such as git-latexdiff provide custom diffing for, in this case, LaTeX code.
For sanity in git+jupyter, see jupyter.
See Git GUIs.
Which repo am I in?
For fish and bash shell, see bash-git-prompt.