Skip to content

Instantly share code, notes, and snippets.

@jim-ec
Created June 21, 2023 10:31
Show Gist options
  • Save jim-ec/f26ccf59dff2d45455f603ecf4a0b13c to your computer and use it in GitHub Desktop.
Save jim-ec/f26ccf59dff2d45455f603ecf4a0b13c to your computer and use it in GitHub Desktop.
Git Cheatsheet

Git Cheatsheet

Configuration

[core]
    editor = nvim
    autocrlf = input
    ignorecase = false
[pull]
    rebase = true
[fetch]
    prune = true
[credential]
    helper = store
[user]
    name = Jim Eckerlein
    email = [email protected]
[alias]
    c = commit
    s = status
    a = add .
    f = fetch
    l = log --all --oneline --graph --tags --color=always
[filter "lfs"]
    process = git-lfs filter-process
    required = true
    clean = git-lfs clean -- %f
    smudge = git-lfs smudge -- %f
[init]
    defaultBranch = main
[advice]
    detachedHead = false

Undo amending commit

git reset --soft @{1}

Credentials

  • Save credentials: git config --global credential.helper store
  • Remove credentials: git config --global --unset credential.helper

Show informations

  • Log: git log --all --oneline --graph --tags
  • List modified files: git diff --name-only [commit-old] [commit-new]

Branches

  • Show remote tracking branches: git branch -vv
  • Set branch upstream: git branch -u [remote]/[branch]
  • Delete remote branch: git push [remote] --delete [branch]
  • Rename branch
git branch -m [new_branch]
git push origin :[old_branch]
git push --set-upstream origin [new_branch]

Tags

  • Create tag: git tag [tag]
  • Push tag: git push [remote] [tag]
  • Delete remote tag: git push [remote] --delete [tag]

Stash

  • Stash away single files: git stash push -- [file]
  • Apply single file from stash: git checkout stash@{0} -- [file]

Pushing

  • Push commit up to a specific commit: git push [remote] [commit]:[remote-branch]

Merging

  • Pull and merge unrelated history: git pull --allow-unrelated-histories

Remotes

  • Rename a remote: git remote rename [old-remote] [new-remote]

Apply a diff/patch

  • Create a patch file: git diff [commit1] [commit2] > [patch-file]
  • Apply patch: git apply [patch-file]

Utilities

  • Cleanup garbage: git gc
  • Convert crlf to lf on check-in, checkout lf: git config --global core.autocrlf input
  • Disable default fast-forward merge: git config --global merge.ff false
  • Enable default rebasing when pulling: git config --global pull.rebase true

GitHub

Update fork including tags:

git remote add upstream [email protected]:user/repos.git
git fetch upstream
git rebase upstream/master
git push
git push --tags

Git remote over SSH (GitHub)

I'm not really sure which steps are actually required.

  1. ssh-keygen -t rsa -b 4096 -C [email]
  2. eval $(ssh-agent -s)
  3. ssh-add ~/.ssh/id_rsa
  4. Add key from cat ~/.ssh/id_rsa.pub to Github account (Settings > SSH and GPG keys).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment