Git - A Simple Guide
Tags: git, github, versioncontrol, development
Published: 2024-02-28 16:25:25

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wn74re07yb5anq9j77bp.png)


**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:{% embed https://git-scm.com/downloads %}

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/icjij2bsswbpp0eovvqr.png)



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

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

{% embed https://www.atlassian.com/git/tutorials/using-branches %}

{% embed https://www.freecodecamp.org/news/git-and-github-for-beginners/ %}