Bytes
Data ScienceWeb Development

Git Cheat Sheet (Basic to Advanced Git Commands Cheat Sheet)

Last Updated: 10th December, 2024
icon

Arunav Goswami

Data Science Consultant at almaBetter

Explore our Git cheat sheet with commands for setup, repository management, branching, merging, stashing, and advanced operations in an easy table format

Git is a distributed version control system that enables developers to track changes in source code during software development. It is widely used for its efficiency, flexibility, and robust branching capabilities. This cheat sheet provides a comprehensive overview of the most commonly used Git commands and their functionalities.

Basic Git commands Cheat Sheet

Git Setup and Configuration

The commands under this category are used to configure Git with user details like name and email, ensuring that all commits are properly attributed.

Command

Description

git config --global user.name "Your Name"Configures the global username.
git config --global user.email "email@example.com"Configures the global email address.
git config --listLists all Git configuration settings.
git --versionCheck the installed Git version.

Repository Management

These commands allow users to create, initialize, or clone repositories, enabling seamless version control management.

Command

Description

git initInitializes a new repository.
git clone <repo_url>Clones an existing repository.

File Operations

File operation commands help in adding, committing, removing, or renaming files in a Git repository.

Command

Description

git add <file>Stages a specific file for commit.
git add .Stages all changes in the repository.
git commit -m "message"Commits staged changes with a message.
git rm <file>Removes a file from the repository.
git mv <oldname> <newname>Renames a file and stages the change.

Branch Management

Branching commands enable the creation, switching, and deletion of branches, facilitating parallel development.

Command

Description

git branchLists all branches in the repository.
git branch <branch_name>Creates a new branch.
git checkout <branch_name>Switches to an existing branch.
git checkout -b <branch_name>Creates and switches to a new branch.
git branch -d <branch_name>Deletes a local branch.

Commit History and Comparison

These commands allow users to view commit logs and compare changes between files or commits.

Command

Description

git logDisplays the commit history.
git diffShows changes between files or commits.
git show <commit_hash>Shows details of a specific commit.

Remote Operations

Remote operation commands help in connecting, pushing, pulling, and fetching from remote repositories.

Command

Description

git remote -vLists configured remote repositories.
git remote add <name> <url>Adds a new remote repository.
git fetch <remote>Downloads objects and refs from a remote repository.
git pull <remote> <branch>Fetches and merges changes from a remote branch.
git push <remote> <branch>Pushes changes to a remote repository.

Stashing

Stashing commands allow users to save uncommitted changes temporarily, ensuring a clean working directory.

Command

Description

git stashTemporarily stores changes.
git stash applyRestores the most recent stash.
git stash clearDeletes all stashed changes.

Tagging

Tagging commands help mark specific commits, making it easier to reference important milestones in the project.

Command

Description

git tag <tag_name>Creates a tag to mark a specific commit.
git push origin <tag_name>Pushes the specified tag to the remote repository.
git tagLists all tags in the repository.

Undoing Changes

These commands allow users to undo or revert changes to a specific commit or working directory state.

Command

Description

git revert <commit_hash>Reverts a specific commit.
git reset --soft <commit>Moves HEAD to a commit without altering the index.
git reset --hard <commit>Moves HEAD and resets the working directory.
git cherry-pick <commit_hash>Applies changes from a specific commit.

Git Flow Cheat Sheet

Git Flow commands standardize the workflow by creating branches for features, releases, and hotfixes.

Command

Description

git flow initSet up a Git flow structure.
git flow feature start <feature_name>Starts a new feature branch.
git flow feature finish <feature_name>Merges the feature branch into develop.
git flow release start <version>Prepares a release branch.
git flow release finish <version>Completes the release process.

Git Commands Cheat Sheet with Examples

Below are common Git workflows with examples to simplify understanding.

Clone and Initialize

git clone https://github.com/example/repo.git
cd repo
git init

Add and Commit Changes

git add file.txt
git commit -m "Add file.txt"

Branching and Merging

git checkout -b feature/new-feature
git add feature.txt
git commit -m "Add new feature"
git checkout main
git merge feature/new-feature

Push and Pull

git push origin main
git pull origin main

Git Bash Commands Cheat Sheet

Git Bash provides a Linux-style terminal interface for Git operations. It also supports basic shell commands:

Command

Description

lsLists files and directories.
cd <directory>Changes to the specified directory.
pwdPrints the current working directory.
git statusDisplays the current state of the repository.
git logShows the commit history.

Git and GitHub Cheat Sheet

GitHub is an online platform for hosting and collaborating on Git repositories. Key integrations include:

Command

Description

git remote add origin <url>Links the local repository to GitHub.
git push -u origin mainPushes commits to GitHub and sets upstream.
Forking & Pull RequestsCreate a fork and submit contributions.
Issue TrackingTrack and manage project tasks.

Tips for Effective Git Usage

  • Commit often with meaningful messages.
  • Use .gitignore to exclude unnecessary files.
  • Use git status frequently to review changes.
  • Pull updates before pushing to avoid conflicts.

Conclusion

Git is a powerful version control system essential for efficient collaboration and project management. This cheat sheet categorizes and explains key Git commands for setup, repository management, branching, and advanced operations. Mastering these commands streamlines workflows and ensures robust code management.

More Cheat Sheets and Top Picks

  • Official Address
  • 4th floor, 133/2, Janardhan Towers, Residency Road, Bengaluru, Karnataka, 560025
  • Communication Address
  • Follow Us
  • facebookinstagramlinkedintwitteryoutubetelegram

© 2024 AlmaBetter