The Git Commands Every Developer Should Know

Meysam Mahfouzi
7 min readMar 8, 2023

I’ve found that 90% of the time, I rely on a handful of essential Git commands to manage my codebase. In this article, I’ll share the Git commands that I use most frequently, and by mastering them, you’ll be able to handle the majority of situations that arise in your development work.

Cloning

First things first, we need to clone a git repository:

git clone git@github.com:namespace/cool_project.git

Getting The Latest Changes

Pull or Fetch? That is the question!

Suppose you cloned a git repository one month ago, and now you want to work on it. Before making any changes, it’s important to ensure that you have the latest version of the code.

To get the latest changes, simply run the following command:

git pull

To be honest, I’ve never used the git fetch command, and I don’t think I’ll ever use it!

The git fetch command retrieves the latest changes from the remote repository but does not automatically merge them into your local repository. Instead, you must run the git merge command separately to incorporate the changes.

Creating a new branch

If you want to add a new feature or fix a bug in the code, it’s important to create a new branch locally before making any changes.

To create a new branch from the main branch, first make sure you are on main branch and it's up to date:

git checkout main # switch to branch main
git pull # get the latest changes

Then create a branch from the main branch:

git checkoub -b branch-name

We also want to have this branch remotely:

git push -u orign branch-name

Compare, Commit and Push

After modifying the files, we have to add and commit the changes, and then push them to the repository:

git add /path/to/file1 /path/to/file2 ...
git commit -m "Add the API client"
git push

Note that:

  • Instead of adding files…

--

--