Korea Driven.

Korean culture is having a big moment in the West right now. It’s an interest that’s been simmering for a while, from when Gangnam Style was the latest inescapable Macarena-esque fad, to K-Pop and…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Git for Beginners

Every newbie programmer encounters the word Git, but very few of them commit to it, pun intended. Git is difficult to understand for someone new, not because it is something complicated but because they are not able to understand the use of it. This post will help you understand the scenarios where you might encounter the use of the most common and most basic git commands. This is an introductory post, meant for absolute beginners who want to understand what git is all about.

In the previous paragraph, I explained Git as a version control system (VCS). In simple words, VCS helps us to keep track of our project. I will explain this using a real-life example: imagine yourself as the manager of the team responsible for deploying a Taxi Booking website for a client. You have successfully deployed the first version of the website Version-1. Your team is currently working on the second version of the website Version-2, but when you deploy your Version-2 website, due to some technical error entire website crashes, and now your client is angry and wants you to revert to Version-1. How can you do this? If you were smart enough you would have made a copy of your Version-1 website and can simply deploy that code. This is version control, you made a copy of your Version-1 before starting to work on Version-2. But now imagine a long-term project, you will have several versions throughout the lifetime of the project, so will you make a copy of all of your versions and waste soo much space? No, you don't want that, this is where VCS comes in. VCS helps you to keep track of various versions of your project in a memory-efficient way, with the ability to revert to any version at any point.

Now let's understand the distributed side of VCS. Continuing the previous example, suppose three teams working on the project, team 1 is responsible for Frontend development, team 2 is responsible for Backend development, and team 3 is responsible for Database management. You want every team member to synchronize efficiently with the other team members while writing the code. How can you do this? VCS helps you with this too. Every team member works on their local repository on their local system and then merges the changes with remote repository for everyone to see.

Untracked Files: These files are not tracked by git and git will not save its change history. These files are simply ignored by git and are not part of the git working tree.

Commit: The git commit command is a way to take a snapshot of the project’s currently staged changes. These snapshots can be thought of as “Safe” versions of a project — Git will never change them unless you explicitly ask it to.

Tracked Files: These files are controlled by git and can be divided into two types- modified files and un-modified files. Un-modified tracked files mean that no modification was made to the final since the last known commit. Un-modified tracked files can either have a prior commit or could have just been added from untracked files. Modified tracked files were changed in some way since the last known commit.

Staged files: Staged files are ready to be committed and once they are committed they are considered un-modified tracked files. After the commit we can push the current git working tree into the remote repository. As discussed earlier these remote repositories can be in GitHub, Bitbucket, GitLab or any other cloud based git hosting service.

Write your first git command here, ‘git’. You will be able to see all the basic git commands available to you. ‘git help -a’ and ‘git help -g’ list available subcommands and some concept guides. See ‘git help <command>’ or ‘git help <concept>’ to read about a specific subcommand or concept. See ‘git help git’ for an overview of the system.

Inside your working directory, click on the View panel>options>Change folder and search options.

The following window will open. In the View panel select ‘Show hidden files, folders, and drives' and also de-select ‘Hide extensions for known file types’.

After that again go into your working directory now you will be able to see a .git folder. Congrats you converted your working directory into a git repository.

Now let's create a python file hello.py and write the code print(‘Hello World!!’) in it. This file is untracked and you can check this by running the command ‘git status’ in your git bash.

To track the file you need to run the ‘git add hello.py’ command. After you run this command, your file will be tracked and staged.

Let’s make your first commit, “git commit -m ‘my first commit’ ”, you need to write a meaningful message in your commit so that other members of your team can understand it. After you have successfully made the commit check the status of your working tree, you will see that your working tree is clean.

Let's make changes to the hello.py, write print(‘Hello World Git is Awesome!’). Now when you check the status of your working tree, you will see that hello.py is modified. Which means than hello.py is still getting tracked by git, but it was modified since the last known commit.

Both the files are ready to be committed. Now run the commit command with a meaningful commit message. This will make your working tree clean again. Try making three more files and commit them one by one to your git repository on your own.

Now you have a total of 5 commits in your git repository, try running ‘git log’ command. You will be able to see all the commits in your repository with its unique ID, author of commit, date of commit, and message of the commit.

Other git log commands you can try running are: ‘git log -p -3’, which gives you the last three commits, ‘git log --stat’, this will give you brief stats for your commits, i.e. how many insertions and deletions you have made.

Let's suppose you want revert to your fourth commit without deleting the fifth commit, run the command ‘git revert <commit unique ID>’. This will open the vim editor. Just type :wq and press enter. Now when you see the logs you can see that a new commit is present with a message Revert “forth commit”.

Note: fifth commit is still present in the logs, this command makes a new commit instead or deleting other commits.

Now if you want to go to the second commit and delete all the commits between the current commit and second commit run the command ‘git reset --hard <commit unique ID>’.

There is so much more to git than what I have discussed in this blog like branching, fork, push, pull requests, merge conflicts, etc. But this blog is just a way to get you started with git. I would encourage you to try the following commands on your own as you may encounter them on a day-to-day basis.

Add a comment

Related posts:

How Can I Leave Myself Again?

I keep betting on the blues that never seem to leave me, getting carried away by tempestuous trade winds that lead to nowhere. There’s a chasm within that amazes me, yet I am empty with a thirsty…

Independent Power to the People

Our Declaration of Independence gives the best reminder there is of what makes us unique, as “We hold these truths to be self-evident.” Meant to be a place of transparency, the United States stands…

Retrograde

I was myself again. Unchained, detached, and released from your arms. No longer was it you that possessed my heart. I wanted nothing more than to dissolve inside my drink, and fade into the clouds…