Git all-in-one cheat sheet: A collection of the most useful git commands

Yash Gupta Feb 18, 2025 8 mins read

Git is a widely used version control system, originally developed by Linus Torvalds to manage the Linux source code. Today, it powers millions of projects across various programming languages. 🚀

Remembering all the Git commands for common tasks can be overwhelming, so we’ve put together this All-in-One Git Cheat Sheet covering both essential and advanced commands. 🚀

Download the GIT for all Platforms:

Setup:

#configuring user information used across all local repositories.

#set a name that is identifiable for credit when review version history
git config --global user.name “[firstname lastname]”

#set an email address that will be associated with each history marker
git config --global user.email “[valid email]”

#set automatic command line coloring for Git for easy reviewing
git config --global color.ui auto

Most Commonly Used GIT Commands:

#create new repository in current directory
git init

#add a file as it's ready for your next commit (stage)
git add <file>

#add all files as they are ready for your next commit (stage)
git add .

#show modified files in working directory, staged for your next commit
git status

#commit your staged content as a new commit snapshot
git commit -m “[descriptive message]”

# Create a new connection to a remote repository by giving it name and it's url
git remote add <remote_name> <remote_url>

# push your local branch to specified remote.
git push <remote_name> <branch_name>

# Download new changes from the branch_name on the remote.
git pull <remote_name> <branch_name>

#Display the entire commit history using the default format.
git log

Creating Repositories:

#create new repository in current directory
git init

#clone a remote repository
git clone <url>

#for example cloning the entire repo locally
git clone https://github.com/CodingBeam/Test Project.git

Stage and Snapshot:

#show modified files in working directory, staged for your next commit
git status

#add a file as it's ready for your next commit (stage)
git add <file>

#add all files as they are ready for your next commit (stage)
git add .

#unstage a file while retaining the changes in working directory
git reset <file>

#diff of what is changed but not staged
git diff

#diff of what is staged but not yet committed
git diff --staged

#commit your staged content as a new commit snapshot
git commit -m “[descriptive message]”

Branch and Merge:

#create a new branch at the current commit
git branch <branch_name>

#list your all branches. a '*' will appear next to the currently active branch
git branch

#switch to another branch and check it out into your working directory.
git checkout

#Create and check out a new branch named [branch_name]. above two commands in one.
git checkout -b <branch_name>

#merge the specified branch’s history into the current one
git merge <branch>

Tracking Path Changes:

# remove files & path changes

#delete the file from project and stage the removal for commit
git rm <file>

#change an existing file path and stage the move
git mv <existingpath><newpath>

#show all commit logs with indication of any paths that moved
git log --stat -M

Share and Update:

#Retrieving updates from another repository and updating local repos
git remote add <alias><url>

#fetch down all the branches from that Git remote
git fetch <alias>

#merge a remote branch into your current branch to bring it up to date
git merge <alias>/<branch>

#Transmit local branch commits to the remote repository branch
git push <alias><branch>

Rewrite History:

#Rewriting branches, updating commits and clearing history

#apply any commits of current branch ahead of specified one
git rebase <branch>

#clear staging area, rewrite working tree from specified commit
git reset --hard <commit>

Delete Commits History from Git/GitHub:

If you’ve accidentally pushed sensitive information (like SECRET_KEY, API_KEY, PASSWORDS, or .env files) to your GitHub repository, simply deleting them won’t remove them from previous commits. Instead of deleting the entire repository, it's best to wipe the commit history to ensure the data is fully removed. 🔒

Manually deleting the .git folder can cause issues in your Git repository. If you want to clear the commit history while keeping your code intact, follow this method instead:

# Check out a temporary branch to hold our commit:
git checkout --orphan TEMP_BRANCH

# Add all the files to the temporary branch:
git add .

# Commit the changes:
git commit -m "Initial commit"

# Delete the old branch (most probably old_branch would be a master branch):
git branch -D <old_branch_name>

# Rename the temporary branch (TEMP_BRANCH) to master:
git branch -m master

# Finally, force update to our repository:
git push -f origin master

I hope this post will help you in your journey. Keep learning!

Yash Gupta
I am passionate about tech and coding. I share expert insights on Test Automation (Selenium, Cypress, Playwright), API Automation, JavaScript, Python, Svelte, Vue.js, ReactJS, Angular, Flutter, and more. Stay updated with the latest trends! 🚀