DevOps Prerequisite (Part 4): Git and GitHub
Tags: devops
Published: 2023-03-21 09:41:19

Assuming this is the code we have:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ttbykhaeu1gwhnlhvni2.png)
When we start changing things regularly, it's hard to keep track of all of the details or get back to the previous files.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dspgz4d7kbzi1ye49mpf.png)
So, when the project becomes big and lots of contributors come in, we have to use source control management tool like git.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qzuo0tznrwo1heyud4p1.png)
**Using Git**
Firstly, we initialize the total files and create a git repo.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ey19htsv068ua6lp9d0t.png)
Then we choose which files to track and which not to track.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7ard02d86z1185n2cx7o.png)
Now, once you make any changes into any of the files. For example(main.py), it will track that down.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2fnacggma9nsvtieryat.png)

Now assume that you have multiple files and you want just one files changes to be confirmed and create a commit, you need to take that specific file to the staging area and then you can commit.

Note: Before commit, you have to take that file to the staging area.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t22adi5bbjk8ikjh4dcd.png)

**Installing git and using it**

First we install it ( here we used centos as our operating system)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/386mw6rzexxa351mh8da.png)
Then you go to the folder where you are working and want to track. Then use this init command to initialize git.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2d5ucn6o39pkft2pxhkk.png)
Now we have no changes here to track , so we will see this.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/080ouvqj5hlpp233ng7f.png)

We can track our desired files using the `git add` command

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nhf1ctfi2msg8ztaj5ff.png)

Now, assume that you have added some new codes and tried to check if git has been able to track that or not using **git status**.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/usei67l3xrcarfngkn9k.png)
Now we have taken it to the staging area.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wgm4xpzixslfgqprr0y2.png)
Now we have committed our files.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rw3y1xt1cjvdv46a99vj.png)

Here all of the files which was kept for tracking has been added.

**Remote repository**

As your team increases, more and more people codes and you guys need to work centrally now.
So, you now need to keep an online central repository from where you will download (fetch/pull) codes or push (upload) codes.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3dc8ha02ejmhdidrw752.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9041ak1qgi4p14i9pvz6.png)

**Using remote repositories**

now you want to track all of the files under your folder "my application" and then use the github remote repository for that.
Here you first locally add them all and take it to the staging area and then create your first commit.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aijwm9tludawopf8djok.png)

You then go to GitHub and create a repository there.
In the beginning this repository will be empty and you will now upload (push) your commit to this remote repository.

Thus all the files will move to the remote repository too.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e4es54mc5sx1g5elskmg.png)
Now add this commit to your remote repository.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5b95l73slpahoklfvsab.png)
Now push this commit to the remote repo.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/akkx8au1606bui0enzse.png)

Here we have pushed to the master (which is main now) branch.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rnxcm1g227pxu8ouvmfm.png)


Now, when someone wants to get all of your codes, they first need to download it (clone it)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lqrabsehspqfnm5v9yie.png)

the user can then modify the codes and contribute too. Then there will he come to know about Pull requests (PR), Merge conflicts and many other terms.

But that's not for this blog.

<img src="https://media0.giphy.com/media/Nfs5y1kzJKfu/giphy.gif?cid=ecf05e472d3w835ptrri5m1p6ifnwl9mi7ope9pifvhpcxf6&rid=giphy.gif&ct=g">