DEV Community

Syed Sadiq ali
Syed Sadiq ali

Posted on

Mastering Git: Top Commands Every Developer Should Know

Introduction

In the world of software development, Git has become an essential tool for version control. It allows developers to collaborate efficiently, track changes, and manage code repositories effectively. Whether you're a beginner or an experienced developer, understanding and mastering Git commands is crucial for maximising productivity. In this blog post, we'll explore the top Git commands that every developer should know.

git init:

The first step in using Git is initialising a repository. By running git init in your project directory, you create an empty Git repository, enabling version control for your project. This command sets up the necessary infrastructure for Git to start tracking changes.

$ git init
Enter fullscreen mode Exit fullscreen mode

git clone:

To start working on an existing project, you'll often need to make a local copy of the repository. git clone allows you to download the entire repository and its history to your local machine. It establishes a connection between your local copy and the remote repository, enabling you to fetch updates and contribute to the project.

$ git clone https://github.com/example/repository.git
Enter fullscreen mode Exit fullscreen mode

git add:

Before committing changes to your repository, you need to stage the files you want to include in the next commit. git add lets you selectively add files or entire directories to the staging area. It prepares them for the upcoming commit, indicating that you want to include these changes in the repository.

$ git add file.txt
$ git add folder/
$ git add .
Enter fullscreen mode Exit fullscreen mode

git commit:

Once your changes are staged, you can create a new commit using git commit. Commits act as snapshots of your project at a specific point in time, preserving the changes you made. Each commit has a unique identifier, commit message, and references the previous commit, forming a chronological history of your project.

$ git commit -m "Initial commit"
Enter fullscreen mode Exit fullscreen mode

git pull:

Collaborative projects often involve multiple developers working on the same repository. git pull allows you to fetch and merge the latest changes from the remote repository into your local branch. It ensures that your local copy is up to date, incorporating any new commits made by others before you start working.

$ git pull origin master
Enter fullscreen mode Exit fullscreen mode

git push:

Once you've made changes to your local branch and want to share them with others, you can use git push to upload your commits to the remote repository. It synchronises your local changes with the central repository, making them available to others working on the project.

$ git push origin master
Enter fullscreen mode Exit fullscreen mode

git branch:

Branches are a powerful feature in Git that enable parallel development and experimentation. git branch lets you create new branches or list existing ones. You can work on different features or bug fixes independently by switching between branches using git checkout.

$ git branch
$ git branch new-feature
Enter fullscreen mode Exit fullscreen mode

git merge:

When you're done working on a feature branch or want to incorporate changes from one branch into another, you can use git merge. It combines the changes from the source branch into the target branch, creating a new commit that includes both sets of changes.

$ git merge new-feature
Enter fullscreen mode Exit fullscreen mode

git stash:

Sometimes, you may need to switch to a different branch while working on unfinished changes. git stash allows you to temporarily save your modifications in a stack-like structure, enabling you to switch branches without committing or discarding your changes. Later, you can apply the stashed changes to the appropriate branch.

$ git stash save "Work in progress"
$ git stash apply
Enter fullscreen mode Exit fullscreen mode

git log:

To examine the commit history of a repository, git log is a handy command. It displays a chronological list of commits, including their author, timestamp, and commit message. You can use various options with this command to filter and format the output based on your requirements.

$ git log
$ git log --author="John Doe"

Enter fullscreen mode Exit fullscreen mode

Conclusion:

Git is a powerful version control system that empowers developers to collaborate effectively and track changes in their projects. By mastering these top Git commands, you'll be able to navigate repositories, manage branches, and track changes with ease. Whether you're working on personal projects or contributing to large-scale software development, understanding and utilizing these commands

Top comments (23)

Collapse
 
ant_f_dev profile image
Anthony Fung

Great intro to Git!

For anyone on Windows, Git Extensions is a great (free) UI that's both helpful for people learning Git, and deep for those who are a bit more comfortable.

Collapse
 
syedsadiqali profile image
Syed Sadiq ali

Awesome @ant_f_dev !!

Thanks!!

Collapse
 
bretbernhoft profile image
Bret Bernhoft

If every developer had these Git commands memorized, life would be a little bit easier for everyone. This is true from users to developer managers. Thank you for the refresher, it is always good to review the basics.

Collapse
 
syedsadiqali profile image
Syed Sadiq ali

Thanks for reading !!

Collapse
 
blessingcode profile image
Prince Luah Marwiah

Nice and straight forward 👍

Collapse
 
syedsadiqali profile image
Syed Sadiq ali

Thanks !!

Collapse
 
triompheweb profile image
Triomphe-dev

Nice post

Collapse
 
syedsadiqali profile image
Syed Sadiq ali

Thank you!!

Collapse
 
taufiqtab profile image
Taufiq Abdullah

Nice post !

git log -1 was my favorite commands :D

Collapse
 
syedsadiqali profile image
Syed Sadiq ali

Thank you very much!!

Collapse
 
dbolser profile image
Dan Bolser

Thanks. Can you add more examples, and bake in best practice? Kthxbi

Collapse
 
syedsadiqali profile image
Syed Sadiq ali

no this time though but will write another blog about best practices.

Thanks for the suggestion;

Collapse
 
hanibikdeli profile image
HaniBikdeli

great post especially the git stash part which has been very useful to me

one thing though, i would also add git fetch to the list

Collapse
 
edurbaez profile image
edurbaez

Very useful thanks a lot!!

Collapse
 
syedsadiqali profile image
Syed Sadiq ali

Thanks for reading !!

Collapse
 
fruntend profile image
fruntend

Сongratulations 🥳! Your article hit the top posts for the week - dev.to/fruntend/top-10-posts-for-f...
Keep it up 👍

Collapse
 
syedsadiqali profile image
Syed Sadiq ali

that's awesome!!

Collapse
 
rmartins profile image
Ricardo Martins

Not to mention the commands to rollback a commit,or change the last git message. They are important too.

Collapse
 
syedsadiqali profile image
Syed Sadiq ali

Yeah, they are !! will write another blog for those.

Collapse
 
bkpecho profile image
Bryan King Pecho

The mention of git stash and git log is valuable. Stashing changes and examining commit history are handy features every developer should know. Thanks for sharing! 🙌

Collapse
 
syedsadiqali profile image
Syed Sadiq ali

Yup!!

I use them everyday.

Collapse
 
rajeevpande26 profile image
Rajeev Pande

Nice concise writeup for someone using Visual Studio, to get to know the magic that happens behind the scenes

Collapse
 
syedsadiqali profile image
Syed Sadiq ali

Yup!! Thanks for reading!!