Tools2023-04-27

25 Essential Git Commands for Developers

Explore the 25 essential Git commands for developers. Know Git commands like 'git commit,' 'git merge,' and more to enhance your development workflow.
Essential Git Commands for Developers

Git is one of the most popular version control systems used by software developers today. It allows teams to work collaboratively on a project, keep track of changes, and manage versions of their code. Git has a command-line interface (CLI) that can seem intimidating at first, but once you master the basics, you can accomplish a lot more with Git.

In this article, let’s take a look at the top 25 Git commands you should know to master Git CLI, these commands cover the most useful Git commands that are often used.

Essential Git Commands and Its Examples

Let’s explore these commands and provide examples to help you grasp their utility.

1. Git init

Initialize a new Git repository in the current directory

The first command you'll use when starting a new Git repository is git init. This command initializes an empty Git repository in the current directory.

Before you can start tracking changes in your project, you need to initiate a Git repository. The git init command creates a hidden .git directory in your project's root folder, setting up the repository's structure.

Example:

git init

2. Git commit

Record staged changes in the repository's history

The git commit command records the changes made to the repository. You can add a message to describe the changes you've made.

Commits are at the heart of version control. They represent a snapshot of your project at a specific point in time. The git commit command allows you to save your changes with a descriptive message, making it easy to track progress.

Example:

git commit -m "Added login functionality"

3. Git branch

List, create, or delete branches

The git branch command lists all the branches in your repository. You can create new branches, delete branches, and switch between branches.

Branches in Git enable parallel development, allowing multiple features or bug fixes to be worked on simultaneously. You can create, list, or delete branches using the git branch command.

Examples:

git branch                       # List branches
git branch new-feature # Create a new branch
git branch -d obsolete-branch # Delete a branch

[Read more: Best Git Branching Strategies For Engineering Teams]

4. Git checkout

Switch between branches or commits

The git checkout command allows you to switch between branches or to a specific commit. You can also use it to create a new branch from an existing one.

The git checkout command is versatile. It allows you to switch between branches, restore files, and even travel back in time by checking out a specific commit.

Examples:

git checkout branch-name     # Switch to a different branch
git checkout commit-hash # Check out a specific commit

5. Git merge

Merge changes from one branch into another

The git merge command allows you to merge changes from one branch into another. It's useful when working on a team, as it allows you to combine your changes with those of your colleagues.

When a feature or bug fix is complete in a branch, you can integrate those changes into another branch, typically the main branch, using the git merge command.

Example:

git merge command

6. Git pull

Fetch changes from a remote repository and merges them into the current branch

The git pull command fetches changes from a remote repository and merges them with your local repository.

Collaborating with others often involves fetching their changes from a remote repository and incorporating them into your local branch. The git pull command simplifies this process.

Example:

git pull origin main

7. Git remote

Manage remote repositories

The git remote command allows you to manage the remote repositories associated with your local repository. You can add new remote repositories, remove them, or rename them.

With Git, you can collaborate with developers worldwide by connecting your local repository to remote repositories. The git remote command helps you manage these remote connections.

Example:

git remote add origin https://github.com/user/repo.git

8. Git clone

Create a local copy of a remote repository

The git clone command creates a copy of a remote repository on your local machine. You can use it to start working on a project that has already been created.

Example:

git clone https://github.com/user/repo.git

9. Git log

Display the commit history

The git log command shows a history of commits made to the repository. It includes the commit message, author, date, and other details.

The commit history contains a chronological record of all changes made to the repository. The git log command offers a detailed view of this history, including commit messages, authors, and dates.

Example:

git log

[Read more: Git Log Cheatsheet For a Productive 2023]

10. Git fetch

Download objects and refs from another repository

The git fetch command downloads the latest changes from a remote repository but doesn't merge them with your local repository.

The git fetch command allows you to retrieve changes from a remote repository without merging them immediately. This is useful when you want to review changes before incorporating them.

Example:

git fetch origin

11. Git blame

Show who made changes to a file and when

This is a git command that allows developers to determine who last modified a particular line of code in a file, as well as when the modification was made. This command is useful for identifying the author of a particular change, and for investigating the history of a file.

The git blame command helps you pinpoint when and by whom each line in a file was last modified. It's a valuable tool for tracking changes and identifying authors responsible for specific code.

Example:

git blame file.txt

12. Git stash

Temporarily save changes that are not ready for commit

This git command allows developers to temporarily save changes to the working directory without committing them to the Git repository. This command is useful for saving changes that are not yet ready to be committed, such as unfinished features or experimental code. Git stash can help developers keep their code organized and easily switch between different code states.

When you're in the middle of a task but need to switch branches or work on something else, the git stash command allows you to save your changes without committing them, preventing loss of work.

Example:

git stash command

13. Git tag

Create, list, or delete tags

The git tag command allows developers to assign a permanent name and version number to a specific commit in the git repository. This command is useful for marking important milestones in the development process, such as the release of a new version of the software. Git tags are typically used to mark stable or production-ready versions of the code and can be used to reference specific versions of the software in the future.

Tags are used to mark specific points in your project's history, often indicating release versions. The git tag command lets you manage these tags.

Example:

git tag v1.0.0

14. Git cherry-pick

Apply specific commits from one branch to another

This command is useful for developers to apply a specific commit from one branch to another branch. For the selectively applying of changes from one branch to another, such as when a bug fix or feature improvement is needed in a specific branch. Git cherry-pick can help developers save time and avoid merging unwanted changes from other branches into their current branches.

Sometimes you need to selectively apply commits from one branch to another, such as when porting a bug fix. The git cherry-pick command makes this process straightforward.

Example:

git cherry-pick commit-hash

15. Git rebase

Reapply commits on top of another base tip

This command is used to modify the commit history of a branch by moving or reordering existing commits. It is a more powerful alternative to Git Merge and can help keep the commit history cleaner and more organized.

Rebasing is a technique used to integrate changes from one branch into another by moving or combining commits. The git rebase command is powerful but should be used with caution.

Example:

git rebase main

16. Git bisect

Find the commit that introduced a bug

This command is used for binary search debugging. It allows developers to find the commit that introduced a bug by automatically testing each commit in a given range until the faulty commit is found.

Debugging can be challenging, especially when trying to pinpoint the exact commit that introduced a bug. The git bisect command helps you identify the culprit commit by using binary search.

Example:

git bisect command

17. Git worktree

Manage multiple working trees

This command is used to create multiple working trees from a single repository. This can be useful when working on multiple branches or when multiple team members need to work on the same repository simultaneously.

Git allows you to work on multiple branches simultaneously by creating separate working trees. The git worktree command facilitates this process.

Example:

git worktree add ../new-feature-branch feature-branch

18. Git reflog

View the reference log

This command is used to show a log of all Git actions that have been performed on a repository, including actions that have been undone or lost due to rebase or reset.

The reference log, accessed via the git reflog command, records the history of Git references, including branch creations, checkouts, and commits. It's useful for recovering lost commits or branches.

Example:

git reflog

19. Git rerere

Reuse recorded resolution of conflicted merges

This command is used to automatically resolve merge conflicts by remembering how conflicts were resolved in the past and applying the same resolution to future conflicts.

Git can remember how you resolved merge conflicts and apply the same resolutions automatically in the future. The git rerere command manages this functionality.

Example:

git rerere command

20. Git submodule

Include external Git repositories as submodules

This command is used to include another Git repository as a subdirectory of the current repository. This can be useful when working on a project that depends on multiple repositories.

When you want to include another Git repository inside your project, the git submodule command helps you manage these dependencies.

Example:

git submodule add https://github.com/user/dependency.git

21. Git filter-branch

Rewrite branch history

This command is used to rewrite Git history by applying a filter to all commits in a repository. This can be useful for removing sensitive data from a repository or for simplifying the commit history.

The git filter-branch command is a powerful yet advanced tool that allows you to rewrite the history of a Git branch. You can use it to apply various filters, such as removing sensitive data or extracting a subdirectory into a new repository. However, it should be used with caution, as it can have a significant impact on your repository's history.

Example:

git filter-branch --tree-filter 'rm -f secrets.txt' HEAD

[Read more: GitHub Command Cheatsheet For 2023]

22. Git update-index

Update the Index

This command is used to update Git's index, which tracks the contents of the working directory. This can be useful when manually modifying the working directory and Git needs to be notified of the changes.

The git update-index command is useful when you want to manually update the Git index without changing the working directory or committing any changes. It's typically used in advanced scenarios where fine-grained control over the index is required.

Example:

git update command

23. Git gc

Garbage Collection

This command is used to optimize and clean up Git's internal database. This can be useful for improving the performance of Git commands and for freeing up disk space.

Git uses a mechanism called "garbage collection" to optimize and clean up your repository. The git gc command triggers this process, which helps reduce repository size and improve performance. In most cases, Git runs garbage collection automatically when needed, so manual execution is rarely necessary.

Example:

git gc

24. Git instaweb

Start a Web-Based Git Interface

This command is used to launch a local web server that displays a repository's commit history and file contents in a web browser. This can be useful for quickly browsing a repository's contents without needing to use the command line.

The git instaweb command launches a web-based interface for your Git repository using a web server. While this feature can be useful for quick inspection of your project's history, it's not a common command and is considered somewhat deprecated in favor of more feature-rich Git hosting solutions.

Example:

git instaweb

25. Git submodule foreach

Execute a Command in Each Submodule

This command is used to execute a Git command in each submodule of a repository. This can be useful when working with a repository that contains multiple submodules and you need to perform a task on all of them at once.

When working with Git submodules—external Git repositories included within your own repository—the git submodule foreach command comes in handy. It allows you to execute a specified command in each submodule, making it easier to manage and update dependencies across multiple submodules.

Example:

git submodule foreach 'git pull origin master'

[Read more: Top Git Extensions For VS Code in 2023]

Git Commands: Elevate Your Efficiency in Code Management

These commands include the most used and also the less commonly used Git commands but can be very useful in certain situations. By mastering these Git commands, you can work more efficiently and effectively with Git and manage your codebase with ease.

However, simply using Git, might not be enough to get the best out of your project space, an engineering analytics tool can equip you with the data-driven insights you need to place your team on top. Hatica offers metrics across 13 dashboards, powered by CI/CD tools, JiraGitHub, Azure, CircleCI, and GitLab. By collating tool activities at one place, Hatica helps teams streamline their workflow and improve productivity.

Subscribe to the Hatica blog today to read more about unblocking developers, and boosting productivity with engineering analytics. 

Subscribe to Hatica's blog

Get bi-weekly insights straight to your inbox

Share this article:
Table of Contents
  • Essential Git Commands and Its Examples
  • 1. Git init
  • 2. Git commit
  • 3. Git branch
  • 4. Git checkout
  • 5. Git merge
  • 6. Git pull
  • 7. Git remote
  • 8. Git clone
  • 9. Git log
  • 10. Git fetch
  • 11. Git blame
  • 12. Git stash
  • 13. Git tag
  • 14. Git cherry-pick
  • 15. Git rebase
  • 16. Git bisect
  • 17. Git worktree
  • 18. Git reflog
  • 19. Git rerere
  • 20. Git submodule
  • 21. Git filter-branch
  • 22. Git update-index
  • 23. Git gc
  • 24. Git instaweb
  • 25. Git submodule foreach
  • Git Commands: Elevate Your Efficiency in Code Management

Ready to dive in? Start your free trial today

Overview dashboard from Hatica