A Quick Git Bash Commands Cheat Sheet

Siddharth Rastogi
3 min readApr 6, 2022

--

Hey everyone, In this article, we will see the most used Git Bash commands and give a quick start with a cheat sheet. We already discussed in the brief of Git commands earlier Important Basic Git Commands — Developers should know.

Git helps to tracks and work on the same workspace that developers or organizations use to collaborate on developing applications.

So, let’s start with the Git commands

Table of Contents

Setup

Set the name and email that will be attached to your commits and tags

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

Start a Project

Create a local repo (omit <directory> to initialize the current directory as a git repo)

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

Make a Change

Add a file to the staging

$ 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

main or master: default development branch

origin: default upstream repo

HEAD: current branch

HEAD^: parent of HEAD

HEAD~2: grandparent of HEAD

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

Merge branch one into branch two. Add no-ff option for no-fast-forward merge.

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

Rebasing

Rebase feature branch onto main(to incorporate new changes made to main). Prevents unnecessary merge commits into the feature, keeping history clean.

$ git checkout feature$ git rebase main

It cleans up a branch commits before rebasing onto the 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>

These are the quick git cheat sheet, this would be helpful to make increase the productivity.

To read full Story checkout here Sidtechtalks.

--

--

Siddharth Rastogi

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