GIT cheat sheet

Top 30 Git commands

  1. Set up your username and email
git config --global user.name "max-p0w3r"
  1. Cache your login credentials
git config --global credential.helper cache
  1. Initialize a repository
git init
  1. Add individual file or all files to staging area ``
git add somefile.js
  1. Check a repository status
git status
  1. Commit changes with a single line message or through an editor
git commit -m "Your short message about the commit"
  1. View commit history with changes
git log -p
  1. View a particular commit
git show "ID or hash of the commit" 
git show "short hash to get the same result"
  1. View changes before committing
git diff
  1. Remove tracked files from the current working tree
git rm dirname/somefile.js
git rm dirname/*.html
  1. Rename files
git mv dir1/somefile.js dir2
  1. Revert unstaged and staged changes
git checkout somefile.js
  1. Amend the most recent commit
git commit --amend -m "Updated message for the previous commit"
  1. Rollback last commit
git revert
  1. Rollback a particular commit
git revert ID
  1. Create and switch to a new branch
git branch new_branch_name
git checkout -b new_branch_name
  1. List all branches
git branch
git branch -a
  1. Delete a branch
git branch -d existing_branch_name
git branch -D existing_branch_name
  1. Merge two branches
git merge existing_branch_name
  1. Show commit log as graph for current or all branches
git log --graph --oneline --decorate
git log --all --graph --oneline --decorate
  1. Abort a conflicting merge
git merge --abort
  1. Add a remote repository
git remote add awesomeapp https://github.com/someurl..
  1. View remote URLs
git remote -v
  1. Get additional information about a remote repository
git remote show origin
  1. Push changes to a remote repository
git push origin main
  1. Pull changes from a remote repository
git pull
git pull --verbose
  1. Merge remote repository with local repository
git merge origin
  1. Push a new branch to a remote repository
git push -u origin new_branch
  1. Remove a remote branch
git push --delete origin existing_branch
  1. Use rebase
git rebase branch_name