Skip to main content

Command Palette

Search for a command to run...

Git Cheat Sheet

Published
•3 min read•View as Markdown
Git Cheat Sheet
N

Enthusiast Cloud and Security with expertise in AWS and DevOps. Proficient in Python, Bash, Git, Jenkins, and container orchestration tools like Docker and Kubernetes, I focus on automation and CI/CD practices. I leverage security tools such as NMAP, Nessus, and Metasploit to identify and mitigate vulnerabilities. Passionate about learning and collaboration, I thrive on enhancing cloud security and efficiency. Always eager to advance my skills, I aim to contribute to the tech community.

What is Git?

Git is a distributed version control system designed to track changes in source code during software development. It allows multiple developers to work on a project simultaneously, managing code versions, collaboration, and backup. Git is essential for managing different versions of projects, making it easy to revert back to previous versions or create branches to develop new features without affecting the main codebase.


Git Cheat Sheet

1. Basic Commands

CommandDescription
git initInitialize a new Git repository in your project folder.
git clone <url>Clone a repository from a remote server.
git statusShow the current status of your repository (modified, staged, or untracked files).
git add <file>Add a specific file to the staging area.
git add .Add all files (tracked and untracked) to the staging area.
git commit -m "message"Commit the staged changes with a message.
git commit -am "message"Commit changes with a message (tracked files only).
git logView the commit history.
git diffShow the changes in the working directory that haven’t been staged yet.
git diff --stagedShow the changes between the staged files and the last commit.
git reset <file>Unstage a file from the staging area.
git reset --hardReset the working directory and staging area to the last commit. All changes will be lost.

2. Branching & Merging

CommandDescription
git branchList all branches in the repository.
git branch <branch-name>Create a new branch.
git checkout <branch-name>Switch to another branch.
git checkout -b <branch-name>Create and switch to a new branch.
git merge <branch-name>Merge the specified branch into the current branch.
git branch -d <branch-name>Delete a branch.
git stashStash changes without committing.
git stash popApply the stashed changes.

3. Remote Repositories

CommandDescription
git remote -vView the remote repositories linked to your local repository.
git remote add <name> <url>Add a new remote repository.
git fetch <remote>Fetch the latest changes from the remote repository (without merging).
git pull <remote>Fetch and merge changes from the remote repository to your current branch.
git push <remote> <branch>Push your changes to the specified remote branch.

4. Undoing Changes

CommandDescription
git checkout -- <file>Discard changes in a working directory (restore file to last commit state).
git reset HEAD~1Undo the last commit but keep changes in the working directory.
git revert <commit>Revert a specific commit by creating a new commit.

5. Tagging

CommandDescription
git tagList all tags in the repository.
git tag <tag-name>Create a new tag.
git tag -d <tag-name>Delete a tag.
git push <remote> --tagsPush tags to the remote repository.

6. Viewing Commits

CommandDescription
git show <commit>Show details of a specific commit.
git log --onelineView the commit history in a simplified format.
git log --graph --decorate --allView a graphical representation of the commit history.

This cheat sheet should cover most of the essential commands you'll use regularly with Git!

Thanks,

Nihal Shardul.

More from this blog

Learn with Nihal

14 posts