Top 10 GIT Commands for every developer

Top 10 GIT Commands for Every Developer
Chinmaya By Chinmaya
7 Min Read

Introduction

Git is an essential tool for developers, allowing them to track changes in their code, collaborate with team members, and maintain version control.

While Git offers a wide array of commands, there are some that every developer should have in their toolkit.

Whether you’re new to Git or looking to brush up on the basics, here are the Top 10 Git commands every developer should know.

GIT Config Commands

GIT config commands are those commands which are used to configure GIT in your local machine.

Some of the commonly used GIT config commands are:

				
					git config --global user.name "Your Name"
				
			

This above command – Sets the name associated with your Git commits.
Example: `git config –global user.name “Chinmaya Rath”`

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

This above command – sets the email address will be associated with your Git commits and will appear the UI of the systems like GitHub, GitLab and etc.. Example: `git config –global user.email “chinmayarath@example.com”`

GIT Essential Commands

After you have configured GIT in your local machine, you need to understand the essential commands for using it.

1. git init

The git init command is used to initialise a new Git repository in your project.
Running this command creates a hidden .git folder, which Git uses to track your project files.

				
					git init
				
			

When to use it?

  1. When starting a new project from scratch.
  2. To convert an existing project into a Git repository.

2. git clone

The git clone command copies an existing Git repository to your local machine.

It’s most commonly used to download a project from a remote repository, such as GitHub.

				
					git clone https://github.com/ username/repository.git
				
			

When to use it?

  1. When you want to contribute to or work on an existing project.
  2. To set up a local version of a project hosted on a remote server.

3. git add

The git add command is used to stage changes before committing them.
It tells Git which files to track for changes in the next commit.

				
					git add <file_name>         # Add a specific file
git add .                   # Add all files in the current directory
				
			
  1. Before committing changes to the repository.
  2. To stage new, modified, or deleted files for commit.

4. git commit

The git commit command saves your changes to the local repository.

Each commit records a snapshot of the project’s current state.
You should always include a meaningful message to describe what the commit does.

				
					git commit -m "Describe the changes made"
				
			

When to use it?

  1. After you’ve staged your changes using git add.
  2. To save a checkpoint in the project history.

5. git status

The git status command shows the current state of the working directory and staging area.

It tells you which files are staged for commit, which files have been modified, and which files are untracked by Git.

				
					git status
				
			

When to use it?

  1. To check the status of your files before committing.
  2. To see which files need to be added, committed, or removed.

6. git pull

The git pull command fetches changes from a remote repository and merges them into your current branch.

It’s commonly used to update your local repository with the latest changes from the remote repository.

				
					git pull origin main
				
			

When to use it?

  1. When you want to get the latest changes from the remote repository.
  2. Before starting new work to ensure your local copy is up to date.

7. git push

The git push command uploads your local commits to a remote repository.

This is how you share your work with others after you’ve made and committed changes.

				
					git push origin main
				
			

When to use it?

  1. After committing changes locally, to share your updates with a remote repository.
  2. When collaborating with a team and needing to sync work.

8. git branch

The git branch command is used to manage branches in your Git repository.

Branches are essential for isolating different tasks, features, or bug fixes. You can list all branches, create new ones, or delete old ones.

				
					git branch                 # List all branches
git branch <branch_name>    # Create a new branch
git branch -d <branch_name> # Delete a branch
				
			

When to use it?

  1. When you want to create a new branch to work on a feature or bug fix.
  2. To list existing branches and switch between them.

9. git checkout

The git checkout command is used to switch between branches or restore a specific commit.

You can use it to move from one branch to another or revert changes in your project.

				
					git checkout <branch_name>    # Switch to a different branch
git checkout <commit_hash>    # Checkout a specific commit
				
			

When to use it?

  1. When you want to switch to a different branch.
  2. To check out a previous state of your project using a commit hash.

10. git merge

The git merge command combines changes from one branch into another.

It’s commonly used to merge feature branches into the main branch once the feature is complete.

				
					git merge <branch_name>
				
			

When to use it?

  1. When you’re done working on a feature and want to merge it into the main branch.
  2. To combine different lines of development in your project.

GIT Cheat Sheet

Here are some external links to Git cheat sheets that you may be handy for you:

    1. Atlassian
    2. GitHub
    3. GitLab
    4. Git Tower
    5. GitKraken

Conclusion

These top 10 Git commands form the foundation for working effectively with Git. By mastering these commands, you’ll be able to manage your code, collaborate with others, and track the evolution of your projects with confidence.

Whether you’re working solo or as part of a team, these commands will become a regular part of your development workflow.

Share This Article
Follow:
Chinmaya is working as a Senior Consultant with a deep expertise in Salesforce. Holding multiple Salesforce certifications, he is dedicated to designing and implementing cutting-edge CRM solutions. As the creator of Writtee.com, Chinmaya shares his knowledge on educational and technological topics, helping others excel in Salesforce and related domains.
Leave a comment