DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Top 11 Git Commands That Every Developer Should Know

Top 11 Git Commands That Every Developer Should Know

Master the most important Git commands that every developer should know!

Riha Mervana user avatar by
Riha Mervana
·
Mar. 07, 23 · Analysis
Like (4)
Save
Tweet
Share
6.09K Views

Join the DZone community and get the full member experience.

Join For Free

Git is a version control system that has become an essential tool for developers worldwide. It allows developers to keep track of changes made to a project's codebase, collaborate with others on the same codebase, and roll back changes when necessary. 

Here are the top 11 Git commands every developer should know.

1. git config 

git config is a command that allows you to configure Git on your system. It enables you to view and modify Git's settings, such as your user name and email address, default text editor, and more.

The git config command is used to set configuration values that affect the behavior of Git. Configuration values can be set globally or locally, depending on whether you want the configuration to apply to all Git repositories on your system or just the current repository.

Some common use cases of the git config command includes setting your user name and email address, configuring the default text editor, and customizing Git's behavior.

By using git config, you can tailor Git to your specific needs and preferences, making it easier and more efficient to work with Git on your projects.

Setting your user name and email address globally:

 
git config --global user.name "Riha Mervana"
git config --global user.email "riha@youremail.com"


You can read back these values as: 

 
git config --list


Output: 

 
user.name=Riha Mervana
user.email=riha@youremail.com


When you open the global configuration file ~/.gitconfig, you will see the content saved as:

 
[user]
    name = Riha Mervana
    email = riha@youremail.com


2. git init

The first command every developer should know is git init. This command initializes an empty Git repository in the current directory. This command creates a .git directory in the current directory, which is where Git stores all the information about the repository, including the commit history and the files themselves.

The git init command can be used in two ways: 

Either changes a directory using the cd command and run git init to create a Git repository…. 

 
git init


Or create an empty Git repository by specifying a directory name using the git init command.

 
git init <directory-name>


3. git clone

git clone is used to create a local copy of a remote repository. This command downloads the entire repository and its history to your local machine. You can use this command to create a local copy of a repository that you want to contribute to or to start working on a new project.

Here is an example of how HTTPS looks. 

 
git clone <https://github.com/reactplay/react-play.git>


This will clone the react-play project locally for you. Then you can change to the directory and start working on it. 

 
cd react-play


4. git add

git add is used to stage changes made to a file. This command tells Git that you want to include the changes made to a file in the next commit. You can add individual files or directories or all changes in the current directory by using the git add . command.

The git add command is used to send your file changes to the staging area. 

 
git add <file-name>


Also, 

 
git add <directory-name>


5. git commit

git commit is used to save changes made to the repository. This command creates a new commit with a message that describes the changes made. The message should be descriptive and provide context about the changes made.

 
git commit -m "add a meaningful commit message"


6. git push

git push is used to upload local changes to a remote repository. This command sends the changes made in your local repository to the remote repository, where other developers can access them. You can use this command to contribute changes to an open-source project or to share changes with your team.

 
git push <remote> <branch-name>


7. git pull

git pull is used to download changes made to a remote repository to your local repository. This command is useful when you want to work on the latest version of a project or when you want to merge changes made by other developers into your local repository.

 
git pull


8. git branch

git branch is used to create, list, and delete branches. A branch is a copy of the repository that you can use to work on new features or fixes without affecting the main branch. You can use this command to create a new branch, list all the branches in the repository, or delete a branch.

List all the branches: 

 
git branch


Create a new branch with a branch name:

 
git branch <branch-name>


Delete a specific branch:

 
git branch -d <branch-name>


Rename a branch:

 
git branch -m <branch-name>


List all Remote branches (with a marking of the current branch):

 
git branch -a


9. git merge

git merge is used to merge changes made in one branch into another branch. This command is useful when you want to incorporate changes made in a feature branch into the main branch. You can use this command to merge changes made by other developers into your local branch or to merge your changes into the main branch.

 
git merge <branch-name>


10. git checkout

git checkout is used to switch between branches or to revert changes made to a file. This command allows you to move between branches or switch to a specific commit in the commit history. You can also use this command to discard changes made to a file and revert it to a previous state.

 
git checkout <branch-name>


11. git log

git log is used to view the commit history of a repository. This command displays a list of all the commits made to the repository, including the commit message, the author, and the date and time of the commit. You can use this command to track changes made to the repository over time and to identify which commits introduced specific changes.

 
git log <options> <branch_name>


Conclusion

Git is a powerful version control system that is widely used in software development. Knowing how to use Git effectively is essential for developers to collaborate on projects, keep track of changes, and maintain code quality. These above commands provide developers with the basic tools they need to manage their codebase effectively. However, Git is a complex system with many additional features and commands that can be used to improve workflow and productivity. Therefore, developers should strive to learn more about Git and its capabilities in order to take full advantage of its benefits.

Git Command (computing)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • REST vs. Messaging for Microservices
  • Fargate vs. Lambda: The Battle of the Future
  • Full Lifecycle API Management Is Dead
  • The 5 Books You Absolutely Must Read as an Engineering Manager

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: