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>

    compare branch with main

    2025-10-31

    CLI

    git diff main..HEAD # does not include uncommitted changes

    git diff main..$(git branch --show-current) # does not include uncommitted changes

    git show $(git branch --show-current)

    git branch --show-current is current branch

    Lazygit

    1. Highlight main branch
    2. Shift w & hit enter
    3. highlight the branch you want to compare with

    Webstorm

    1. GitToolBox? Should not be necessary I think

    https://useful-snippets.netlify.app/posts/webstorm-compare-with-branch/

    image

    VS Code

    1. Gitlens?

    Neovim

    1. Diffview

    prepush / git hooks

    2026-02-25

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


    Example script:

    GREEN='\033[0;32m'
    RED='\033[1;91m'
    RESET='\033[0m'
    

    branch_name="$(git branch --show-current)" allowed_branch_name="my-branch"

    if [ "$branch_name" != "$allowed_branch_name" ]; then echo "Current branch: ${RED}${branch_name}${RESET}" echo "Allowed branch: ${GREEN}${allowed_branch_name}${RESET}" echo echo "You can't push directly to this branch." exit 1 fi

    Insert the current branch into the top of the pre-push file

    sed -i "" "s&An example&$(git branch --show-current)\n# An example&" .git/hooks/pre-push

    Replace "An example" with current branch \n # An example

    Copy current branch to Mac clipboard

    echo $(git branch --show-current) | pbcopy

    From vim:

    :!echo $(git branch --show-current) | pbcopy

    rename branch locally & remote

    2026-04-22

    notes:

    1. rename in lazygit
    2. git push $remote --delete $old_name
    3. git push origin HEAD

    This will close any existing PR if exists


    # Source - https://stackoverflow.com/a/30590238
    # Posted by CodeWizard, modified by community. See post 'Timeline' for change history
    # Retrieved 2026-04-22, License - CC BY-SA 4.0
    

    # Names of things - allows you to copy/paste commands old_name=feature/old new_name=feature/new remote=origin

    # Rename the local branch to the new name git branch -m $old_name $new_name

    # Delete the old branch on remote git push $remote --delete $old_name

    # Or shorter way to delete remote branch [:] git push $remote :$old_name

    # Prevent git from using the old name when pushing in the next step. # Otherwise, git will use the old upstream name instead of $new_name. git branch --unset-upstream $new_name

    # Push the new branch to remote git push $remote $new_name

    # Reset the upstream branch for the new_name local branch git push $remote -u $new_name

    https://stackoverflow.com/questions/30590083/git-how-to-rename-a-branch-both-local-and-remote/30590238#30590238

    At a glance: Branch renaming commands

    Task Command

    Rename current branch git branch -m new-name Rename specific branch git branch -m old-name new-name Delete remote branch git push origin --delete branch-name Push renamed branch git push origin new-name Set upstream tracking git push --set-upstream origin new-name

    https://www.codecademy.com/article/rename-git-branch

    git branch -m new-branch-name 

    https://www.freecodecamp.org/news/git-rename-branch-how-to-change-a-local-branch-name/