Skip to content

Instantly share code, notes, and snippets.

@LiutongZhou
Last active January 26, 2024 13:38
Show Gist options
  • Save LiutongZhou/b9a5267ba16e47e7eb1226bd69111742 to your computer and use it in GitHub Desktop.
Save LiutongZhou/b9a5267ba16e47e7eb1226bd69111742 to your computer and use it in GitHub Desktop.
Git Tips
  1. Squash commits into a single commit and rebase feature branch onto upstream/develop

    git fetch upstream && git rebase -i $(git merge-base feature_name upstream/develop)
  2. Cleanup git repository aggressively

    use bfg https://rtyley.github.io/bfg-repo-cleaner/

    java -jar bfg.jar --delete-files your_unwanted_files
    git for-each-ref --format="delete %(refname)" refs/original | git update-ref --stdin
    git reflog expire --expire=now --all && git gc --prune=now --aggressive
    git push --force
  3. Use git submodule

    git config submodule.recurse true  # Always use --recurse-submodules for every git command
    git submodule update --init --recursive  # Manual update
  4. Prune local branches that no longer exists on remote

    git remote prune <remote name such as origin>
  5. Checkout to a local branch (create if not exists) that tracks a branch in a different remote.

    git remote add <name your remote> git@...   # add a remote
    
    git fetch --all
    git checkout -b <local branch name such as <remote name>/<branch name>> <remote name>/<branch name>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment