Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Git cheatsheet

Commands

  • git checkout -b __newbranch__ : get a new branch and switch to it
  • git checkout -b __branchname__ / __branchname__ : get a local copy of a remote existing branch
  • git checkout __branchname__ : switch branch
  • git checkout --orphan __branchname__ : create branch without full history
  • git log -n X -p : show X last commits with diffs
  • git log __branchA__ ^__branchB__ : commits in branch A that aren't in branch B
  • git log --pretty=oneline --stat --all __foldername__ : modified files under a given folder
  • git fetch
  • git pull origin __branchname__
  • git pull --rebase: Avoid having a merge commit if you had changes and origin had commits too (rewrites your local history)
  • git merge --no-ff __sourcebranch__ : merge into current branch source one without rebase/FF
  • git push origin __destinationbranch__ : push merges/changesets to a branch
  • git push origin __destinationbranch__ --force-with-lease : push merges/changesets to a branch. If remote had different commits not in local, fails and does not push. Safer than --force which would override remote commits.
  • git remote show origin : display the path of the repository
  • git remote set-url origin xxx : Change remote URI to xxx
  • git remote rm origin : Remove remote URI
  • git remote add origin git@github.com:<path>.git: replace the origin remote URI. Need to firstgit remote rm origin to remove the previous one.
  • git git remote add <user> git@github.com:<user>/<path>: add <user> fork to a repository as a remote. When pushing changes afterwards to that remote, Github will propose you to create a pull request directly from the fork remote towards the original remote.
  • git add xxx : add files (use . for everything, folder/.. for folder recursive children)
  • git commit : commit changes
  • git status : show status of uncommited files
  • git checkout __file__ : revert a file
  • git checkout __branchname__ __file__ : Checkout all changes to file from branch branchname into current
  • git checkout __revision__ . : revert a full branch to specified revision if not commited
  • git revert __commit1__ __commit2__ ...: Reverts certain commits if commited
  • git clean -f : remove all local uncommited modifications
  • git branch : display local branches, active one is with a *
  • git diff : Show changes in files
  • git diff __branch__ origin/__remotebranch__ : show a diff between a local branch and a remote one
  • git rebase __branchname__ : rebases current branch with specified branch (fetches remote branch changes and then adds yours at the tip)
  • git rebase -i __branchname__: rebases currrent branch with specified branch, allowing you to reorder changes
  • git rm __filename__ : delete a file from branch and filesystem
  • git branch -d __branchname__ : delete a local branch
  • git push origin --delete __branchname__ : delete a remote brach
  • gitk __filename__ : show visual git log
  • git reset __revision__ .: revert a full branch to specified revision if commited
  • git reset --soft HEAD~1 : reset to last commit (even if pushed). Can re-commit stuff but if already pushed will need to push with --force.
  • git reset --hard HEAD^: forcibly resets to the branch's head, discarding any commit.
  • git reset --soft <new-root-sha1> && git commit --amend -m "<new message>" && git push --force: squash all branch pushed commits previous to the one specified into a single commit with the desired new message.
  • git log origin/__branchname__..__branchname__ : Show diff between local commits and remote commits
  • git config --list : List currently setup config values
  • git config --global user.name "Kartones" : Setup global user name
  • git config --global user.email "d...@....net" : Setup global user email
  • git config --global credential.helper 'cache --timeout=28800' : Make git cache credentials for 8 hours
  • git config --global color.ui true : Activate colors in diffs, etc.
  • git config --global core.autocrlf true : Fix Convert newlines to Unix-style ones (Windows)
  • git config --global core.autocrlf input : Fix Convert newlines to Unix-style ones (Unix)*
  • git config --global core.excludesfile ~/.gitignore : Instruct git to always ignore patterns defined at ~/.gitignore
  • git config --global pager.log 'diff-highlight | less': Better diff highlighting (same for 3 following options)
  • git config --global pager.show 'diff-highlight | less'
  • git config --global pager.diff 'diff-highlight | less'
  • git config --global interactive.diffFilter diff-highlight
  • git submodule update --init --recursive : Init and update all submodules
  • git submodule init && git submodule update : Retrieve and update all submodules (alt)
  • Switch from current branch having a submodule to a branch without it:
rm -Rf __submoduledir__
git reset && git checkout .
git checkout __branchname__
  • git pull https://github.com/__username__/__reponame__.git __branchname__: Merge a pull request to local branch
  • git stash: Stash current changes
  • git stash apply: Unstash and merge stored changes
  • git checkout --theirs xxxx git checkout --ours xxxx: Keep changes from incoming branch or local one, respectively.
  • git blame -M: Blames original commit, not the move commit
  • git blame -CCC: Looks at all commits in history
  • git cherry-pick __commit__: merges and commits a specific commit to current branch
  • git reflog + git reset HEAD@__commit__: show all changes on all branches and revert to a specific one
  • git commit --amend: Squash a change on previous commit and change the commit message
  • git diff --staged: Show both staged and unstaged changes that you will commit
  • Undo a commit removing it from history:
git reset --hard HEAD~1
# your new commit here
git push origin <branch> --force
  • Tag any commit of a repo (e.g. with a certain version):
git tag <label> <commit-id>
git push origin <label>
  • Syncing a fork (first configure remote upstream)
  • Apply a .diff file: git apply <filename>.diff
  • Push to a different remote branch: git push origin <localb-ranch>:<remote-branch>
  • Rename a branch: git branch -m <old-name> <new-name>
  • git commit --no-verify ...: With great power comes great responsibility. This flag disables all commit hooks, so use it only when really in need.

Searches

Third-party Tools

  • tig: to navigate commits & branches
  • Github:
    https://github.com/issues?utf8=%E2%9C%93&q=is%3Aopen+org%3Athemotion+sort%3Aupdated-desc
    
    • Additionally, filter to things assigned to me or involving me:
    https://github.com/issues?utf8=%E2%9C%93&q=is%3Aopen+org%3Athemotion+sort%3Aupdated-desc+involves%3Akartones
    

Tutorials

Git conceptual model (source: https://blog.acolyer.org/2016/10/24/whats-wrong-with-git-a-conceptual-design-analysis/)

Misc

xkcd: Git

source: https://xkcd.com/1597/

@Kartones
Copy link
Author

Kartones commented Sep 13, 2016

Added some nice commands seen at Oh shit, git!

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