avatr of Ibrahim

Published 4th Mar 2023

Top 10 Git Commands Cheat Sheet

4 min read

Git is a tool that helps developers manage changes to their code over time. It's like a backup system for your code, allowing you to save different versions of your work as you make changes. This means you can always go back to an earlier version if you need to, or compare different versions to see what changed.

Git also helps developers collaborate on code projects with others. It allows multiple people to work on the same codebase at the same time without overwriting each other's work.

Git is widely used in the software development industry and is an essential tool for managing code changes and collaborating on code projects. While it can seem overwhelming at first, learning how to use Git is well worth the effort, as it will save you a lot of time and headaches in the long run.

Let's have a look at some of the most frequently used Git commands.

1. Git init

git init is a command that initializes a new Git repository. When you run git init, Git creates a new directory called a "repository" in your current working directory. This repository is where Git will keep track of all the changes you make to your code over time.

2. Git add .

git add . (with a period at the end) is a command that tells Git to add all the changes you've made in your current working directory to the staging area. This means that your changes will be included in the next commit.

You will notice, if you're using VSCode as the code editor that there will be an 'A' next to the files that have been added to the staging area i.e. after you've run the git add . command. All the files will be included in the next commit.

3. Git commit

When you run git commit, Git takes all the changes you've added to the staging area (using git add), along with a message you provide, and creates a new commit that includes those changes.

Git will prompt you to enter a commit message that describes the changes you've made. This message is important because it helps you and other developers understand what changes were made in this commit. So the command becomes git commit -m 'your message here'.

4. Git push

git push is a command that sends your local Git commits to a remote repository, such as GitHub or GitLab. When you run git push, Git uploads the changes you've made to the remote repository, making them available to other developers who are working on the same codebase.

git push is useful when you want to share your code changes with other developers or when you want to deploy your code to a production server.

5. Git pull

git pull is a command that retrieves the latest changes from a remote repository and merges them into your local repository.

When you have other developers working on the same repository, they may have pushed some changes, and so you may want to keep your local repository in sync/updated with the remote repository. When you run git pull, Git downloads any new changes that have been made to the remote repository and incorporates those changes into your local repository.

6. Git clone

git clone is a command that creates a copy of a remote Git repository on your local machine. When you run git clone, Git downloads all the files and folders from the remote repository and creates a new local repository that is an exact copy of the remote repository.

A typical command could be

git clone https://github.com/username/repo-name.git

Once you have a local copy of the remote repository you can start making changes to the code.

7. Git branch

git branch is mostly used for its 3 sub commands:

  • git branch -r - lists all the branches in the repository
  • git branch my-branch - creates a new branch named 'my-branch'
  • git branch -d old-branch - deletes the branch named 'old-branch'

A 'branch' in Git is essentially a parallel version of your code. When you create and switch to a new branch, you can make changes to the code in the new branch without effecting the main branch. You don't have to worry if you screw up. When you're ready and confident about the new version of the code you can merge the branch back onto the main branch.

8. Git checkout

git checkout is a command that allows you to switch to another branch. Running git checkout my-branch switches you to my-branch. This is useful when you want to switch between branches to work on different features.

You can also switch to previous commits to view or edit the code at that version in history if you provide the commit hash (commit id number): git checkout #commitHashNumber.

9. Git merge

git merge my-branch merges the changed code from my-branch onto the current branch.

Say you have a main branch and you've been working on a feature on a branch named navbar-links where you now want it to be 'merged' onto the main branch. You switch to the main branch by running git checkout main. You run git merge navbar-links.

If there are no code conflicts, Git will merge the navbar-link onto the main branch. If there are conflicts, Git will prompt you to resolve the conflicts before merging.

10. Git status

git status is a command that shows you the current status of your Git repository. When you run git status, Git will tell you which files have been modified, which files are staged and ready to be committed, and which branch you are currently working on.

Overall git status gives you an overall view of the repository. For example, if you have modified a file, but you haven't added it to the staging area, Git will tell you that the file is "untracked". If you have added a file to the staging area, Git will tell you that the file is "staged".

Thank you for reading. Let's connect!

Feel free to connect on Twitter or LinkedIn

© Ibrahim Al-Quraishi 2024