Youssef Nader | 21 Mar, 2023

23 Basic Git Commands for Beginners in 2024 (With Examples)


As a software developer, mastering Git is essential to easily manage your codebase, track changes, collaborate with team members, and deploy code to production environments.

To learn Git, you need to start with basic Git commands. That's why we've compiled a comprehensive Git commands list covering 23 basic Git commands with examples.

We’ve covered everything from initializing a local repository to pushing changes to a remote server. We also provide explanations of each command alongside examples to help you understand real-world use cases.

Whether you’re an aspiring developer or an experienced dev that’s looking to expand their knowledge of version control, this article will help you use Git to work more efficiently, collaborate more effectively, and produce better code. Let’s dive in!

Learn Git at Udemy

23 Basic Git Commands for Beginners

Let’s dive into 23 basic Git commands to help you unleash its true potential. Quick note, if you’re new to Git, you should consider creating a GitHub account, as this online service allows you to store and manage your code using Git. Also, make sure you have Git installed on your laptop.

1. Git init

The git init command initializes Git on the working directory it is invoked. In addition, the command creates a hidden .git folder that contains all the directories and files used by git to manage the project.

Git Command Syntax:

git init

To see this in action, navigate to a folder you want to use with Git. I’m using a folder called MyWebsite. Initialize an empty git repository in your folder using the command git init as shown below.

Git init

Initializing a Git repository using git init

2. Git config

The git config command sets or retrieves git configuration variables on system, global, or local levels. This can be used to set user information, set up remote URLs, configure aliases, and customize Git output. Note, that if you add no configuration options, then the changes will be applied at the local branch or repo level.

Git Command Syntax:

git config 

Let’s look at a simple example where we change the user email at the global level. We simply run the following config command and add a global flag after two hyphens. We finally provided the new email, and we’re all set.

Git Command Example:

git config --global user.email example@example.com

git config

Using git config to change global email

3. Git clone

The Git clone command is used to clone (hence the name) an existing repository. Cloning creates a new folder on your local computer, allowing continued development and collaboration. 

A great example of using the clone command is when a new engineer joins a company and needs to clone an existing repo that the team has been working on. This will allow them to contribute to the codebase.

Git Command Syntax:

git clone <repo_url>

Let's take a look at a real-world example by cloning a repo from FreeCodeCamp's GitHub page. All we need to do is enter the below command to clone this repo to our local machine. If you're unsure where to find a repo URL on GitHub, click on the code button to reveal the HTTPS URL for the repo, as shown below.

Git Command Example:

git clone https://github.com/freeCodeCamp/freeCodeCamp.git

git clone

Using git clone to clone a GitHub repository

repo URL

Copying the repo URL from GitHub to clone it

4. Git fetch

The git fetch command fetches updates from a remote repos without merging them with the local repo. This can be useful when we want to view contributions made by others but we’re unsure about what work has already been done, thus preventing duplicated efforts

Git Command Syntax:

git fetch

For example, if I had a repo where my teammate had made specific changes, I could use the git fetch command to retrieve the changes without merging them, as shown below.

git fetch

Using git fetch to fetch changes from a remote Git repository

5. Git merge

The git merge command combines changes made from two or more branches into a single branch. This command is commonly used when merging feature branches to the main branch.

For example, if you’d built a new feature on a separate branch that you want to release to the public, you would merge the code from the feature branch to the main branch.

To use this command, switch to the feature branch where you’ve made changes you want to merge, then enter the command below. All the extra code and files you added to the feature branch will automatically transfer to the main branch!

Git Command Syntax:

git merge <name_of_branch_to_merge>

6. Git branch

The git branch command is used to create and manage branches. Git branches are separate copies of your main codebase that you can change without disturbing the main code. Your main branch is the one you initialized using git init. 

If you want to build a new feature but don’t want to alter a live product (which could end in disaster), you can create a new branch (copy) of your main product using the syntax below. You can then build your feature there and merge it into the main branch when ready.

Git Command Syntax:

git branch <branch_name>

You can also list all active branches with the command shown below.

Git Command Syntax:

git branch

git branch

Viewing all git branches using git branch

7. Git rebase

The git rebase command applies changes from one branch to another, as shown in the syntax below. In this syntax, base refers to the branch where you want to apply changes to.

Git Command Syntax:

git rebase <base>

If you’re unfamiliar with rebasing, think of it as building two Lego castles, where one is currently taller. You can add Lego layers from the taller castle to the shorter one. This is how a git rebase works.

But instead of Lego, you add git commits from one branch to another. Note that sometimes, commits may not merge perfectly, leading to conflicts and commits that need modifying or removing.

8. Git checkout

The git checkout command allows users to switch from one branch to another. For example, a project can have two branches called dashboard and charts. After working on charts, a user can use git checkout to change the dashboard branch.

Git Command Syntax:

git checkout <branch_name>

This command can also be used to switch between branches with different codebase versions or to navigate to a specific commit, as shown in the syntax below.

Git Command Syntax:

git checkout <commit_id>

So, if we want to switch to our main branch, we can run the checkout command with a branch_name of main, as shown below.

Git Command Example:

git checkout main

git checkout main

Switching to the main branch using git checkout

9. Git remote

The git remote command allows you to manage remote repos in Git. A remote repo is like a backup folder on another computer that you can use to store copies of your code. The most common remote repo used by developers is GitHub.

The general syntax for git remote is shown below, where you can see the command is followed by a subcommand, optional parameters, and the name of the remote repo. 

Git Command Syntax:

git remote [subcommand] [options] [remote_repo_name]

One example of git remote involves the add subcommand followed by the name and URL of the remote repo, as shown below.

Git Command Syntax:

git remote add<name><url>

Another example is to remove a remote repository with the rm subcommand followed by the repo name, as shown below.

Git Command Syntax:

git remote rm <name>

Let’s take a look at a real example where I have created a remote repo on GitHub for MyWebsite. I then used the git remote command to add this remote repo, allowing me to link the local repo to the remote repo.

Git Command Example:

git remote add origin 
git@github.com:namancoderpro/git-commands-article-mywebsite.git

git remote add origin

Linking my local repo to a remote repo with git remote

10. Git pull 

The git pull command helps you fetch and merge changes from a remote repository to your local computer. Running the git pull command ensures you are working on the latest codebase version, including all recent changes made by any contributors. 

Git Command Syntax:

git pull <remote_name> <branch_name>

This git command performs two key actions at the same time, namely git fetch and git merge. Meaning it retrieves the latest changes and then merges these with code in your local directory, as shown below.

git pull

Pulling changes from a remote repo using git pull

11. Git add

The git add command adds new files from the working directory to the staging area, which is a temporary location where changes are prepared before being committed to a repo.

Git Command Syntax:

git add <file_name>

If you want to add everything inside the working directory to the staging area, you can use a period rather than a file name, as shown below.

Git Command Syntax:

git add .

After you’ve added your files to the staging area, you can run the git commit command, which we’ll cover next.

12. Git commit

When you’ve added files to the staging area, the next thing is to run the git commit command, as shown below. Think of commits as snapshots of your code at a specific time that you can use to track changes and collaborate with other teams.

The -m flag in the syntax below indicates a message describing your changes. Note that it’s always best practice to write a clear and descriptive commit message.

Git Command Syntax:

git commit -m "message"

git commit

Committing changes using git commit

13. Git push

The git push command is used to upload local changes to a remote repo. This is useful if you’ve created a new feature on your local machine that you want to share with your team. With the git push command, you can send changes to a remote repo like GitHub, allowing other developers to access it. 

The general syntax for git push requires you to specify the remote_repo_name and the local branch_name, as shown below.

Git Command Syntax:

git push <remote_repo_name> <branch_name>

If you want to overwrite remote changes with your local changes, you can use the optional --force flag, as shown below. Be careful with this option, as it can easily overwrite important changes from other developers.

Git Command Syntax:

git push --force <remote_repo_name> <branch_name>

git push

Pushing changes to a remote Git repo using git push

14. Git diff

The git diff command compares changes made to files in the working directory with those in the staging area. If you’ve updated a file on your computer but are unsure about the changes you’ve made, use this command to highlight changes in a file, as shown below.

Git Command Syntax:

git diff <filename>

git diff

Git highlighting changes with the git diff command

You can also use this git command to compare changes in two branches, as shown below.

Git Command Syntax:

git diff <branch-name-1> <branch-name-2>

15. Git log

The git log command shows the history of the repository. This shows every commit alongside the author, date, messages, and unique SHA-1 hash. The history is shown in reverse order, with the latest commits appearing at the top. 

To use the git log command, navigate to the local repo directory and use the below syntax.

Git Command Syntax:

git log

git log

Using git log to view commit history

16. Git tag

The git tag command can be used to create, list, delete, and verify tags. Tags are references that point to a specific point in a repo and are often used to mark important commits. To create a tag, use the command shown below.

Git Command Syntax:

git tag <tag_name> <commit_hash>

git tag

Using git tag to make a new tag

You can also list all available tags by omitting the tag name and commit hash, as shown below.

Git Command Syntax:

git tag

And if you want to delete a tag, use the -d flag, as shown in the syntax below.

Git Command Syntax:

git tag -d <tag_name>

17. Git status

The git status command displays the current state of the local repository, including any changes that you’ve staged for a commit.

Git Command Syntax:

git status

The git status command will also show you the branch you are currently running on and whether your local repository is up to date with a configured remote repository.

git status

Showing the current Git status with the git status command

18. Git show

The git show command shows information about a given object in Git. This can be commits, blobs, tags, or trees. With the git show command, you can also view the details of a commit, including the author, changes, and the commit message. 

To use the git show command, enter the command below followed by the object hash you’d like to view.

Git Command Syntax:

git show <object_hash>

git show

Using git show to view details of recent commit

To view the details of the most recent commit, you can use the HEAD keyword, as shown below.

Git Command Syntax:

git show HEAD

19. Git stash

The git stash command allows you to save uncommitted changes. This is useful if you’ve made changes to a current branch but are not ready to commit them. 

Git Command Syntax:

git stash

git stash

Using the git stash command to save uncommitted changes

You can also add the optional save parameter to append a message, as shown below.

Git Command Syntax:

git stash save "your message"

And when you’re ready to return to your current branch and your uncommitted changes, you can resume where you left off by using the git stash command with the apply parameter.

Git Command Syntax:

git stash apply

20. Git revert

The git revert command offers a safe way to revert changes in Git. Let's say you’ve realized that a commit contains a bug, and you want to undo changes made by the commit, but you do not want to delete it altogether.

To solve this issue, simply enter the git revert command followed by the commit hash to undo changes while keeping the project’s revert history.

Git Command Syntax:

git revert <commit_hash>

21. Git reset

The git reset command undoes changes to a local branch or git repository. This is useful for undoing changes that you have not committed. There are three main ways to use the git reset command.

The first involves unstaging changes to a file, which removes the file from the staging area but retains the changes made. To do this, use the git reset command followed by the HEAD keyword and the file name, as shown below.

Git Command Syntax:

git reset HEAD <file_name>

The second way to use this command is to discard all changes made to the working directory since the last commit. To do this, use the git reset command followed by the optional --hard flag, as shown below.

Git Command Syntax:

git reset --hard

git reset

Usage of git reset

The third way to use git reset is to move the HEAD pointer to a certain commit. This effectively reverts a repo to a specific commit, and any changes made after that commit are effectively lost. To do this, use the git reset command followed by the commit hash.

Git Command Syntax:

git reset <commit> 

IMPORTANT: Use the git reset command cautiously, as it can easily discard important changes that other contributors have made. 

22. Git rm

The git rm command removes files or directories that are tracked by Git. When you execute this command, it does not delete the file, it just tells Git not to track the file. Simply enter the command followed by the filename, as shown below.

Git Command Syntax:

git rm <file_path>

git rm

Using the git rm to stop Git tracking a file

23. Git clean

The git clean command is ideal for cleaning a directory of any untracked files that you no longer need in your repo. It’s also a really simple command, as shown below.

Git Command Syntax:

git clean

If you want to check for untracked files without deleting them, you can add an optional -n flag, as shown below.

Git Command Syntax:

git clean -n

You can also use the optional -dflag to include directories in the clean operation, as shown below.

Git Command Syntax:

git clean -d

And if you use the optional -fflag, you can forcefully delete files without warning, as shown below.

Git Command Syntax:

git clean -f

git clean

Using the git clean command to forcefully clean a repo

What is a Version Control System?

A Version Control System (VCS) is a software tool to manage changes in a codebase, which is useful for large projects with many developers or even for solo projects. Let’s look at some key benefits of a VCS.

  • Track changes made
  • Protects from human error
  • Easy to fix and debug problems via earlier codebase versions
  • Provides a detailed project history for better management
  • Ability to work on various parts of a project asynchronously
  • Easy developer identification in case of errors
  • Enables easy software testing

Version control systems range from basic file-based systems to complex distributed systems like Git. Typically, they provide features like branching and the ability for teammates to work on separate code copies. Let’s look at a few options for a VCS.

  • Git: This is, hands down, the most popular VCS solution. It’s ideal for teams of all sizes, free, and requires minimal configuration to get started.
  • CVS (Concurrent Version Systems): This was developed in 1986 and is typically best for smaller teams as it can become slow as project size increases. 
  • SVN (Subversion): SVN is a centralized VCS, making it the opposite of Git. It has one central repository where everyone makes changes, meaning it’s faster than Git.
  • Mercurial: Mercurial is a distributed VCS built in Python. It’s much simpler than Git but lacks certain aspects, like branching. It’s also less popular than Git.

What is Git?

Created by Linus Torvalds in 2005 while working on the Linux kernel, Git is the most popular distributed VCS, and it’s widely used in software development to manage and track changes to source code over time.

Git allows multiple developers to work on the same codebase simultaneously without interfering with each other's work. Unlike other version control systems, Git does not rely on a central repository to manage code changes but is a distributedVCS. This allows each dev to have a copy of the master repository (or repo), enabling them to work independently. 

Let’s look at the key pros and cons of Git.

Pros

  • Many people can work on a repo without creating merge conflicts
  • Ability to quickly try, test, and keep the best changes
  • Ability to work offline on your own local branch or repo, boosting productivity

Cons

  • Can lead to working in isolation, creating issues later on
  • Less secure as any developer can clone the repository

Need to brush up on Git for an interview? Check out these

Git Interview Questions & Answers

Conclusion

Git is an indispensable tool for software development, and its popularity continues to grow among developers worldwide. 

This article covers 23 basic Git commands you need to know in 2024, including general syntax and real-world examples for Git add commands, Git branch commands, Git commands to push code, Git history commands, and many more.

By mastering this list of Git commands, you can streamline your workflow, collaborate more effectively, and improve the quality of your code. You can also avoid common mistakes and recover from errors quickly by leveraging the powerful features of Git. 

Want to boost your Git skills in 2024? Check out our 

Git Cheat Sheet

Frequently Asked Questions

1. What Are the Git Commands?

Git commands are tools that enable users to manage changes to their code, collaborate with others, and synchronize their code with remote repositories. Some common Git commands include git add, git commit, git push, git pull, git clone, and more.

2. What Is Git and Basic Git Commands?

Git is a popular version control system used to manage changes to code, with basic commands including git init, git add, git commit, git push, git pull, git clone, git branch, git checkout, git merge, git fetch, git remote, and git rebase. These help developers leverage the full power of git.

3. How to Write Commands in Git?

If you have Git installed, navigate to the directory where your Git directory is located. The basic structure for writing Git commands is git <command> <options> <arguments>.

4. What Are the Git Commands in Order?

One potentially logical order for git commands is: git init, git status, git add, git commit, git remote, git push, git clone, git pull, git merge, git branch, git fetch, git checkout, and git rebase.

5. How to Commit Code in Git?

To commit all changes, use git add . then commit the file using the command git commit -m.

6. How Do I Commit a File in Git?

Add the file to the staging area using git add <file>, then commit the file using the command git commit -m "<commit message goes here>.

People are also reading:

By Youssef Nader

Youssef Nader, Computer Engineering Student at Cairo University. Technology technical writer and blogger, full-stack Web developer, specializes in rails and node. Founder of Yadawy, an E-commerce platform under construction. AI enthusiast, loves reading, traveling and martial arts.

View all post by the author

Subscribe to our Newsletter for Articles, News, & Jobs.

I accept the Terms and Conditions.

Disclosure: Hackr.io is supported by its audience. When you purchase through links on our site, we may earn an affiliate commission.

In this article

Learn More

Please login to leave comments