Stud2design LogoBlog

Getting Hang of GIT

publish date

3 May, 2021

read time

9 mins

coverSource: Unsplash by @wesleyphotography

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. 😂


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.


A version control system helps us continue with our development by tracking and maintaining 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.


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 🤬


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”.


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.


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 😂


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.


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.


Tip 👀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.

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.


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.


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.


git status

You can use the git status command to see all the files that are being tracked by GIT.


Note ✍🏽 When you run the git status command it also shows you which branch you are in.

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


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.


Tip 👀git add . command adds all the tracked files to staging area


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.


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.


git stash

You can discard all the work you have done after the last committed changes by using the git stash command.


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.


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.


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.


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.


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.


Note ✍🏽Only committed files can be uploaded to the remote location that means only saved work can be pushed to the remote drive.

After providing a remote location you have to upload these files


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.


Tip 👀git remote -v to can be used to list all remote links available in current project

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.


Creating a copy to update


git checkout -b <new_branch_name>

The new branch will have the content of the branch from which you are checking out.


Note ✍🏽The flag -b is only required with checkout command when creating a new branch

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.


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.


Sharing the updated version

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


git push <local ref name> <new_branch_name>

Bringing all in one place


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.


Locally


git merge <new_branch_name>

git merge command help you copy your work from one branch to another.


Remotely


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.


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.


git pull <local ref name> <branch_name>

git pull command helps you to pull/download changes from remote to local storage.


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


GIT Snacks

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


Repository: The remote copy of our local project directory is referred to as repository aka repo.


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.


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.


git log: This command provides the timeline of all the commits that have been made in the currently active branch.


.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 remote.


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


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