Skip to content

Instantly share code, notes, and snippets.

@Adhjie
Forked from gorshkov-leonid/git-tricks.md
Created December 13, 2024 10:43
Show Gist options
  • Save Adhjie/c9f092533d542856c81b8ffd4c8eafcd to your computer and use it in GitHub Desktop.
Save Adhjie/c9f092533d542856c81b8ffd4c8eafcd to your computer and use it in GitHub Desktop.
Git Tricks

Git installation

Settings

git config --global init.defaultBranch main
git config --global user.name "Gorshkov"
git config --global user.email [email protected]
git config --global core.autocrlf false
git config --global core.eol lf
git config --global alias.last 'log -1 HEAD'
#git config --global pull.rebase true
git config --global core.longpaths true
git config --global core.editor "'/mnt/c/Program Files/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"
git config --global core.editor "'C:/Program Files/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"

Set email per host

  • Create script mkdir -p ~/.git-scripts && touch ~/.git-scripts/set-email.sh && chmod +x ~/.git-scripts/set-email.sh
    #!/bin/bash
    remote=`git remote -v | awk '/\(push\)$/ {print $2}'`
    [email protected]
    if [[ $remote == *mycompany.com* ]]; then
      [email protected]
    fi
    echo "Configuring user.email as $email"
    git config user.email $email
  • Create alias
    git config --global alias.set-email \!"bash -c ~/.git-scripts/set-email.sh"
    
  • Setup hooks folder
    mkdir -p ~/.git-scripts/hooks && git config --global core.hooksPath ~/.git-scripts/hooks
    
  • Create post-checkout hook
    • touch ~/.git-scripts/hooks/post-checkout && chmod +x ~/.git-scripts/hooks/post-checkout
    • Set content of created file
      #!/bin/bash
      # On clone “previous SHA” is all zeros
      if [[ $1 == 00000000000* ]]; then
         git set-email
      fi

See links:

Validate issue code in commit message

  • Setup hooks folder
    mkdir -p ~/.git-scripts/hooks && git config --global core.hooksPath ~/.git-scripts/hooks
    
  • Create commit-msg hook
    • touch ~/.git-scripts/hooks/commit-msg && chmod +x ~/.git-scripts/hooks/commit-msg
    • Set content of created file
      #!/bin/bash
      remote=`git remote -v | awk '/\(push\)$/ {print $2}'`
      echo $remote
      if [[ $remote == *mycompany.com:SOME.REPO.GROUP* ]]; then
        test "" != "$(grep 'SOMECODE-' "$1")" || {
             echo >&2 Issue code SOMECODE-xxxx is required.
             exit 1
        }
      fi

See links:

SSH Keys for Github

Read downstream article

GIT Clients

  • TortoiseGit
    • Use Open SSH agent
    • Use Credential Manager manager-core

Useful command

Read downstream article

Update File Permissions

Read downstream article

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