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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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
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

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Mastering Git
  • Terraform Best Practices: The 24 Practices You Should Adopt
  • How To Use Git Cherry-Pick to Apply Selected Commits
  • Top 20 Git Commands With Examples

Trending

  • The Full-Stack Developer's Blind Spot: Why Data Cleansing Shouldn't Be an Afterthought
  • Manual Sharding in PostgreSQL: A Step-by-Step Implementation Guide
  • Endpoint Security Controls: Designing a Secure Endpoint Architecture, Part 2
  • MySQL to PostgreSQL Database Migration: A Practical Case Study
  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!

By 
Riha Mervana user avatar
Riha Mervana
·
Mar. 07, 23 · Analysis
Likes (5)
Comment
Save
Tweet
Share
7.2K 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.

Related

  • Mastering Git
  • Terraform Best Practices: The 24 Practices You Should Adopt
  • How To Use Git Cherry-Pick to Apply Selected Commits
  • Top 20 Git Commands With Examples

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!