Skip to content

Instantly share code, notes, and snippets.

@tlareg
Last active December 9, 2023 11:31
Show Gist options
  • Save tlareg/26b5964bee1038869a05 to your computer and use it in GitHub Desktop.
Save tlareg/26b5964bee1038869a05 to your computer and use it in GitHub Desktop.
git cheatsheet



  • user

  • ssl

  • config

    • list all

      $ git config -l
      
    • unset value

      $ git config --global --unset core.editor
      
    • set value

      $ git config --global core.editor "vim"
      
    • aliases

      git config --global alias.st status 
      git config --global alias.s "status -sb"
      git config --global alias.a "add -A"
      git config --global alias.ci commit
      git config --global alias.cm "commit -m"
      git config --global alias.wip "commit -m 'WIP'"
      git config --global alias.cane "commit --amend --no-edit"
      git config --global alias.pushf "push --force-with-lease"
      git config --global alias.co checkout
      git config --global alias.br branch
      git config --global alias.rd "pull --rebase origin develop"
      git config --global alias.rc "rebase --continue"
      git config --global alias.cp "cherry-pick"
      git config --global alias.pu "git push --set-upstream origin \"$(git branch --show-current)\""
      git config --global alias.conflicts "diff --name-only --diff-filter=U"
      git config --global alias.unstage 'reset HEAD --'
      git config --global alias.lg "log -5 --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative"
      # run git command in all git subdirectories - e.g. git all pull
      git config --global alias.all '!f() { ls -R -d */.git | xargs -I{} bash -c "echo {} && git -C {}/../ $1"; }; f'
      

      must be added directly in .gitconfig:

      [alias]
        po = "!git push --set-upstream origin \"$(git rev-parse --abbrev-ref HEAD)\""
      
    • push and pull only current branch by default

      $ git config --global push.default current
      $ git config --global pull.default current
      
  • clone a repository without getting the entire history

    $ git clone <repository URL> --depth 5
    
  • checkout

    • checkout to commit

      $ git checkout <commit>
      
    • HEAD

      • ^2 means the second parent where a commit has more than one parent (i.e. because it's a merge) moving upwards one commit at a time with ^

        $ git checkout HEAD^2
        
      • ~2 means up two levels in the hierarchy, via the first parent if a commit has more than one parent moving upwards a number of times with ~

        $ git checkout HEAD~2
        
      • HEAD@{} variables capture the history of HEAD movement, and you can decide to use a particular head by looking into reflogs of git using the command git reflog.

        $ git checkout HEAD@{2}
        
      • topic~3^2 in English is the second parent of the merge commit that is the great-grandparent (three generations back)

      • https://stackoverflow.com/questions/2221658/whats-the-difference-between-head-and-head-in-git

          G   H   I   J
           \ /     \ /
            D   E   F
             \  |  / \
              \ | /   |
               \|/    |
                B     C
                 \   /
                  \ /
                   A
        
          A =      = A^0
          B = A^   = A^1     = A~1
          C = A^2
          D = A^^  = A^1^1   = A~2
          E = B^2  = A^^2
          F = B^3  = A^^3
          G = A^^^ = A^1^1^1 = A~3
          H = D^2  = B^^2    = A^^^2  = A~2^2
          I = F^   = B^3^    = A^^3^
          J = F^2  = B^3^2   = A^^3^2
        

        J = A^^3^2 czyli najpierw wybieramy rozwidlenie 1, później wybieramy rozwidlenie 3, potem wybieramy rozwidlenie 2
        H = A~2^2

      • https://i.stack.imgur.com/pDAzG.png

    • switch branch

      $ git checkout <LOCALBRANCHNAME>
      
    • switch to last branch

      $ git checkout - # shorthand for: git checkout @{-1}
      
    • create local branch

      $ git checkout -b <LOCALBRANCHNAME>
      
    • switch to remote branch

      $ git fetch
      $ git checkout -b <LOCALBRANCHNAME> <REMOTENAME>/<REMOTEBRANCHNAME>
      

      or just

      $ git checkout <REMOTEBRANCHNAME>
      

      If is not found but there does exist a tracking branch in exactly one remote (call it ) with a matching name, treat as equivalent to $ git checkout -b <branch> --track <remote>/<branch>

  • branch

    • origin/master is the local branch representing the state of the master branch on the remote origin.

    • origin master is the branch master on the remote origin.

    • list all branches

      $ git branch -a
      
    • list local branches

      $ git branch
      
    • list remote branches

      $ git branch -r
      
    • list local branches which contain the specified commit

      $ git branch --contains <commit>
      
    • lists remote tracking branches which contain the specified commit

      $ git branch -r --contains <commit>
      
    • rename branch

      $ git branch -m <oldname> <newname>
      # or when renaming current branch
      $ git branch -m <newname>
      
    • delete local branch

      $ git branch -d <LOCALBRANCHNAME>
      
    • delete local branches matching pattern

      $ git branch --list 'feature*' | xargs -r git branch -D
      
  • diff

    • unstaged changes

      $ git diff
      
    • staged changes

      $ git diff --staged
      
    • unstaged + staged changes

      $ git diff HEAD
      
  • clean / clear

    • removes all untracked files

      $ git clean -fd
      
    • clear all unstaged changes (discard all changes in working directory)

      $ git checkout -- .
      
  • commit

    • add and commit

      # git add -A is a shortcut for git add .; git add -u 
      # The first command stages all modified/updated files already tracked. 
      # Second command stages all untracked or removed files.
      $ git add -A && git commit -m "message"
      
    • amend commit

      $ git commit --amend -m "message"
      
    • amend commit without changing message

      $ git commit --amend --no-edit
      
  • remote

    • list remotes

      $ git remote -v
      
    • add remote

      $ git remote add <remote name> <repository URL>
      
  • fetch -p, --prune After fetching, remove any remote-tracking branches which no longer exist on the remote.

    $ git fetch -p
    
  • pull

    $ git pull <REMOTENAME> <REMOTEBRANCHNAME>
    
    • git pull fails “unable to resolve reference” “unable to update local ref”
      $ git gc --prune=now
      $ git remote prune origin
      
      $ rm .git/refs/remotes/origin/master
      $ git fetch
      
  • push

    • push to remote branch; also can create remote branch, the remote branch is automatically created when you push it to the remote server. -u sets remote tracking

      $ git push -u <REMOTENAME> <REMOTEBRANCHNAME>
      
      • error
        error: src refspec master does not match any.
        error: failed to push some refs to 'git@github ... .git'
        
        solution:
        $ git push -u origin HEAD:master
        
    • push with rename

      $ git push -u <REMOTENAME> <LOCALBRANCHNAME>:<REMOTEBRANCHNAME> 
      
    • delete remote branch

      $ git push origin :<REMOTEBRANCHNAME> 
      # or
      $ git push origin --delete <REMOTEBRANCHNAME>
      
  • merge remote branch - first switch to branch you want to merge into; can add --no-ff --no-commit, or --ff-only

    $ git merge <REMOTENAME>/<REMOTEBRANCHNAME>
    
  • rebase

    (master)$ git checkout new-feature
    (new-feature)$ git rebase master
    # git pull --rebase origin master
    # git rebase -i master
    (new-feature)$ git checkout master
    (master)$ git merge --ff-only new-feature
    
  • interactive rebase to change history

    $ git rebase -i <commit>
    
    pick a9c8a1d Some refactoring
    pick 01b2fd8 New awesome feature
    pick b729ad5 fixup
    pick e3851e8 another fix
    
    # Rebase 8074d12..b729ad5 onto 8074d12
    #
    # Commands:
    #  p, pick = use commit
    #  r, reword = use commit, but edit the commit message
    #  e, edit = use commit, but stop for amending
    #  s, squash = use commit, but meld into previous commit
    #  f, fixup = like "squash", but discard this commit's log message
    #  x, exec = run command (the rest of the line) using shell
    #
    # These lines can be re-ordered; they are executed from top to bottom.
    #
    # If you remove a line here THAT COMMIT WILL BE LOST.
    #
    # However, if you remove everything, the rebase will be aborted.
    #
    # Note that empty commits are commented out
    
  • conflicts

    • resolve conflict and add file
      $ git add <FILE>
      
    • or remove file
      $ git rm <FILE>
      
    • us vs them
      • git pull --rebase origin develop
        • us (current change) == develop
        • them (incoming change) == feature-branch
      • git merge develop
        • us (current change) == feature-branch
        • them (incoming change) == develop
  • reflog - see your previous git actions

    $ git reflog -10
    
  • reset hard

    $ git reset --hard HEAD@{4}
    
  • patch

    • many commits to one file

      git format-patch -<number_of_commits> <hash> --stdout > <file_name>.patch
      
    • create patch with one commit from sha1

      $ git format-patch -1 <sha1>
      
    • apply patch (this adds changes as commits)

      # show stats. 
      $ git apply --stat file.patch 
      # check for error before applying.
      $ git apply --check file.patch 
      # apply the patch finally 
      $ git am < file.patch
      
    • save working directory diff

      $ git diff > diffFileName
      
    • apply working directory diff

      $ git apply diffFileName  
      
      • in case of troubles
        $ git apply --ignore-space-change --ignore-whitespace diffFileName
        
  • cherry pick

    $ git cherry-pick <sha1>
    
  • create a new commit that removes these changes

    $ git revert <sha1>
    
  • stash

    • stash files

      $ git stash
      

      or

      $ git stash push -m "my message"
      
    • stash with untracked

      $ git stash --include-untracked
      

      or

      $ git stash -u
      
    • show most recent stash entry

      $ git stash show -p
    • pop stash

      $ git stash pop
      
    • apply stash at n

      $ git stash apply stash@{n}
      
    • show stash list

      $ git stash list
      
    • clear all stashed states

      $ git stash clear
      
  • bisect start

    $ git bisect start
    $ git bisect good <commit>
    $ git bisect bad <commit>
        # then good
        $ git bisect good
        # or bad
        $ git bisect bad
        # to end
        $ git bisect reset
    
  • blame

    $ git blame <filename>
    $ git log -p <commit>
    
  • stats - show sorted number of commits by person without merges

    $ git shortlog -ns --no-merges
    
  • usefull log formats

    $ git log --pretty --oneline -5
    $ git log --oneline --all --decorate
    $ git log --author="<AUTHOR>" --pretty=tformat: --numstat | gawk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s removed lines: %s total l 
    ines: %s\n", add, subs, loc }' -
    $ git log --author="<AUTHOR>" --oneline --shortstat
    $ git log --author="<AUTHOR>" --pretty=tformat: --numstat
    
  • tags

    • list tags
      $ git tag -l
      
    • create lightweight tag
      $ git tag <tagname>
      
    • push tag
      $ git push origin <tagname>
      
    • push all tags
      $ git push --tags
      
    • delete local tag
      $ git tag --delete <tagname>
      
    • delete remote tag
      $ git push --delete origin <tagname>
      
  • whatchanged

    $ git whatchanged --since="2018-01-01" --until="2018-01-31" -p --author="<AUTHOR>" --all > kupa.txt
    
    $ git log --author="<AUTHOR>" --since="2022-05-01" --until="2022-05-31" --pretty=format:"%Cred%h%Creset - %s" --abbrev-commit --date=relative"
    
  • mv

    $ git mv -f FileNameCase filenamecase
    
  • .gitignore not working? clear cache link

    $ git add [uncommitted changes you want to keep] && git commit
    $ git rm -r --cached .
    $ git add .
    $ git commit -m "fixed untracked files"
    
  • $ gitk --follow [filename] show file history

  • skip-worktree - local ignore changes in e.g. server configuration file (more info: https://stackoverflow.com/questions/1753070/how-do-i-configure-git-to-ignore-some-files-locally)

    $ git update-index --skip-worktree <file-path>
    

    to revert

    $ git update-index --no-skip-worktree <file-path>
    
  • submodules

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