Git and GitHub introduction for beginners
This article is solely based on creating a basic foundation for absolute beginners about what git and github is.
Git is a version control system. Version control is keeping track of changes made to a project. As the project grows, it becomes difficult to keep track of all the changes and manage those changes. So, we use version control system i.e. git to help perform this task for us. GitHub, on the other hand is a cloud based central repository that stores all changes made to our project. Whenever a team of developers is involved, we often use GitHub to sync project among all developers in real time. GitHub also helps ease of code integration of multiple developers to the central repository and helps manage conflicts in our code, if any occurred.
How are changes tracked by Git?
Git has 3 important locations to store a file’s version. They are
i. Working directory
ii. Staging Area
iii. Local repository
The changes made to a file at first stands at a working directory. To keep track of these changes, we need at first send these files to staging area. The command to send any file to staging area is:
git add <file_name>To send all files to staging area, we do:
git add *Staging area holds all files that are to be committed and tracked next. The files in staging area are considered ready to be committed to a repository. To send files from staging area to a repository, we need to commit them. For commit, we do:
git commit -m <commit_message>If you want to follow a proper git message pattern, then the accepted convention for git commit is as below:
git commit <type>(scope): <commit message><body of the commit><footer>
Git commits consists of header, body and footer. Header is mandatory but scope in header is optional. Body of the commit consists of the motivation for that commit. Lastly, footer consists of any breaking changes in that commit and any github issues that this commit closes.
After committing the files, files are then transferred to a repository and hence now will be tracked by git with proper commit message hash.
How GitHub comes into play?
Upto now, we talked about how git stores the changes. Now we will discuss how github comes into play after this step. One of the developer makes changes and commits to his/her local repository but in a team there are many developers whose code might be dependent on his/her code. So we need to make that changes available to a central repository so that other team members can access that code and move forward. For this developers after committing to their local repository, if all the test passes, pushes to github. They then create a pull request/merge request to merge those changes to a main branch.
Get Sulav Panthi’s stories in your inbox
Join Medium for free to get updates from this writer.
In a working environment, developers should not push their code directly to the main branch. Instead they work on specific features and push to a different branch and later create a merge request to merge those changes to the main branch. After changes are merged, other team members can work on features on top of that pushed code. This is the standard practice being followed by software engineering community throughout the world and is the best practice till now.
How to link local git repository to a remote GitHub repository?
To link local and remote repository, we first copy the ssh link of the remote repository and add that remote to our local by following command:
git remote add <remote_name> <remote_repo_ssh_link>
e.g. git remote add origin git@github.com:django/django.gitTo check/list all remote repositories linked to our local, we have following command:
git remote -vGit branch
We always work by creating branches and later merge those branches to the main branch if it passes all the tests. To create a new branch, we have following command:
git branch <branch_name>To navigate between branches, we do checkout:
git checkout <branch_name>There is a shorter command that combines both of the above operations, that command is:
git checkout -b <branch_name>Git Merge
After making necessary changes to a branch, we merge that branch into main or whichever branch we need to. First we need to checkout to destination branch where we want to merge new changes by following command:
git checkout mainThen we merge changes from the newly created branch by following command:
git merge <branch_name>Conclusion
This articles hence consists of basic git operations and commands that will help beginners to get comfortable working with git and github. Obviously this is just the tip of an iceberg, so I will be posting few more articles about git and github in future. Keep learning and have a good day ahead.
Thank you.







