DEV Community

Cover image for Git - A Simple Guide
Pratyoos Panta
Pratyoos Panta

Posted on

Git - A Simple Guide

Git is a tool for version control. It is a version control system used to track different versions of codebase. This software keeps the track of every modification in the code by using a special kind of a database after once it is initialized. Due to advancement in development environments, version control tools help software teams work faster and in an efficient manner.

Git Logo

Installing Git

If you don't have Git installed in your system, it can be installed from official Git website or from the link below:

To check if you have Git installed properly in your system, type the command git --version in the terminal.

Getting started with Git

There are two ways of running Git, Terminal vs GUI. This quick guide will be focused on Terminal approach. If you choose to use a GUI, Git offers in-depth documentation on their website.

To set your username, type these commands on git terminal: git config --global user.name "<username>" and git config --global user.email "<email>"

Initializing a Git repository in local machine

To initialize a Git repository in a working directory, type git init command on terminal. If you list all the files of current working directory by ls -a command, you should now see a .git folder inside the current working directory.

Adding files

After initializing a git repo on a system and making some code changes, now it is time to track the file. It is done by adding the file which takes the file to staged area. To add all the files of current directory into staged area, type git add . in the terminal whereas to add a specific file, the command is git add <file_name>. To check the status of the file, type git status

Commiting files

After the files are in staged area, it is the time to commit the changes, it can be done with the help of git commit command. To add a commit message, git commit -m "<message>" can be used. Now the file is in commited state.

Reverting code commits

To view the detailed log of commits, git log command can be used. To revert the code commit and return to previous state in case of any code errors, git revert <commit_id> can be used. Commit Id can be found in detailed log of commits. This can be used to restore the previous versions of codebase.

Github - A public codebase

Github is a hosting service of Git repositories. Code repositories are stored in this platform. The project hosted on Github can be accessed from any device. Github also gives the ability of collaboration with fellow developers. After commiting code changes through Git on local repository, now it is turn to push the codebase into Github.

Github Logo

Pushing(Uploading) a repository into Github

Among various methods of pushing a repository to Github, we can use push an existing repository from the command line section. The command is:

git remote add origin <git-url>
git branch -M main
git push -u origin main
Enter fullscreen mode Exit fullscreen mode

Using Git branches

Adding a new feature directly to the main branch of a full-fledged project could be risky. It can be added to a git branch. To create a new git branch, run git checkout -b <branch-name>. To return to the main branch, run git checkout main. All the existing branches can be listed by git branch command. Now, merging some branch to the main branch can be done by git merge <branch-name> command.

Cloning a Github repository into local machine

Run git clone <github-url> which will clone the repository into your local machine. This is used to download Github repository into new local machine or to contribute to public repositories.

Consider to checkout other tutorials and documentations from the following websites:

Git Branch | Atlassian Git Tutorial

This document is an in-depth review of the git branch command and a discussion of the overall Git branching model.

favicon atlassian.com

Git and GitHub Tutorial – Version Control for Beginners

Git and GitHub are two technologies that every developer should learn, irrespective of their field. If you're a beginner developer, you might think that these two terms mean the same thing – but they're different. This tutorial will help you understand what Git and version control are, the

favicon freecodecamp.org

Top comments (0)