DEV Community

Akhil Athuluri
Akhil Athuluri

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 (0)