Essential Git Commands Every Developer Should Know

Rashad Ansari
7 min readJun 27, 2023

--

Summary

In the article, we begin by introducing a basic set of essential commands that every developer needs to understand. These commands are like building blocks that form the groundwork for effectively using Git. Afterward, we delve into a collection of more advanced commands that can be incredibly useful in different situations and workflows. By familiarizing yourself with these advanced commands, you’ll have a valuable toolkit to handle a variety of scenarios when working with Git.

Covering the Basics

As a developer, there are several essential basic Git commands you should know to effectively work with version control and collaborate on projects. Here are some of the most important Git commands that you are probably aware of:

  1. git init: Initializes a new Git repository in the current directory.
  2. git clone <repository>: Creates a local copy of a remote repository on your machine.
  3. git add <file>: Adds a file or changes to the staging area, preparing them to be committed.
  4. git status: Shows the current status of the repository, including any modified, added, or deleted files.
  5. git commit -m “Commit message”: Commits the changes in the staging area to the repository, with a descriptive commit message.
  6. git pull: Fetches and merges changes from a remote repository into the current branch.
  7. git push: Pushes committed changes from a local repository to a remote repository.
  8. git branch: Lists all existing branches and shows the current branch.
  9. git checkout <branch>: Switches to the specified branch.
  10. git merge <branch>: Merges changes from a different branch into the current branch.
  11. git stash: Temporarily saves changes that are not ready to be committed, allowing you to switch branches without losing your work.
  12. git stash pop: Restores the most recently stashed changes and removes them from the stash.
  13. git log: Displays a chronological list of commits, including commit messages and other details.
  14. git diff: Shows the differences between the working directory and the staging area.
  15. git reset <file>: Removes a file from the staging area, but keeps the changes in your working directory.
  16. git revert <commit>: Reverts the changes made in a specific commit and creates a new commit to record the reversal.
  17. git rm <file>: Deletes a file from both the working directory and the repository.

While there are many other commands you may use in your daily work, the ones mentioned above are some of the most commonly used and well-known among developers. It’s likely that you are already familiar with and have used these commands before.

Rebase

The git rebase <branch> command and the git merge <branch> command are both used to incorporate changes from one branch into another, but they do so in different ways.

When you use git merge <branch>, Git combines the changes from the specified branch into the current branch. It creates a new "merge commit" that incorporates the changes from both branches. This results in a commit history that shows the individual branch names and their respective commits.

On the other hand, git rebase <branch> works by moving or reapplying the commits from the current branch onto the tip of the specified branch. It essentially makes your current branch look like it was branched directly from the specified branch, as if the commits were made on top of the latest changes. This creates a linear commit history without the additional merge commits.

The main difference between the two commands lies in how they handle the commit history. git merge preserves the commit history of both branches, whereas git rebase modifies the commit history of the current branch.

In general, git merge is commonly used for integrating changes from one branch into another, especially when you want to keep a clear record of branch merges. git rebase, on the other hand, is useful when you want to maintain a cleaner and more linear commit history, as it incorporates the changes from one branch onto another as if they were applied directly.

Interactive Rebase

The git rebase -i or git rebase --interactive command allows you to perform an interactive rebase in Git. It provides a way to modify and rearrange commits during the rebase process, giving you more control over the commit history.

When you run git rebase -i <branch>, you open an interactive interface that presents a list of commits in your current branch. Each commit is represented by a line that begins with the word "pick". This interface allows you to modify the commits by rearranging, combining, or even removing them.

The interactive rebase interface allows you to perform various actions on the commits. Some common actions include:

  1. Reordering commits: You can change the order of commits by rearranging the lines in the interactive interface.
  2. Squashing commits: Combining multiple commits into a single commit helps to keep the commit history clean and concise. You can mark commits as “squash” or “s” in the interactive interface to merge them with the previous commit.
  3. Editing commits: You can modify the commit messages by marking commits as “edit” or “e” in the interactive interface. During the rebase process, Git will pause at these commits, allowing you to amend the commit message or make changes to the commit.
  4. Deleting commits: If you want to remove a commit from the history, you can simply delete the corresponding line in the interactive interface.

Once you’ve made the desired changes in the interactive interface, save and exit the file. Git will then proceed with the rebase, applying the modifications you specified.

Let’s consider the usage of the following command in a repository:

git rebase -i HEAD~2

This is what you observe:

As you can see, Git has lovely command line tools; you can find almost everything. Also, it has a charming error description, and if you look at the report, you can probably find out the problem.

Additional Use Cases and Scenario

In this section, we will explore common scenarios that developers may encounter and provide explanations on how to handle those situations.

Scenario 1 (amend)

You have made a commit to your branch and now you wish to make some changes without creating a new commit. In this case, you can amend your changes to your previous commit.

git commit --amend

Please be aware that amending a commit alters the commit history. If you have already pushed the commit to a remote repository, you will need to perform a force push to update your amended commit.

Scenario 2 (reset — soft)

You have committed changes to your branch and now you want to remove the commit without losing your changes.

git reset --soft HEAD~1

The command is used to undo the last commit in your Git repository while keeping the changes from that commit staged. It moves the branch pointer to the previous commit, effectively "uncommitting" the last commit.

Scenario 3 (reset — hard)

You made some changes in your current branch, but you have reconsidered and no longer wish to keep those changes. Since you haven’t committed your changes yet, you can simply discard them.

git reset --hard

If you have already committed your changes and you no longer want to keep them.

git reset --hard HEAD~1

Scenario 4 (cherry-pick)

You have a branch called branch1 with some commits, and another branch called branch2 with its own set of commits. You would like to apply a specific commit from branch1 to branch2 and incorporate it there.

git cherry-pick <commit>

This command is used to apply the changes from a specific commit to your current branch. It allows you to select and apply individual commits from one branch to another.

Scenario 5 (fetch)

You want to retrieve the latest commits and updates from a particular branch in the remote repository.

git fetch --origin <branch>

git fetch and git pull are both Git commands used to update your local repository with the latest changes from a remote repository. However, there are key differences between the two:

1. git fetch: This command retrieves the latest commits and updates from the remote repository but does not automatically merge them into your local branch. It updates your remote-tracking branches, allowing you to see the changes made in the remote repository. You can then choose to inspect the changes and decide how to incorporate them into your local branch using additional Git commands such as git merge or git rebase.

2. git pull: This command is a combination of two Git commands: git fetch followed by git merge. It automatically fetches the updates from the remote repository and merges them into your current branch.

When you run git pull, Git fetches the changes from the remote repository, updates the remote-tracking branches, and then merges the changes into your local branch. If there are no conflicts, the merge is performed automatically. However, if conflicts arise, you will need to resolve them manually.

Conclusion

Git is an indispensable tool for developers worldwide, utilized extensively in their day-to-day work. Its command line interface provides a wealth of information, allowing developers to navigate their repositories efficiently. Additionally, the error messages returned by Git often provide valuable insights into the root causes of issues, and occasionally even offer the correct commands to resolve them.

Throughout this article, we have covered several commonly used Git commands that every developer should be familiar with. It is important to note that the world of software development is constantly evolving, and new use cases for Git emerge regularly. As such, this article will be continuously updated to incorporate additional scenarios that are useful for developers.

We invite you, the readers, to actively participate in this ongoing process. If you have any specific scenarios in mind that you believe would benefit everyone, we encourage you to comment on this article. Your suggestions will be reviewed, and valuable contributions will be incorporated into the article, ensuring its relevance and usefulness for developers worldwide.

--

--

Rashad Ansari

Curious and continuously learning software engineer, driven by crafting innovative solutions with passion. Let’s collaborate to shape a better future!