Skip to main content

Command Palette

Search for a command to run...

Git for Beginners: Basics and Essential Commands

Updated
3 min read

Git is a version control system,it is used to track your code and changes you made ,i mean suppose you are working on a project on your laptop(locally) but you want your friend to make some changes in the project .

So now you should share complete project code with your friend but how?? You can share the project code in multiple ways but how can we make sure that whatever changes done by your friend are tracked .Or else you might give the code to your friend ,he/she might make changes in the code ,but how will you know that which changes are done by your friend.

So to solve this issue,we have GIT (version control system) and Github,Bitbucket,etc which are the servers to host the Git .You might be confused how this Git and GIthub solves the issue.

So Git tracks all the changes in the project by creating .git folder in it.Whenever you make a change in the code ,it tracks the change by storing all the changes in the .git folder. And coming to sharing the project with your friend ,you can push the code to Github ,from where your friend can fork it and clone the project in his/her laptop/computer.The changes made by your friend can seen by you.

Basic Flow

The above diagram explains the basic flow of how we can push the code to the Github Repo. There are Working Directory,Staging Area,Local Repository ,Remote Repository.

Working Directory: Where you are making changes in the code locally in the Laptop.

Staging Area:So before sending all the changes to github repository,we can filter out what changes need to be saved in the github Repo.Staging area is used for that filtering ,as i said above git add file-name or git add . can be used to stage the files. git add file-name is used to stage a particualr file but git add . is used to save all files to staging area .

Local Repository: so after the staging area,the git need to take snapshot of the code so that it can track the changes made in the code.git commit -m “ message” this command can be used to commit the chnages.Then we can push the code to remote repository by command “git push -u origin main “,remember this command is for first time ,then we can use the short command -”git push” as the git remembers the branch .

SOME JARGONS IN GIT AND GITHUB

Repository:This is the place where your project code is present .Repository might local or remote,Local Repository is the one which is present in your laptop and Remote Repository is the one which is present in the Github.

Commit:You can think commit as the changes saved in the code.For example ,you want to add a color feature in the code,so you make changes locally in your laptop and push those changes to the Github.These saved changes are called commits.

Branch: You are working on a project ,want top add a new feature to the project but little confused because it might broke the currently working code.So in these cases ,we can create a new part /space ,which has same code as current project and expirement on it .Once the feature is working without any error ,you can merge that code to origin/main code.So here the space/part we created is called the branch ,the original code is main .So whenever we need to try out some changes on the code ,we can create new branch ,work in it ,without distrubing the original code.

Head: It shows where we currently are in the repository like the branch and the commit.Example : Main branch and abc123 commit.

Some Essential Commands :

git init: It initializes the git in a project ,so that changes can be tracked .

git status:It shows what changes are done in the code like which files are modified .

git log: It shows the commits and with messages.

git logoneline :It will also show the commits but in short form.