Useful snippets

    Ivareta git historikk ved file renaming

    2023-10-22

    Ifølge https://koukia.ca/rename-or-move-files-in-git-e7259bf5a0b7

    Hvis du vil være sikker på at file history blir ivaretatt i en fil om du renamer den (eller flytter den), så må du bruke commando-linja og kjøre

    git mv mycoolclass.cs myCoolClass.cs
    

    eller

    git mv layout/CardList common/List
    

    Hvis du renamer direkte i editor-en, så kan du ikke være sikker på at git skjønner at det er samme fil.

    Når du ivaretar git history så kan du bruke ting som git log -p --follow -- path/to/filename. Se gist: https://gist.github.com/paalss/7127b421ff7f3a862ae8d76c772aeb6a

    Kommentarer:

    Mulig det er unødvendig å bruke mv. Da jeg renamet og flyttet layout/CardList til common/List så skjønte git at disse to filene var 74% like.

    git log -p --follow components/common/List/List.tsx funker fjell. Historikken strekker seg tilbake til før renamingen

    Links

    2023-10-22

    Links

    Oh my bash adding missing code completions

    2023-10-22

    source /usr/share/bash-completion/completions/git
    __git_complete gco _git_checkout
    __git_complete gb _git_branch
    __git_complete gbs _git_bisect
    __git_complete ga _git_add
    __git_complete gcmsg _git_commit
    __git_complete gcs _git_commit
    __git_complete gd _git_diff
    __git_complete gl _git_pull
    __git_complete gp _git_push
    __git_complete gm _git_merge

    Delete all branches listed by git branch -a

    2023-10-29

    Færre antall branches i code completion suggestion når du skriver git checkout feat-[Tab].

    Slett alle branches som ikke finnes på remote:

    git fetch -p

    (git fetch prune)

    Også, sørg for å slette unødvendige tags

    List tags

    git tag
    git tag -d tagName

    search for text appearance in a file's git history

    2023-11-05

    Eg. Search for "use-query-params" in package.json's git history

    git grep use-query-params $(git rev-list --all -- package.json) -- package.json

    As a function in .bashrc/.zshrc-file

    # search for word in a file's git history
    # usage: filehistorysearch "use-query-params" package.json
    

    filehistorysearch() { git grep $1 $(git rev-list --all -- $2) -- $2 }

    Revert changes introduced in old commit

    2025-08-11

    Reverse apply changes from old commit

    git checkout <commit-hash>
    git log # copy previous commit hash
    git diff <previous-commit-hash> > diff.patch
    git checkout <main-or-up-to-date-branch>
    git apply diff.patch --reverse

    Create new commit that revert last commit

    git revert <last-commit-hash>

    git hooks

    2026-02-25

    To activate pre-push, rename .git/hooks/pre-push.sample to .git/hooks/pre-push


    Example script:

    branch_name=$(git symbolic-ref HEAD 2>/dev/null)
    

    echo "$branch_name"

    if [ "$branch_name" != "refs/heads/min-branch" ]; then echo "You can't push this branch." # also prevents git push --force-with-lease exit 1 fi

    exit 0

    should work given that this command:

    git symbolic-ref HEAD 2>/dev/null

    gives: refs/heads/min-branch

    and:

    git symbolic-ref HEAD 2>/dev/null | cut -d"/" -f 3

    gives min-branch