DEV Community

Cover image for Git: the basic commands every developer should know
Heather Parker
Heather Parker

Posted on

Git: the basic commands every developer should know

Git is useful for anyone who writes code or tracks changes to files, from web developers to app developers. So, what exactly is it, and why should you start using it?

What is Git?

Git is a version control system that tracks file changes. Using Git allows you to keep a record of all changes and return to specific versions as needed. It is simple to use, takes up little space, and is extremely productive. Its branching model distinguishes it from nearly every other SCM available. The ability to merge changes from multiple people into a single source is what makes Git so simple. You can use GitHub or other online hosts to store backups of your files as well as their revision history.

Git’s main components

To me, Git is a wonderful tool to use in team projects because it helps to avoid confusion in code and brings a simple yet effective system to work. Here, I’d like to cover up the main components of Git:

Repository
A Git repository (or simply repo) contains all of the project files as well as the entire revision history. You'll take an ordinary folder of files (such as the root folder of a website) and tell Git to turn it into a repository. This creates a .git subfolder in which all of the Git metadata for tracking changes is stored. Simply put, a repository is a place where you keep your code.

Commit 
To add new code to the repository, you need to make acommit, which is a snapshot of your repository at a particular point in time. commits a specific change, or series of changes, to a file in the repository. Git's history is made up of successive commits.

Branch 
A branch is used to store your changes until they are ready. While the main branch (master) remains stable, you can work on a branch. When you're finished, you can merge it with the master. The great advantage is that you can have a few branches in one repository and merge them whenever you need.

Pull requests 
This is a technique used in Git for discussing changes before they are merged into your codebase. A pull request is more than just a notification; it's a dedicated discussion forum for the proposed feature. This is especially convenient when several people are working on the same code, allowing developers to check each other's work.

Now that we have briefly discussed the main theoretical Git components, I want to list 10 basic Git commands that every developer must know before starting to work with Git.

1. Starting a new repository

git init
Enter fullscreen mode Exit fullscreen mode

2. Setting the author name and email address respectively to be used with your commits

git config - - global user.name “[name]”
git config - - global user.email “[email address]” 
Enter fullscreen mode Exit fullscreen mode

3. Downloading existing source code from a remote repository

git clone  <https://name-of-the-repository-link> 
Enter fullscreen mode Exit fullscreen mode

4. Creating a new branch

git branch <branch-name>
Enter fullscreen mode Exit fullscreen mode

5. Merging branch in master

git merge <branch-name> 
Enter fullscreen mode Exit fullscreen mode

6. Getting updates from the remote repository

git pull <remote> 
Enter fullscreen mode Exit fullscreen mode

7. Adding files into the staging area for Git

git add <file or directory name>
Enter fullscreen mode Exit fullscreen mode

8. The current state of the repository

git status 
Enter fullscreen mode Exit fullscreen mode

9. Sending the changes made on the master branch, to your remote repository

git push [variable name] master  
Enter fullscreen mode Exit fullscreen mode

10. Changing the head (records or snapshots the file permanently in the version history)

git commit -m " Commit Message"
Enter fullscreen mode Exit fullscreen mode

So far, these are the main commands that everyone who works with Git must know. In fact, Git is extremely easy to use, and the number of commands is quite large. But to remember these commands is not a tough task—you simply need to start working with Git, and most of the commands will be remembered intuitively.

Top comments (12)

Collapse
 
sergeyleschev profile image
Sergey Leschev • Edited

I believe you did an excellent job explaining Git and its main components. Their list of 10 essential Git commands is very valuable for beginner developers.

Collapse
 
justplegend profile image
JPL

This is good article, but also beside this, I think it's important to understand and different things you need to do on local computer (own laptop) and GitHub.
On this link it's little bit explained that things: local computer vs GitHub

Collapse
 
swordheath profile image
Heather Parker

Completely agree! Thanks for valuable source👍🏻

Collapse
 
swordheath profile image
Heather Parker

Thank you so much, Sergey!

Collapse
 
hanibikdeli profile image
HaniBikdeli

awesome article, but i would also add git fetch

Collapse
 
surbhidighe profile image
Surbhi Dighe

Good Job!!

Collapse
 
mattiasfjellstrom profile image
Mattias Fjellström

I agree that with these commands you can live a whole life with git (if you are lucky, at least). I saw that someone else mentioned git switch, but I'd like to chime in on that command. The purpose of git switch is to switch between branches, or create new branches. All you can do with git switch you can do with git checkout, but git checkout has a lot more use-cases so it is like using a sledgehammer for small nails! :)

Collapse
 
soderluk profile image
K.S.

A couple of commands I've been using a lot since they came out, is git switch [-c] <branch> to switch to another branch (or create one with -c), and git restore <files> for restoring working tree files. Adding --staged to git restore will just restore the index, i.e. un-stage files.
These two were introduced so there's a distinction between switching branches and restoring the index, which can be achieved by the git checkout command.

Collapse
 
bcouetil profile image
Benoit COUETIL 💫

Hey ! I would replace git init with git reset (because you already have git clone). Hard to live without it.

Collapse
 
bybydev profile image
byby

If you want to create an alias named "lg" that shows a history of all commits, you could use the following command:

git config --global alias.lg "log --all --graph --decorate --oneline"
Enter fullscreen mode Exit fullscreen mode
Collapse
 
shabink profile image
Shabin-k

thank you

Collapse
 
timmcwilliams profile image
Tim McWilliams

Wish I could figure out how to use my VS Code interface to correctly use my repositories.