Getting Hang of GIT
Tags: codenewbie, beginners, github, tutorial
Published: 2021-05-03 17:11:37

You may have seen designers or memes showing how designers save their work with files name like design.png, design-final.png design-final-final.png and so on. 😂
<br/>

When I started my journey as a developer I too used a somewhat similar approach to save my work, but thanks to the version control system we don't have to go through this forever.
<br/>

A __version control system__ helps us continue with our development by tracking and maintaining a reference to multiple versions of our work. It makes it easier for us to work with different versions of files mutually or switch between them. Irrespective of what language you are working with, a version control system is a very important part of your development journey. GIT is one of the version control tools, which is wildly adopted in the development community.
<br/>


## When I didn’t know GIT.

I used to keep a progressive copy of my project directory and files. I made sure that I have a copy of my previous stable code before adding any extra feature/code to it. This was my version of _version control_ system based on what I knew then. I mean what else I could do. This trick didn't help me always, as sometimes you are confident enough to start working without a copy and then there is no going back, or sometimes you forget to make a copy. And then you end up cursing yourself 🤬
<br/>


Losing lots of work, re-writing stuff again, the cursing didn’t work out well for me so, I had to find a reasonable solution, after talking around and doing some initial research(googling) I came to know about the “git”.
<br/>


## Meeting  GIT

I heard great things about GIT, and I wanted to use it, so I started by visiting the official site for docs, and it was not much of help then. I next began to watch GIT tutorials, and Oh my git, GIT is a terminal application. I was scared of the terminals then hardly have used the terminal. I got cold feet. 
<br/>

I had to re-watch tutorial videos to understand the commands to grasp what’s happening despite my fear, as there were not many choices then. So, rather than using GIT directly with my main projects, I started by practising with new dummy projects.

> This may sound easy now, but running these commands on the terminal was very overwhelming in starting for me. The fear of end up losing my hours of work or end up making some changes in system config 😂
<br/>


## Making peace with GIT.

After starting with the GIT, I knew, without it, there was no way forward. I had to know GIT to be able to work efficiently & peacefully. I tried to strengthen my understanding of GIT by comparing and putting it into real-world examples and analogies. 
<br/>

The mental model I developed was file management, creating files, directories and renaming them. GIT is very much similar to what I have been working with in the past. GIT takes away from us the effort of creating, copying and maintaining them and do itself. __Understanding the git's basic with directory analogy.__
<br/>


<Note title="Tip 👀" message="That's how I learn new stuff by relating to real-world analogies and making sense of things in the world where there is no code and development." />
<br/>
 

## Staring a new project. 

You kick start a new project by creating a new folder in your system, where all the work will be done and files related to it will be stored. 
<br/>

```bash  
git init
```

`git init` command does the same, GIT initializes your project and confirms that from now it will keep track of the work being done in the directory. One can assume git creates folders too to store our work and when initializing a project it creates a folder named `master`. In GIT vocab these folders are referred to as _branch_.
<br/>


## Making progress.

Now that you have run the `git init` command GIT is tracking every change you are making in your files and subdirectories. When working on your project, you add content to files if required you may add more files. GIT tracks everything so in terms of GIT vocab, the changes are being _tracked_ right now.
<br/>

```bash 
git status
```

You can use the `git status` command to see all the files that are being tracked by GIT.
<br/>

<Note title="Note ✍🏽" message=" When you run the git status command it also shows you which branch you are in." /> 
<br/>


## Saving development

Let's say you have done good enough work or you are confident of your work progress you want to save it. GIT helps you achieve this by running two commands
<br/>


```bash
git add <files_path>
```

The `git add` command is used for specifying all the files you want to save. After running this command in terms of GIT vocab, the files are staged right now.
<br/>


<Note title="Tip 👀" message="git add . command adds all the tracked files to staging area" />
<br/>


<br/>

```bash
git commit -m  <message>
```

`git commit` command help you save changes with a message that you can use as a reference for what you are saving. And after running this command your files are considered saved or committed.
<br/>

## Falling back to saved files

After saving initial changes you can continue with your natural work. But what if you are not satisfied with the changes you have made or you want to start fresh again, you want to undo all the changes to previously saved work.
<br/>

```bash
git stash
```

You can discard all the work you have done after the last committed changes by using the `git stash` command.
<br/>

## Making changes

You can keep making changes to your files if you want to modify them and GIT will track those changes but these changes will not be saved. To save new changes you have to again add and commit your files. 
<br/>

So, these are the few common GIT commands that you will be using on daily basis. All these commands will help you track, stage and commit changes in your files/directory. 
<br/>

Everything till now what we have done was in our local system. What if we want to share these files, we want to take them to a remote local.
<br/>


## Sharing the files

So, in general, when you want to share your files you upload them to dropbox, drive and share a link to that storage. GIT also helps you do the same, you can add a remote reference to your local files and upload them to that remote location.
<br/>


```bash
git remote add <local ref name> <remote_link>
```

`git remote add` help you add a remote location as a reference to upload local git committed files.
<br/>

<Note title="Note ✍🏽" message="Only committed files can be uploaded to the remote location that means only saved work can be pushed to the remote drive." />
<br/>

After providing a remote location you have to upload these files
<br/>

```bash
git push <local ref name> master
```

The `git push` command is used for uploading/sending local files to your remote storage. The common remote GIT services are Github, Gitlab and Bitbucket. You can add multiple remote links to your project and push your changes to all of them.
<br/>


<Note title="Tip 👀" message="git remote -v to can be used to list all remote links available in current project" />
<br/>


## Updates are required

Now that you have shared your work with others, you will receive feedback on it no escape there everyone is here to judge you. After receiving the feedback you want to make some changes, but you are not confident about what you will do so what you are in the habit to do is copy your local folder somewhere and then make changes to that folder so that if update and save anything that doesn’t work out you have a copy of your previous work to have a fresh start again.
<br/>


## Creating a copy to update
<br/>


```bash
git checkout -b <new_branch_name>
```

The new branch will have the content of the branch from which you are checking out.
<br/>

<Note title="Note ✍🏽"  message="The flag -b is only required with checkout command when creating a new branch" />
<br/>

`git checkout` command helps you to switch between multiple copies for your current work. After running the checkout command all your work would be tracked under the new branch and the parent branch will remain untouched.
<br/>

## Updating the copied version

You can then make your changes in the new branch and add, commit new changes. These changes will not affect the master copy.
<br/>

## Sharing the updated version

You then can share the new branch with others by pushing the new branch to a remote location.
<br/>

```bash
git push <local ref name> <new_branch_name>
```


## Bringing all in one place
<br/>

Now that you have shared the updated version you can have +ve or -ve feedback if it's -ve then you make updates and share again this cycle repeats till you have satisfied your reviewer. After you know what your final version is want all the updates to be your master copy. You can copy all your work from the new branch to the master branch. This can be done in two ways locally or remotely. As you have reference to both branches in your local and remote location you can copy/move your in a new branch in both these places.
<br/>


__Locally__
<br/>

```bash
git merge <new_branch_name>
```

`git merge` command help you copy your work from one branch to another.
<br/>


__Remotely__
<br/>

In the remote location, we have a concept of _pull request_ where you raise a request with a title saying please merge my  `new_branch_name` to the master branch. So, it is similar to the merge command but the merge command in remote doesn’t get executed until someone/you approves that pull request. It is kind of gatekeeping to make sure no unnecessary changes are being copied into the master branch. 
<br/>

Now that your changes are merged into the remote master branch you have to download updated remote files into your local system to have the latest copy of the work.
<br/>

```bash
git pull <local ref name> <branch_name>
```
<br/>

`git pull` command helps you to pull/download changes from remote to local storage.
<br/>

Well, that's most of the git commands which we require on daily basis in our development journey.
<br/>


## GIT Snacks

Before saying goodbye there are few things I would like to mention which will come in handy when working with GIT.
<br/>

__Repository__: The remote copy of our local project directory is referred to as repository aka repo.
<br/>

__Conflicts__: Conflicts is a scenario in GIT when two different changes are done on the same content and GIT is not sure which one to keep and have a conflict. This can happen when multiple devs are working on the same files simultaneously.
<br/>

__git clone__: This commands helps you to create a copy of the remote project to local. You can visit Github, Gitlab or Bitbucket and can clone a repo in your local if you have access to a repo.
<br/>

__git log__: This command provides the timeline of all the commits that have been made in the currently active branch.
<br/>

__.gitignore__: In this file, you can mention all the files to ignore, which GIT can avoid tracking, for example, you can use .gitignore to ignore your build directories and environment files from being tracked and pushed to the remote.
<br/>

__HEAD__: HEAD refers to the last commit in the current branch, using heads GIT can create a tree structure of your source code. 
<br/>

That is my comprehensive guide to the GIT. Hope this helps you with your understanding of GIT and the version control system.