Git Demystified
Tags: beginners, git, github, productivity
Published: 2023-04-05 17:23:51

Git can seem to be an intimidating tool to use and especially for those starting out, it is easy to get lost in the complexities and the vast use cases which might lead you to wonder- Is it worth the headache? 
In this blog, I will attempt to show a simple workflow on how to use git in a simplified way and cover basic commands and use cases. This will help in getting comfortable with using git and github and cover basic commands to get started with.


## What is Git?

Git is a version control system which lets you to track all the changes made to a file system.
A repository is created and initialized which contains all the files. Git maintains a history of all changes made to these files over time. This allows you to view the changes made to the code, collaborate with other developers, revert to previous versions and essentially time travel between the different versions of the code base.
<br>


## Creating a Github Repository

We can easily create a repository as shown below.

![create repo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tqgqoao5zaoa1k5afkhp.PNG)

Once it is created, we can clone the repository on our local machine so that we can easily make changes in our files. For this we use the command: `git clone`

![cloning](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ffjbde9elxmznp0hrrq.png)
We copy the https key and do git clone as shown below using powershell

![powershell](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/szneaf1tr91q24xlkihz.png)

Great! so now we have created an empty repository and cloned that on our local system. You can see the `.git` folder present in the project's root directory.

![.git folder](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3gpgu0j44skty3pk2stj.png)
Now we can start adding files and making changes.



## Making changes and pushing the code

Now in the directory where we have cloned, we can create a sample html file and add content.

![html file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/edblcq419alpre9rjgv3.png)

Now we need to accomplish the following:

1. Add all our changes to the working tree so that we can push it
2. Commit our changes to record all the changes we have made
3. Push our changes to github

For this we use the following commands:

- `git add -A` : This adds all our changes made in our directory 

- `git commit -m "commit message"` : This records the changes and adds a comment to the commit.

- `git status` : To see all the changes which are staged but not yet pushed

- `git push` : This uploads the changes to the remote repository which is in github

These commands are seen below in action:

![commands](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/48e0m4vd71r5rkwecha4.png)


And here instantly we can see the file we created and all the changes reflected on our github repository.

![Github](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uvxvvy1gfl575nhnwc0p.png)

## Git Branches

Creating separate branches to work on is very useful especially when working on different features which you want to keep separated. It is good practice to work on a branch instead of the main so that the main or master branch is protected and changes are only made to the required branch.


The following commands are used:

- `git branch <branch_name>` : Creates a new branch with given name

- `git branch` : Gives the name of the current branch

- `git checkout <branch_name>` : Shifts to the given branch

![branch](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/spqxcwi2ndjgavzd1ai3.png)
 
Thus it is easy to switch between branches and using different branches for different features is often useful.

We create a branch `test1` and push the changes to it. 

![test1 branch](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ugfafc0g9be7j4qh1dvh.png)
Note that when pushing to a new branch which does not have an upstream branch in github yet, we use `git push --set-upstream origin test1`

Now a friend wants to contribute to your project. This is easy to do as they can clone the repository in their system and create a branch and start working on their own.

![branching](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q6k3xpwv7wh9au1hrdsz.png)

As shown above, the repository is cloned and `test2` branch is created to work on.

This covers the basics of setting up a git project, and a workflow on the basics of git you need as a developer working on your personal project and wants to start with simple version control to make life easier. In the next posts, I will go into more detail on how to work collaboratively using git, make pull requests and cover other useful commands.