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.
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 --list | Lists all Git configuration settings. |
git --version | Check the installed Git version. |
These commands allow users to create, initialize, or clone repositories, enabling seamless version control management.
Command | Description |
git init | Initializes a new repository. |
git clone <repo_url> | Clones an existing repository. |
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. |
Branching commands enable the creation, switching, and deletion of branches, facilitating parallel development.
Command | Description |
git branch | Lists 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. |
These commands allow users to view commit logs and compare changes between files or commits.
Command | Description |
git log | Displays the commit history. |
git diff | Shows changes between files or commits. |
git show <commit_hash> | Shows details of a specific commit. |
Remote operation commands help in connecting, pushing, pulling, and fetching from remote repositories.
Command | Description |
git remote -v | Lists 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 commands allow users to save uncommitted changes temporarily, ensuring a clean working directory.
Command | Description |
git stash | Temporarily stores changes. |
git stash apply | Restores the most recent stash. |
git stash clear | Deletes all stashed changes. |
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 tag | Lists all tags in the repository. |
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 commands standardize the workflow by creating branches for features, releases, and hotfixes.
Command | Description |
git flow init | Set 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. |
Below are common Git workflows with examples to simplify understanding.
git clone https://github.com/example/repo.git
cd repo
git init
git add file.txt
git commit -m "Add file.txt"
git checkout -b feature/new-feature
git add feature.txt
git commit -m "Add new feature"
git checkout main
git merge feature/new-feature
git push origin main
git pull origin main
Git Bash provides a Linux-style terminal interface for Git operations. It also supports basic shell commands:
Command | Description |
ls | Lists files and directories. |
cd <directory> | Changes to the specified directory. |
pwd | Prints the current working directory. |
git status | Displays the current state of the repository. |
git log | Shows the commit history. |
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 main | Pushes commits to GitHub and sets upstream. |
Forking & Pull Requests | Create a fork and submit contributions. |
Issue Tracking | Track and manage project tasks. |
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