The Guide to Git and Github

I have always felt a need for someone who could cater to my doubts now and then, but it is not always possible. So for all the beginners out there I am starting with a series of posts to guide them through in and out of different skills I have gained in recent years of my Undergraduation.  I will keep on adding things to these posts, hope they turn out to be helpful J.

So in this post we will talk about popular version control, open source platform for code sharing and collaboration - Git and Github. Firstly, we talk about what is meant by version control system. A version control system (VCS) refers to a system that can be used to track the additions and deletions made in a project by various contributors. A VCS helps us to reach to any change/edit that was made in the project at any time. It is open for all to see what changes other people have made and why these changes were made. Git is a distributed version control system (DVCS) used for open source and commercial software and project development. Git is generally preferred for locally commits on your system. Changes can be later committed to Github directly from Git itself (we will discuss the git and github commands shortly). With Git team members can avoid redundant tasks, slower timelines, and multiple copies of a single project. It allows contributors to work at different timezones and enjoy the benefits of an open source developments (To know more about open source, follow my blog- https://empiricaltechno.blogspot.com/2019/02/wwcd-mentorship-programs-week-4.html ). 



Github is a cloud based code hosting platform built around the Git tool i.e., it uses the core principles of Git to host codes and softwares. For starting with Git and Github, you need to make an account at https://github.com/ . You can download desktop version of Github or operate it using their website. For Git, however, you must have Git( Git Bash) installed on your systems. Download the Git from https://git-scm.com/downloads . Github is simpler to start with so I recommend exploring it first. Few basic terms and options that you will encounter in Github are discussed below.

  1.  Repository- It is used to organize a single project and contains folders and files containing codes, images, videos, data sets, etc. that we need to include in our project to work. A repository can be created using “New Repository” tab while adding details to your project like a line description of your project, README.md (use this to add a brief description of your project with other important details), License files( choose from a variety of options).
  2. Once your repository has been created you can upload your codes and files using Upload files tab. Try exploring various options that you see on this window.
  3. Branch- It is used to when the person wishes to work on the project while not disturbing the main project details. If you wish to contribute to an open source project where people are already contributing, you may wish to separate your current work from others in order to maintain controlled commits to the project. Here we use branch. You will need to create a new branch for this. The default main branch for your repository is always ‘master’ branch which can be changed later. To create a new branch click the drop down at the top of the file list that says branch: master and enter new branch name, thereby clicking ‘create branch’.
  4. Commits- Commits are the additions or deletions you make to your repository. This includes adding new codes, files, images, etc. Whenever you make up any commit they are shown as green boxes on your profile.
  5.  Pull Requests- This is something that you need to work upon extensively while contributing to any project. When you open a pull request, you propose your changes and request that someone review and pull in your contribution and merge them into their branch. Once you have created a branch of your project, you can open a pull request to let developers know about your code and if everything is good you can ask for merging your commits to the main branch.
  6. Merging your pull request- Once you have successfully made valid commits, you can merge your pull request. Click the green Merge pull request button to merge the changes into master. Click Confirm merge. To delete the branch with the Delete branch button in the purple box.



Now that we have discussed about Github, it’s time to explore Git and Git Bash. As stated earlier Git is encouraged for local commits on the system and preferred over directly using Github in the way that it offers wide variety of committing options and ways to rectify too!

As a newbie, we may often commit mistakes while learning stuff. In VCS, however, each mistake is as much visible as is a contribution. Hence, avoid making them. We will however in this post also deal with some methods to rectify them. Well, if you have installed Git on your system, you might be able to open Git Bash.

So in order to configure your local Git to sync with your Github account, you need to run following commands on the Git Bash ( Note: Commands described in this post are relevant to Windows Operating System, commands for others may/mayn’t differ)- 

o   git config user.name “Your Github account user name”
o   git config user.email “Your Registered email with Github”

To clone( copy a repository at your device) –

o   git clone  github_repository_url

In order to navigate to the respective repository and/or branch of active change the path of the directory by using      
       
o   cd directory_path

Once you are in your local repository copy, you can –

o   Add a file to it using -  git add file_name
o   Commit recent added files -  git commit –m “Commit message”
o   Push your changes to Github -  git push origin branch_title
o   Force push your changes to Github - git push origin branch_title –f

In order to delete few erroneous commits –

o   Delete a file/folder -  git rm –r  file/folder_name
o   Delete previous N commits – git reset - -soft HEAD~N

But what if you accidently delete more of what was required? Well, I did that and that’s why I am writing up this post to let you avoid them. So, even if you accidently delete things, no need to worry. Use the following –

o   To see what were the last few commits – git reflog
o   Now, to revive last x2x345x commit use – git reset - -hard  x2x345x


Now, I hope you might feel much more familiar to Git and Github. Don’t stop learning and explore other things as well and most important of them all, make your contribution count! With regards, I now end up this blog post. If you feel struck up somewhere or have any doubts do write me at the comment section.  

Comments

Popular posts from this blog

The Next Scholarship on the List

Earning your next Internship

Women Who Code Delhi Mentorship Program: Week 1