A Quick Git Bash Commands Cheat Sheet

Table of Contents

Setup

$ git config — global user.name “YOUR NAME”$ git config — global user.email “myemail@gmail.com”

Start a Project

$ git init <directory>// Download a remote repo$ git clone <url>

Make a Change

$ git add <file>// Stage all files$ git add .$ git commit -m “commit message”// Add all changes made to tracked files & commit$ git commit -am “commit message”

Basic Concepts

Branches

  • List all local branches.
  • Add -r flag to show all remote branches.
  • Use -a flag for all branches.
$ git branch// Create a new branch$ git branch <new-branch>
  • If you want to switch to a branch & update the working directory.
$ git checkout <branch>// Create a new branch and switch to it.$ git checkout -b <new-branch>
  • Delete a branch
// Delete a merged branch$ git branch -d <branch>// Delete a branch, whether merged or not$ git branch -D <branch>
  • Add a tag to the current commit (often used for new version releases)
$ git tag <tag-name>

Merging

$ git checkout b$ git merge a// Merge & squash all commits into one new commit$ git merge — squash a

Rebasing

$ git checkout feature$ git rebase main
$ git rebase -i main// rebase the last 2 commits on current branch$ git rebase -i HEAD~2

Undoing Things

  • Move (or rename) a file and stage move.
$ git mv <existing-path> <new-path>
  • Remove a file from the working directory & staging area, then stage the removal.
$ git rm <file>
  • Remove from staging area only
$ git rm — cached <file>
  • View previous commit (READ only)
$ git checkout <COMMIT_ID>
  • Create a new commit, reverting the changes from a specified commit.
$ git revert <COMMIT_ID>
  • Go back to a previous commit & delete all commits ahead of it (revert is safer). Add –hard flag to also delete workspace changes (Be very careful)
$ git reset <COMMIT_ID>

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Siddharth Rastogi

Siddharth Rastogi

41 Followers

I am a full stack developer, I have an expertise in Web Development. I write tech stuff and share my knowledge with others with the help of articles.