learn Git in 5 mins…

Harshal kondke
5 min readJan 19, 2020

Introduction

First thing first, let me tell you.. no one can learn git in 5 mins okay. but i guarantee you, after reading this you will understand it so much that you can start using it. lets start then.

Have you ever heart of git? what it is? and why it so important?

Lets go back to 1991. when linus torvalds was building linux kernal. same software that will be used in future for supercomputers. when he open-source it, he found that managing it was really difficult when hundreds of geeks around the world are working on it. forwarding 14 years later in 2005, he created the best version control system. GIT.

Git : It is the version control system that can help one to keep track of multiple versions and changes made time to time. imagine you have the file with 3 lines in it. you added one extra. then you deleted it. but after a month you thought that that line should be there. don’t worry git has your back. you can do that. even you can use git to synchronize code between different people.

Sometimes we have working code but we want to add some new functionality to it. when we do that there is chance we screwed that code. but with git we can easily get back to previous version i.e. working one.

Enough for the introduction let gets our hand dirty.

Git Hands on:

There are various terms used in git that sometimes hard for beginners to understand. here is the simple explanation for that.

Repository (repo):

Think of it as some sort of carry bag. that hold all the stuff you put inside it. and keep it organized. repo is nothing more than a simple folder with one extra sub-folder in it. which keeps the track of changes and versions.

git Clone <url>:

when you execute “ git clone <url> ” it will make a copy of a repository and stores it on your computer. it is like fork. which create your own copy of someone else’s repository. but on your computer. where you can edit or make necessary changes.

after executing git clone command you will get a new folder as same name as repository you just clone. cd to new folder and lets move with more get commands.

git add <filename>

This command will add the file your git tree as explain earlier. it tells git to add the file to “staging area”. with ‘git add * ’ or ‘git add .’ you can add all the files inside the local repository to git.

git commit -m “message”

As the word commit itself suggest it will make the changes permanent. that means it will save the changes to the repository as a new version of the file. but git forces you to add commit message. so that it will easy for you as well as someone else to understand in future.

git status

git status will shows current status of the repository. that is current branch. and status compare to remote repository. we will learn more about branching. keep going.

git push

git push will send committed changes to the remote repository. which most of time on GitHub. more explicitly, it could be written as “git push origin master”

git pull

when you are working with someone else on the same repository, and you want to have updated version of code to work, use “git pull” it will retrieves changes mode by others to remote repository.

here comes my favorite part. it took me a month to understand what exactly merge conflicts are and how to solve it.

git log

it is one of the most used command while using git. basically git log will display the history of commits and messages. with some key points such as commit hash, author name of repository, and date modified.

git reset

Sometimes we realize that the previous version of our code works more flawlessly. or we just want to move to previous version of code. well in that case git reset will help you. “git reset — hard <commit>” you have to provide the commit hash to which you want to go back. you can get the commit hash using “git log” command.

“git reset — hard origin/master” reverts code back to remote repository version.

Branching:

Branch can be imagined as exact copy of repository with current version. you can play around. if the code still works you can merge the branch else just delete the branch and get back to fully functional code. each branch has its own commit history and version maintain.

git branch <branch_name>

“git branch” will simply display all branches of code. current active branch will be in green or followed by *. “git branch <branch_name>” will simply create a new branch in the current repository.

git checkout <branch_name>

I know checkout sounds like leaving the branch but in git, “git checkout <branch_name>” will switch you the desire branch.

git merge <branch_name>

when you are sure to add the changed code into your master branch. use “git merge <branch_name>” after switching to master branch. it will simply merge the branch with master resulting a new version in master branch.

Merge Conflicts:

Imagine that you and your friend are working on same code. initially value of variable is 2. but as per requirement you set n = 1 and your friend set n = 3. and your friend pushed code to remote. when you pull it, you get the error saying merge conflicts occurred. see. git is just a software program having some rules. it can merge the code but when it finds that the variable n has to different values, it confuses. which one to take. in simpler words when two different commits can’t be automatically merged and it need to be resolved merge conflicts occurs.

solve:

Solution is simpler than you thought. you just have to confirm the final value of variable n suppose. and commit it. that’s it. still it took me a month to understand merge conflicts. lol.

end:

If you made it this far, thank you. i really appreciate it. i hope you learn something from it. this is my first blog. please let me how can i improve. see you…

--

--