Skip to content

git

git goodies

git branch -d `git branch | grep feature`                             # delete all branches with feature in its name
git branch | grep feature | xargs git branch -d                       # same as ^
git push origin --delete branchXYZ                                    # git push origin :branchXYZ
git push origin --delete `git branch -r | grep feature | cut -c10-`   # delete all feature branches from remote
git show branch:file > write_here                                     # https://stackoverflow.com/questions/7856416/view-a-file-in-a-different-git-branch-without-changing-branches
git checkout branch -- path/to/file                                   # similar to previous but checkout the file
git checkout --ours foo/bar.java                                      # useful for rebases or merges, works with --theirs too
git log -p -- src/data_capture_tools                                  # changes only made to a specific directory
git log --all --full-history -- "**/thefile.*"                        # https://stackoverflow.com/questions/7203515/how-to-find-a-deleted-file-in-the-project-commit-history
git log --left-right --graph --cherry-pick --oneline feature...branch # https://til.hashrocket.com/posts/18139f4f20-list-different-commits-between-two-branches
git diff main...                                                      # the PR diff - changes since I branched out from main

.. and ... notation

main: 🔴──🟠──🟢
            \
             🔵 B <- HEAD
git log main B
# 🟢 commit-main-3   (main)
# 🔵 commit-b-1      (HEAD -> B)
# 🟠 commit-main-2
# 🔴 initial commit1
git diff main..B
    diff main  B
    diff main    # (when you're on B)
# +🔵 commit-b-1
# -🟢 commit-main-3

git diff main...B
    diff main...
    diff $(git merge-base main B) B
# +🔵 commit-b-1
git log main..B
git log main..
git log ^main B
# 🔵 commit-b-1 (HEAD -> B)
# think of it as set difference: B \ main

git log main...B
git log main...
# 🟢 commit-main-3 (main)
# 🔵 commit-b-1 (HEAD -> B)
# think of it as the symmetric set difference: main Δ B