Basic Git Commands Cheat Sheet (With Examples)

by Inflectra on

Basic Git Commands Explained: Cheat Sheet With Examples

What is Git?

Git is a free and open-source version control system for tracking changes in files and code that allows multiple developers to work on the same codebase simultaneously. It tracks changes to files over time, enabling teams to collaborate efficiently, maintain a detailed history of changes, and revert to previous versions if needed. It's particularly useful for software development, where multiple people collaborate on the same codebase.

Git Syntax

Git commands typically follow a specific format:

git <command> [options] [arguments]

  • git: The program itself, telling your system that you want to use Git.
  • command: Specifies the action you want to perform (we’ll cover these more in a second).
  • options: Optional flags that modify the command's behavior (such as -m for adding a commit message).
  • arguments: Provide additional details to the command (like the relevant filename or repository URL).

Basic Git Commands

Working with Git commands is a fundamental skill for any developer contributing to a project. Let’s explore some of the most common and essential Git commands for beginners that you should know:

Commands for Local Repositories

Git utilizes two types of repositories — local and remote. A local repository is your personal workspace on your machine. It holds all the project files, version history, and commit information. The local repository is essentially your private workspace, where you can work on your code without immediately affecting any shared or remote repositories. Here are some common commands used with local repositories:

git add

Used to stage changes in the working directory, preparing them for the next snapshot (commit) of your project. It tells Git to start tracking these changes and is typically followed by git commit to save the staged changes.

Example: You've finished working on a new feature and want to include it in your next commit. You would use git add filename.py to stage that specific file. Alternatively, git add . adds all modified files in the current directory.

git branch

Creates, lists, renames, or deletes branches in your Git repository. Branches are lightweight versions of your project history that allow you to experiment with features or isolate bug fixes without affecting the main codebase.

Example: To create a new branch called "feature-x," you can use git branch feature-x. This command alone doesn't switch to the new branch; you need to use git checkout for that.

git init

Creates a new Git repository in your current directory, which is the first step to start tracking files and committing changes.

Example: If you have a project folder that is not yet under version control, you can navigate to that folder and run git init to create a new Git repository.

git commit

Saves changes to the local repository at a particular point in time with a message or note provided by you. Commits represent milestones in the development process and create a record of the project's history.

Example: After staging changes with git add, you can use git commit -m "Added new feature X" to commit those changes. The -m flag lets you specify a meaningful message describing your commit, creating a snapshot of your project with that message.

git status

Provides a quick overview of the current state of your Git repository — which files are modified, staged (added), or untracked (not yet added to Git). This command is useful for checking the status of your repository before committing changes or switching branches.

Example: You're unsure what changes are currently tracked by Git. Running git status will display information about modified files, letting you decide which ones to stage for the next commit.

git config

Configures various Git settings, both globally and for the current repository. This allows you to personalize your Git experience, like defining your name and email for commits.

Example: To have Git automatically include your name and email in all your commit messages, you could use git config --global user.name "Your Name" and git config --global user.email "youremail@example.com".

git checkout

Switches between different branches in your Git repository. A branch allows you to experiment or work on a specific feature without affecting the main project line.

Example: You've created a new branch to fix a bug. After working on the fix, you can switch back to the main branch using git checkout main (assuming your main branch is named "main").

git merge

Combines changes from different branches into a single branch. This is helpful when collaborating with other team members who might be working on separate branches.

Example: Your teammate has fixed a bug in a separate branch and you want to integrate that fix into your main branch. You can use git merge bugfix-branch to merge the changes from the "bugfix-branch" into your current branch.

Commands for Remote Repositories

A remote repository, on the other hand, is a central vault hosted on a remote server, typically on code hosting platforms like GitHub, GitLab, or Bitbucket.

git remote

Manages connections between your local Git repository and remote repositories hosted on platforms like GitHub or GitLab. It allows you to add, list, remove, or rename these connections. You can also use git remote -v to list all the remotes associated with your local repository.

Example: You've created a project on GitHub and want to connect your local repository to it. You would use git remote add origin https://github.com/username/your-project.git to create a remote named "origin" that points to the URL of your GitHub repository.

git clone

Creates a copy of a remote repository on your local machine. This is a convenient way to download and start working on an existing project.

Example: If you want to work on an existing project hosted on a remote server like GitHub, you can use git clone https://github.com/username/repo.git to create a local copy on your machine.

git pull

Retrieves the latest changes from a remote repository and merges them into your local branch. This command is commonly used to keep your local repository up-to-date with the latest changes from the remote repository.

Example: You've been working on your local branch, and your teammates have made changes on the remote branch. You can use git pull origin main (assuming your remote branch is named "origin" and your main branch is "main") to download the latest changes from the remote "main" branch and merge them into your local "main" branch.

git push

Uploads your local commits to a remote repository. This is typically used after you've made changes and want to share them with collaborators or contribute to a project.

Example: You've fixed a bug on your local branch and want to share it with your team. You would use git push origin bugfix-branch to push your local "bugfix-branch" to the remote repository's corresponding branch.

git fetch

Downloads the latest changes from a remote repository without merging them into your local branch. This allows you to see what changes are available on the remote repository and decide whether to merge them into your local branch using git merge.

Example: You want to stay updated with the latest changes on the remote repository but don't want to merge them immediately into your local branch. You can use git fetch origin to download the changes without merging and then review them using git log to decide when to integrate them using git merge.

More Git Commands

While the commands we’ve covered so far provide a strong foundation, Git offers a treasure trove of additional functionalities. Here's a glimpse into some valuable commands for more advanced workflows:

git log

Displays the commit history of a repository, showing information like commit message, author, date, and hash (unique identifier) for each commit. It’s useful for reviewing the project's history, tracking changes, and understanding the evolution of the codebase.

Example: You're unsure when a particular bug was introduced. You can use git log to explore the commit history and identify the commit that might have caused the issue.

git diff

Shows the difference between two sets of changes, such as commits, branches, or the working directory and staging area. It highlights lines that have been added, modified, or removed.

Example: You've made changes to a file but are unsure of the exact modifications. You can use git diff to see the specific line-by-line changes.

git rm

Removes a file from your working directory and stages it for deletion in the next commit. It can be useful for cleaning up your repository and removing unnecessary files.

Example: If you want to remove a file called obsolete.py, you can use git rm obsolete.py to remove it from the repository and stage the removal.

git mv

Renames a file in your working directory and stages the rename for the next commit. This allows you to reorganize files or rename them in a way that preserves the file's history in the repository.

Example: If you want to rename a file from old_name.txt to new_name.txt, you can use git mv old_name.txt new_name.txt.

git cherry-pick

Selects and applies a specific commit from another branch onto your current branch. This is useful for selectively applying beneficial changes from another branch without merging the entire branch.

Example: A bug fix was committed on a separate branch. You can use git cherry-pick commit-hash (where commit-hash is the unique identifier of the fix) to apply that specific fix to your current branch.

git rebase

Reapplies commits from one branch onto another, effectively rewriting the commit history. This can be useful for cleaning up your branch history or integrating changes from another branch in a linear fashion, but should be used with caution (especially when working with shared branches).

Example: You have a feature branch that you want to integrate into the main branch, but the main branch has also progressed, you can use git rebase main on the feature branch to apply its commits on top of the latest main branch commits.

git tag

Creates a lightweight or annotated reference to a specific commit in your history. Tags are useful for marking important milestones or releases in your project’s development.

Example: You've completed a major feature and want to mark it with a tag. You can use git tag v1.0 to create a tag named "v1.0" that points to the commit representing that version.

git submodule

Integrates another external Git repository as a submodule within a parent repository. This is useful for incorporating external dependencies, shared code, or modular components into your project while maintaining separate version control for each component.

Example: If your project depends on a third-party library hosted in a separate repository, you can use git submodule add https://github.com/library/repo.git to add it as a submodule.

git reset

Discards changes in your working directory or staging area, or even moves your current branch pointer to a different commit. This is used to undo mistakes or revert to a previous state. However, it should be used with caution because it can potentially lead to permanent data loss if not used correctly.

Example: If you want to unstage a file that was previously added for commit, you can use git reset HEAD filename.ext.

git stash

Temporarily saves your changes in the working directory and staging area, allowing you to switch branches or work on something else without committing the changes. Stashing is useful for keeping your working directory clean and avoiding unnecessary commits while switching contexts.

Example: If you're working on a feature but need to switch to a different branch to fix a bug, you can use git stash to stash your current changes, switch branches, and then use git stash apply or git stash pop to reapply the stashed changes later.

git bisect

Performs a binary search through the commit history to identify the commit that introduced a specific bug or issue. This can be helpful for debugging issues by identifying the root cause of issues in the codebase.

Example: If you know that a bug was introduced somewhere in the last 100 commits, you can use git bisect start, mark the current commit as "good" or "bad", and then use git bisect good or git bisect bad to narrow down the search until the commit that introduced the bug is found.

git blame

Shows who last modified each line of code in a file, allowing you to track code ownership and understand historical changes.

Example: To see the commit information for each line of a file, you can use git blame filename.ext.

git reflog

Displays a log of all the references (branches, tags, etc.) and their movements in the repository, which is helpful for recovering lost commits or references that are not available in the regular commit history.

Example: If you accidentally delete a branch or want to recover a commit that was deleted, you can use git reflog to find the commit hash and then use git checkout commit_hash to restore it.

git clean

Removes untracked files (those not yet added to Git) from your working directory. This is another useful tool for cleaning up the working directory and removing unwanted files that are not part of the repository.

Example: If you have generated build artifacts or temporary files that you don't want to track, you can use git clean -n to preview the files that will be removed, and then git clean -f to actually remove them.

Empower Your Coding with Git Integrations

For larger teams where standalone file storage and management are necessary with tools like Git, it’s crucial to have testing and development tools that integrate seamlessly with this repository. TaraVault and Spira enable easy and secure access to Git repositories and enhance your development capabilities. Learn more about TaraVault or get started with a free 30-day trial today!

Spira Helps You Deliver Quality Software, Faster and with Lower Risk.

Get Started with Spira for Free

And if you have any questions, please email or call us at +1 (202) 558-6885

Free Trial