Skip to content

Instantly share code, notes, and snippets.

@ahgraber
Last active December 14, 2022 16:15
Show Gist options
  • Save ahgraber/080045457a41c74ab8b4ba9ea5121778 to your computer and use it in GitHub Desktop.
Save ahgraber/080045457a41c74ab8b4ba9ea5121778 to your computer and use it in GitHub Desktop.
Git Cheats

Git cheatsheet & reminders

Add existing repo to local file structure

# at destination folder:
git init
git remote add origin <git repo https url>

Post-merge branch cleanup

# removes listed remote branches that are deleted on (remote) origin
git remote prune origin

or

### bash/zsh
# removes LOCAL branches that are deleted on (remote) origin
git fetch -p
git branch -vv | grep ': gone]'|  grep -v "\*" | awk '{ print $1; }' | xargs git branch -d

# may have to rerun with "-D" as last option to force delete
### powershell
# removes LOCAL branches that are deleted on (remote) origin
git fetch -p
git branch -vv | Select-String -Pattern ": gone]" | % { $_.toString().Trim().Split(" ")[0]} | % { git branch -D $_ }
# REF: https://dalehirt.com/2017/11/20/git-tip-deleting-old-local-branches-with-powershell/\
# REF: https://www.graef.io/clean-up-local-and-remote-branches/

Rebase to bring feature branch up to date with dev

See comparison between rebase and merge here

  1. First, try to merge develop branch into feature branch

    git merge features/12345-branch-name origin/develop
  2. If that doesn't work, try rebase (this may delete git history)

    git rebase
  3. If that doesn't work, create a new feature branch based on develop. Then create a PR to merge the old feature branch into the new feature branch.

Retroactively remove ignored files

# individual files
git rm --cached file1 file2 dir/file3

# bulk files
git rm --cached `git ls-files -i --exclude-from=.gitignore`
# or (for Windows)
git ls-files -i --exclude-from=.gitignore | xargs git rm --cached

# directories
git rm -r --cached ./dir

Remove file(s) from git history with git-filter-repo

ref [docs](https://www.mankier.com/1/git-filter-repo#Synopsis

  1. install git-filter-repo
  2. run:
    # after installing git-filter-repo
    git filter-repo --path <path to the file or directory> --invert-paths
    git push origin --force --all
  3. tell your collaborators to rebase

Git identity

# store user info
git config --global user.name <username>
git config --global user.email <email@address>

# store token (will preserve token in plaintext in ~/.gitcredentials)
git config --global credential.helper store

Git and .ipynb

Problem: Jupyter saves cell outputs and cell metadata. Git recognizes these as changes, and includes them in every commit and merge. Solution: Global filters to remove cell outputs and cell metadata as git operations occur.

Steps:

  1. Download 'jq' (and note where you saved it). For Windows, install with chocolatey.

  2. (*nix) Add the following to ~/.gitconfig

    [core]
       attributesfile = ~/.gitattributes_global
    
    [filter "nbstrip_full"]
       clean = "jq --indent 1 \
                '(.cells[] | select(has(\"outputs\")) | .outputs) = []  \
                | (.cells[] | select(has(\"execution_count\")) | .execution_count) = null  \
                | .metadata = {\"language_info\": {\"name\": \"python\", \"pygments_lexer\": \"ipython3\"}} \
                | .cells[].metadata = {} \
                '"
        smudge = cat
        required = true

    (windows) Add the following to C:\Users\<username>\.gitconfig. You may have to alter jq to the full path to your jq install location.

    [core]
        attributesfile = C:\\Users\\<username>\\.gitattributes_global
    
    [filter "nbstrip_full"]
        clean = "jq --indent 1 \
                '(.cells[] | select(has(\"outputs\")) | .outputs) = []  \
                | (.cells[] | select(has(\"execution_count\")) | .execution_count) = null  \
                | .metadata = {\"language_info\": {\"name\": \"python\", \"pygments_lexer\": \"ipython3\"}} \
                | .cells[].metadata = {} \
                '"
        smudge = cat
        required = true
  3. Add the following to ~/.gitattributes_global or C:\Users<username>.gitattributes_global:

    *.ipynb filter=nbstrip_full
  4. To force these changes on notebooks that are already created or have already been included in a repo, run git add --renormalize

    # refresh current directory
    git checkout .
    # see what will be updated by the filter
    git add --renormalize --dry-run
    # actually renormalize (this applies the "clean" process freshly to all tracked files to forcibly add them again to the index)
    git add --renormalize
  5. Debugging If adding new ipynbs or cloning a git repo and git (via VSCode) throws errors, try adding files manually. Commits can then be done via VSCode GUI

    git add <filename>

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment