git cheat sheet
git cheat sheat

git cheat sheat

1. Getting started

  • clone repo to your hard drive for the first time

    git clone ssh://diskstation:22/volume1/git/your_repo.git

    (add --depth 1 to only clone the latest version)

  • add a new remote with its URL

    git remote add <remote name, e. g. origin> <URL>
  • checkin for the first time (repo origin, branch master)

    git push --set-upstream origin master

2. Comparisons

  • Compare the local, not yet staged (added) changes with the local staged or commited version

    git difftool
  • Compare the local, added (staged) changes with the local commited version

    git difftool --cached
  • Compare the local, commited and not yet pushed version with the latest of the repository

    git difftool @{upstream}
  • Compare last two commited versions

    git difftool HEAD^ HEAD
  • show difference between two commits

    git difftool <commit-id>^ <commit-id> (these are the same commit-ids, e. g. printed by git log <filename>)
  • show all configuration settings with the config file paths

    git config --list --show-origin
  • store entered passwords (particularly those for git lfs)

    git config --global credential.helper 'store'
  • use https:// instead of git://

    git config --global url."https://".insteadOf git://
  • Remove global .gitignore

    git config --global core.excludesfile false
  • Prevent auto crlf

    git config  core.autocrlf false
  • check global gitignore

    git config --global --get core.excludesfile
  • Ignore chmod file changes

    git config --global core.fileMode false

4. Submodules

  • add a submodule

    git submodule add https://github.com/wxWidgets/wxWidgets.git
  • fetch all submodules (needed if submodules were added in context of a pull)

    git submodule update --recursive --init
  • pull all submodules

    git pull --recurse-submodules
    git submodule update --recursive --remote
  • retract all the local changes in all submodules

    git submodule foreach --recursive git reset --hard
  • in all submodules, delete all files that are not part of the repository

    git submodule foreach --recursive git clean -fdx
  • make submodule’s URL accessible via SSH

    • on the client machine:

      vi ~/.ssh/config
      Host *
        User SSH_user_name_of_the_main_repo
    • in file .gitmodules, use a relative entry url = ../your_submodule.git

5. Change working copy state

  • undo last commit (before a push)

    git reset --hard HEAD^
  • change local working copy to state of repository (possible old <commit-id>)
    (in case you have submodules, do the same for each submodule)

    git reset --hard [HEAD|<commit-id>]
    git clean -d -x -f
  • unstage all files with extension .ext

    git reset **/*.ext
  • create a new branch and switch to it

    git branch <name>
    git checkout <name>
  • backup current working copy state and switch back to HEAD

    git stash
  • fetch a previously stashed state

    git stash pop
  • remove file from commit list

    git rm —cached <filename>

6. Mac specific

  • Configure KDiff3 as diff and merge tool

    git config --global --add merge.tool kdiff3
    git config --global --add mergetool.kdiff3.path "/Applications/kdiff3.app/Contents/MacOS/kdiff3"
    git config --global --add mergetool.kdiff3.trustExitCode false
    git config --global --add diff.guitool kdiff3
    git config --global --add difftool.kdiff3.path "/Applications/kdiff3.app/Contents/MacOS/kdiff3"
    git config --global --add difftool.kdiff3.trustExitCode false

7. Windows specific

  • Create SSH key pair under Windows

    "C:\Program Files\Git\usr\bin\ssh-keygen" -t rsa -C "your@mailadress.de" -b 4096
  • Configure KDiff3 as diff and merge tool

    git config --global --add merge.tool kdiff3
    git config --global --add mergetool.kdiff3.path "C:/Program Files/KDiff3/kdiff3.exe"
    git config --global --add mergetool.kdiff3.trustExitCode false
    git config --global --add diff.guitool kdiff3
    git config --global --add difftool.kdiff3.path "C:/Program Files/KDiff3/kdiff3.exe"
    git config --global --add difftool.kdiff3.trustExitCode false

8. Managing repositories on a Synology Diskstation

  • List all repositories

    ssh -l admin -p 22 diskstation
    cd /volume1/git
    ls
  • Create new repository

    ssh -l admin -p 22 diskstation
    cd /volume1/git
    git init --bare your_repo.git

9. Other stuff

  • list private files

    git ls-files . --ignored --exclude-standard --others
  • fix author of commit

    git commit --amend --author="your name <your@mail-adress.com>"
  • undo erroneous commit
    Check the commit id of the erroneous commit using git log, e. g. f414f31, then type

    git revert f414f31
  • force the remote branch to match your working copy (a rarely needed hack)

    git push origin master --force
  • List all files of a certain commit

    git diff-tree --no-commit-id --name-only -r <commit-id>
  • get last commit date and message, then push this info to a different repository (empty commit)
    ($BUILD_DISPLAY_NAME can be used to refer to a Jenkins build, which sets this variable automatically)

    cd dependent_repo_path
    msg=$(git log -1 --pretty="%ci %B")
    cd ../different_repo
    git commit --allow-empty -m "dependent_repo $BUILD_DISPLAY_NAME: $msg"
    git push origin HEAD:master
  • Sample .gitignore file

    $tf/*
    BuildLog.htm
    unsuccessfulbuild
    *.bi
    *.bsc
    *.cache
    *.db
    *.dep
    *.embed.manifest
    *.embed.manifest.res
    *.exe
    *.idb
    *.intermediate.manifest
    *.iobj
    *.ipch
    *.ipdb
    *.ilk
    *.lastbuildstate
    *.lnk
    *.ncb
    *.obj
    *.opensdf
    *.orig
    *_original
    *.pch
    *.pdb
    *.pgd
    *.pkg
    *.pst
    *.resources
    *.sdf
    *.suo
    *.swp
    *.temp
    *.tlog
    *.tmp
    *.user*
    *.VC.db
    *.VC.opendb
    *.vcxproj.user
    *.vspscc
    *.vssscc
    **/.vs/*
    **/.nuget/*
    **/packages/*