In this blog, I am introducing Git , one of the most widely used Version Control systems.
Git is a free and open sourced Version Control System you download in your computer that can keep track of the changes you make to your code/project. It is important to know Git is different from GitHub and GitLab. GitHub, GitLab are web-based repositories(hosting services) that help with code management and sharing local file changes with a remote repository. Many companies are using Git, that includes Google, Microsoft, Twitter, Linkedln, Facebook, NetFlix .
You can download and install Git from this link.
After installing or if already installed, you can check the version of Git using git --version command.
Getting started with Git:
We can use Git either with terminal or GUI. if you are using terminal, it requires you to have understanding of the commands to be used whereas GUI requires understanding of steps to be followed. But the basic understanding of the work flow remains same for both. You do not have to memorize all the commands except some, which are used often. You can explore all the commands in the documentation.
Here, we can see how we use terminal to prepare a project to use Git. You can use Git Bash(terminal).
Let us begin with preparing an existing project to use Git.
Step 1: Prepare the project
In the terminal, navigate to the project folder
Example:
cd "/C/Users/myFolder/myProject"
Step 2: Initialize a git Repository
Once you are in your project folder, you need to initialize a git repository using the git init command
User /C/Users/myFolder/myProject (main) git init
This will create a hidden folder .git inside your project folder in the background. You can see that using ls -a command. This is your local git repository. This repository will keep track of all the changes you make to the project over time.
Step 3: Commit the changes
This include two steps:
a) git add
Before committing, you have to explicitly tell Git about what changes you have made in the project. This means that a file won't be automatically included in the next commit just because it was changed. Instead, you need to use the git add command to mark the desired changes to include.
git add .
Or you can explicitly mention the changes to be added using the following syntax:
git add index.html
After adding, you can commit the changes. A commit is the version of your project. It represents all the changes you made in your project which include all the files and folders that are part of the changes.
b) git commit
Now you can commit the changes into the local repository by using git commit command. You can also pass messages to the commit using git commit -m message.
git commit -m "First Commit"
What is happening behind the scene???
When you are creating a .git folder , Git will be just aware of the files and folders included in the project. It will not keep track of the changes until you commit it. In order to understand what is happening behind, you need to know about Stages of Git Life Cycle.
A project under Git Version Control System falls under three Git stages:
Working directory
Staging area
Git directory
These three stages are the core features of Git. You get great flexibility in tracking the files. Due to these stages, the files can reside under Git.
Let’s understand each of these states one by one.
Working directory:
The project residing in your local machine is the Working Directory. It may or may not be tracked by Git. When you initialize the git repository(.git), Git will be aware of the files and folders of the project but still will not be tracking the changes. This is also called as Working Directory. To track the changes you have to add the changes to the Staging area using git add.
Staging area:
You are in Working Directory and you want to track the changes in the project. In that case, you will add those files into the local repository. In other words, staging area is the area where you group or add the files to be committed.
Git Directory:
After grouping/ adding the files to the staging area, you can now commit the changes.
Apart from commit message, this step also records the author and time of the commit. Now, a snapshot of the files in the commit is recorded by Git. The information related to this commit (names of files committed, date and time of commit, author of commit, commit message) are stored in the Git directory. Now your project is ready to be pushed to the repository.
The next step is to push the local repository to the remote repository. For that we need to create a remote repository.
There are number of websites that allow hosting . Among them the most popular website is GitHub. GitHub is the website that stores Git repositories. You can access these repositories and manipulate them as required.
Lets see how you create your first repository:
Go to www.github.com and signup for a free account. Then go to github.com/new
1. Give a repository name, say myWebsite
2. Give a description, say 'This is my website using html , css , scss, and python'.
3.Now choose option if it need to be
public : Anyone can see the repository, but you can choose who can commit
private: You can choose who can see and commit
4. Click Create to create the repository
An empty repository is created. Now, you can push your project into this repository.
Step 4:
For the first time you are pushing the code into a remote repository, you have to add the remote repository path in Git where your repository is stored, using the command git remote add in the terminal
git remote add <name> <url>
For Example:
git remote add origin https://github.com/Git_Username/myWebsite.git
You will get the url from the repository you created.
Before you continue further, we can discuss a little about git remote command and its uses.(You may skip to step 5 directly if you are already familiar with git remote)
git remote:
The git remote command lets you create, view, and delete connections to other repositories. Remote connections are more like bookmarks rather than direct links into other repositories. The git remote command is essentially an interface for managing a list of remote entries that are stored in the repository's ./.git/config file. The following commands are used to view the current state of the remote list.
To list all the remote connections to all the remote repositories:
git remote
To list all the remote connections with url:
git remote -v
To create a new connection to a remote repository. After adding a remote repository with name, you’ll be able to use this name as a convenient shortcut for other Git commands.
git remote add <name><url>
To remove the connection to the remote repository
git remote rm <name>
To rename a remote connection from to
git remote rename <old_name> <new_name>
Step 5: Before pushing the code , you can add a pointer to the commit. This is called branch.
By default , a branch called master is created when you create a repository. Here I am renaming it as main
git branch -M main
You can skip to Step 7 if you are already familiar with branches
What are Branches?
Branches are pointer to commit performed. Branches gives you the freedom to independently work on different modules and merge the modules when you finish developing them. The default branch in Git is master or main.
How branch works?
Generally master will be the default branch when you create a repository . But creating new branch depends on your requirement. Lets think that you are working on a feature and your friend is working on another feature. In this case you both can create two different branches and commit your progress separately on your branches. Once the features are complete and working as a stable code, you can merge it with the main branch.
What if one of the features is no longer needed for some reason, you can delete that branch without merging. This feature is an exceptional one, when dealing with bugs and rollbacks which I will cover in my subsequent blogs.
Some common git branch options are:
To list all the branches in the repository:
git branch
To create a new branch, this does not check out the new branch:
git branch <branch_name>
To delete the specified branch. This is a “safe” operation, where Git prevents you from deleting the branch if it has unmerged changes.
git branch -d <branch_name>
Force delete the specified branch, even if it has unmerged changes. This is the command to use, if you want to permanently remove all of the commits associated with a particular line of development.
git branch -D <branch_name>
To rename a branch:
git branch -m <branch>
To list all remote branches:
git branch -a
Step 6: Push the code into the repository.
git push -u origin main
Your project will be pushed into your Git repository. The above steps are for pushing the project for the very first time. One you have pushed your first Commit, you can further follow the following steps for subsequent pushes .
Step1: Navigate to the project path and check if remote repository is added
git remote -v
Step 2: Check git status
The git status command displays the state of the working directory and the staging area. It lets you see which changes have been staged, which haven't, and which files aren't being tracked by Git.
git status
Step 3: Add the modified files to the Staging area
You can either specify the name of file to git add or use git add . to add all the changes.
git add .
Step 4: Check the status again to make sure all the changes are added to the staging area
git status
Step 5: If you see all the changes to be committed ,the next step is commit
git commit -m "write descriptive message"
Now you can see the number of insertions and deletions performed. Just check the status using git status to make sure no more files need to be committed.
Step 6: You can review the history using git log
Git log is an utility tool to review and read a history of all events that happens in a repository. Multiple options can be used with a git log to make history more specific. Generally, the git log is a record of commits.
git log
Step 7: Push the changes into the repository
git push origin main
Note: If someone else already committed to this branch before you and it is not updated in your local repository, it may throw an error. In this case, you can create a new branch or if you are committing to the same branch, you have to perform 3 additional steps:
Step1: Fetch the remote repository using git fetch or git pull command
git fetch
Step2: You can view the difference in your local repository compared to remote repository using the following command:
git diff main origin/main
Step 3: Merge the changes
git merge
Now you can push the changes as mentioned in Step 7.
Hope this blog helped you to understand basic git commands, create your own repository and push your code into your repository. Happy Reading!