• find all git repositories on machine:
    find / -name .git -print 2>/dev/null
  • ignore spaces in diff – add “-w” switch
  • the same on github – add “?w=1” to URL of unified diff
  • the same in local git gui – switch on “ignore space change” in diff window
  • reset local changes (example for master branch):
    git reset --hard origin/master
  • reset one file only:
    git checkout HEAD -- filename
  • checkout local branch tracking remote branch (create it if does not exist):
    git checkout -t -b develop origin/develop
  • show git branches based on pattern:
    git show-branch -a | grep pattern
  • check differences between branches in meld file by file:
    git config --global diff.tool meld
    git difftool master..develop
  • see all differences between current and other branch in meld:
    git difftool -d develop
  • the same between two branches regardless on branch you are now:
    git difftool -d master..develop
  • add branch name to the promtp – see here https://coderwall.com/p/fasnya/add-git-branch-name-to-bash-prompt
    you have to add into .bashrc (or other .*rc according to your shell):

    parse_git_branch() {
         git branch 2>/dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
    }
    export PS1="\u@\h \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ "

    or other version:

    # Show git branch in prompt
    function parse_git_branch () {
      git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
    }
    
    NO_COLOR="\[\033[0m\]"
    RED="\[\033[0;31m\]"
    YELLOW="\[\033[0;33m\]"
    GREEN="\[\033[0;32m\]"
    BLUE="\[\033[1;34m\]"
    LIGHT_RED="\[\033[1;31m\]"
    LIGHT_GREEN="\[\033[1;32m\]"
    CYAN="\[\033[0;36m\]"
    LIGHT_CYAN="\[\033[1;36m\]"
    WHITE="\[\033[1;37m\]"
    LIGHT_GRAY="\[\033[0;37m\]"
    PURPLE="\[\033[0;35m\]"
    LIGHT_PURPLE="\[\033[1;35m\]"
    BLACK="\[\033[0;30m\]"
    
    #PS1="$LIGHT_GREEN\u@$LIGHT_PURPLE\h$BLUE:\w$LIGHT_CYAN\$(parse_git_branch)$WHITE\$ "
    PS1="$LIGHT_GREEN\u$BLUE:\w$LIGHT_CYAN\$(parse_git_branch)$WHITE\$ "
  • see history of a file (even deleted one) – you can use wild cards in the name, shows only commit headers:
    git log -- filename.txt
  • search history of the repository for specific string in files (like by mistake commited password) – based on this stackoverflow page:
    • git log -Spassword
  • use specific ssh key for git clone/ pull command (based on this)
    • add section for github.com into your ~/.ssh/config file (must point at private key – not a public one):
      Host github.com
      IdentityFile ~/.ssh/yourspecific_id_rsa
      IdentitiesOnly yes
  • check difference between local repo and origin (based on this)
    • git diff @{upstream}
Tags: