Skip to content

Instantly share code, notes, and snippets.

@abidanBrito
Last active March 29, 2024 20:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abidanBrito/80bf3e04129cf3c0e9bfd61455b1fd3f to your computer and use it in GitHub Desktop.
Save abidanBrito/80bf3e04129cf3c0e9bfd61455b1fd3f to your computer and use it in GitHub Desktop.

GIT CHEATSHEET

Basic Concepts

  • Distributed Version Control System.
  • Working Directory.
  • Staging Area.
  • Local Repository.
  • Remote Repository.
  • Origin.
  • Upstream.
  • HEAD.






Git Configuration

git config --global user.name "John Doe"
git config --global user.email johndoe@example.com
git config --global core.editor emacs

NOTE: these values get stored in a .gitconfig configuration file, under ~/.gitconfig (or ~/.config/git/config) most likely. There may be several such files, by levels of specificity (system, global, local).

Setting Up SSH Keys for GitHub.

Essentials

git init
git init --initial-branch=main
git add .
git add <path/to/file>
git add <path/to/file> <path/to/other/file> <...>
git add -i <path/to/file>
git commit -m <“The comment goes here.”>
git commit -a
git status
git log
git pull 
git push
git checkout <branch / commit>

NOTE: you can add all untracked files to the staging area with git add .. Also, you can add blocks or sections of a file with git add --interactive or git add -i for short.

Working with branches

Creating

git branch <new-branch>
git branch <new-branch> <base-branch>
git checkout -b <new-branch>
git branch --track <new-branch> origin/<base-branch>
git branch <new-branch> <commit-id>
git branch <new-branch> <tag>
git branch -m <current-branch-name> <new-branch-name>

NOTE: there are several ways to create branches off each other. The --track flag is used to denote basing the new branch off a remote one.

NOTE: say you're working in the dev branch locally, then you should push origin dev, i.e., git push origin dev.

Listing

git branch -r
git branch -a

Merging

git checkout <branch-to-merge-to>
git merge <branch-to-be-merged>

Deleting

git branch -d <branch>
git branch -d -r origin/<branch>

Working with a remote repo

git branch -M main
git remote add origin git@github.com:abidanBrito/git-masterclass.git
git remote add origin https://github.com/abidanBrito/git-masterclass.git
git remote add upstream https://github.com/abidanBrito/git-masterclass.git
git push --set-upstream <remote> <branch>
git push -u origin <local-branch>
git push -u origin --all
git push origin --delete <remote>
git remote -v
git remote set-url upstream https://github.com/abidanBrito/git-masterclass.git
git fetch upstream
git remote set-url origin https://github.com/abidanBrito/git-masterclass.git

Other useful commands

Reviewing changes

git diff --<path/to/file>                       (untracked)
git diff --cached <path/to/file>                (staged)
git diff --staged
git diff <branch-a> <branch-b> --<path-to-file>
git diff --name-status <branch-a> <branch-b>

Undoing changes

git commit --amend
git commit --amend -m <"Your message goes here">
git reset <path/to/file>
git reset --hard
git reset --hard HEAD~1
git reset --hard <commit-to-reset-to>

Removing untracked files from the working tree

git clean -n
git clean -f

Unstage all tracked files recursively

git rm --cached -r -n .
git rm --cached -r -q .

Synchronizing with remote

git fetch
git fetch <remote> <branch>
git fetch upstream

What if someone pushes changes between a fetch and a push?

git fetch origin main:tmp
git rebase tmp
git push origin HEAD:main
git branch -D tmp

NOTE: by default, git fetch downloads files, references and commits from origin.

Sometimes you may run into issues when setting up a local repo because of the new "main" default branch name on GitHub. Let's rename the local branch and set its upstream properly.

git branch -m master main
git fetch origin
git branch -u origin/main main
git remote set-head origin -a
git remote -v
cat ./git/config
git repack -a -d --depth=250 --window=250
git fsck
git prune
git gc --prune=now
git gc --aggressive --prune=now
git-prune-packed
git rm -r --cached .
git lfs uninstall
git filter-branch --tree-filter 'rm -f FILENAME' HEAD

Getting rid of problematic LFS files in git history

git lfs uninstall
git rm .gitattributes

git filter-branch --tree-filter 'rm -f FILENAME' HEAD

git push -f origin develop # rewrites history!

Change the author of the last commit

git commit --amend --author="Author Name <johndoe@example.com>"

Alternatively, if you want to reset your email globally, you could do:

git config --global user.email "johndoe@example.com"
git commit --amend --reset-author --no-edit

Change the date of the last commit

git rebase HEAD~1 --exec "git commit --amend --no-edit --date 'Mon Mar 14 19:02:52 2022 +0100'"

NOTE: git remote set-head origin -a fetches and sets origin/HEAD, so that the local repository has a local reference of what the remote repository considers to be the default branch.

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