If you are someone who is just getting started with Git or is struggling to get around this version control space. Then, this is the perfect guide for you! Here, we will start from scratch and you require absolute no coding or prior knowledge at all! You are just 3 min read away from going beginner to a master in Git! Here will talk basics and that’s more than enough to start enjoying this space! So, lets get started!!!!!!!!!!
1. What is Git ?
Git is a distributed version control system that way introduced in 2005. This is big statement let’s break it down first. ‘Version control system’- whenever you are working on a project, and hence making changes in files , the change you made-whether it is edit/delete/insert is a new version of file. Hence git helps to track what what was previously on the file to what is now here. Now, coming to the ‘distributed’ portion, when you could record changes not only in your system but also on the other developer’s system when you are working on the same project!
Git does that by comparing changes line by line and keeping a snapshot of the same at every version it creates, but all the changes internally by git are maintained using hash values. Also, Git is a noSql database(it stores values in key-values pair) with transactions and concurrency and tree structure.
You can install git from here https://git-scm.com/downloads. What you will get basically is similar to CMD (command line) called git bash. You might need to set path variable if the error of ‘git isn’t recognized as internal/external command shows up!’
2. Git Repository,Git Init, Staging Area, Working Directory
Now, repository is a great term! You must be familiar with the term folder or even directory, but what exactly is a repository! So, repository is basically directory+.git file. So, what is means is that whenever you are working with a project and use command git init( about which we will talk just after!) a .git file is created! Now, that .git file is what is maintaining all the versions, and history of changes.
Now, coming back to git init, this command sets a directory to a repository! Now, comes two new terms a) Staging area b) Working directory
A) Staging area: Try to visualize this to get it better! You would always be making changes! Editing, deleting, adding, but to keep the record of changes just like we use SAVE to have a markpoint that till this point changes are recorded is what is staging area. So, version would be maintained till changes are STAGED!
B) Working Directory: It is basically local system you are working on, because it is been compared to the server on which you would eventually push the changes to! Also when you check git status it tells you which changes are staged and which aren’t.
3.Git Remote, Origin
Lets tackle these terminologies first! The changes you do on locally are first recorded locally and then pushed onto the server! Ok! Now, remote and origin are referring to that server portion of it.
Command: Git remote origin https:/github.com/abc@username/respository_name.git
Now, you are telling the local system that the server on which you would push the code is! And origin is the name with which you would be calling your remote As!
4. Git Branch, Checkout, Master
So, master is basically the root here, and branches are sub parts/nodes/child here. So, how you actually work on git is that everyone works on their respective branches which you integrate to the root, called master here! So, you first create a branch off the master, move into the branch, make your changes on the branch and stage them.
1. git fetch origin(to get all the newly created branches from remote that is basically updating local about remote changes)
2.git branch branchone origin/branchone (you are creating a local branch ‘branchone’ referring remote branch )
3.git checkout branchone(Now, you move into the local branch and make your changes here)
5. Git Add
git add command adds up all the changes done on the different files to the staging area. You can use git add . to add all the changes or respectively add the file names which you can get from git status. so use git add file1
6. Git Commit
git commit -m”changes done on the authentication portion” : Now those staged changes are given a name and from now onwards would be referred only as commits. So, history of commits of repository would be maintained!
7. Git Push
git push origin branchname : Now you can push the changes to the remote which were just present locally . So, here it could be git push origin branchone. Note: you usually see that git push origin master, but in reality changes are never pushed to master! NEVER! Everyone creates their respective branches and these branches are merged onto the master.
8. Git Pull
git pull origin branchname:To get the changes from the remote to locally, we use git pull command. git fetch only takes down changes from remote to locally, but git pull even merge them and hence there are chances of merge conflicts because when two people are working on same file they would be having different piece of code on the same line!
9. Git Log
git log: Is a help command to help you see logs of all the previous commits, their name stored as hash values to if you ever need to rollback any change.
10. Git Status
git status: This is the best command to know what is going on! The branch you are, staged changes, is the working directory clean !
Hope, it would have brought lot more clarity from where you started. If you still have an any doubt, leave it in the comment section below. Our team would revert back within 24 hours.
Do Follow our Facebook page https://www.facebook.com/lovebeingengineer/.