The Beginner’s Guide to Git and GitHub

git-guide-simplenet

More and more top employers, including Google and Microsoft, are focusing more on Stack Overflow, Git, and GitHub accounts than on resumes.

These accounts say a lot more about your knowledge and skills than a CV because they effectively reveal what you’ve worked on.

Past or side projects, contributions to open source works, and your interaction with other developers are more relevant than a self-proclaimed title of “Master of JavaScript.”

Under these circumstances, you’d better learn how to get the most from Git and GitHub. To do that, start with our beginner’s guide – Git for beginners. Both tools have a steep learning curve, so be ready to invest substantial time and resources.

What is Git?

Git-Official-Site
Git

Linus Torvalds created Git in 2005 to help his development team work collaboratively on the Linux kernel. Over time, Git turned into the favorite version control system (VCS) of most developers, coders, and designers.

A VCS records the changes performed to a file or a set of files over time, and it allows users to recall particular versions later on. This functionality is golden for teams of developers working on complex code.

What is GitHub?

GitHub-Official-Website
Github

You need a place to store the files you use for each project, and GitHub is the preferred solution of Git users. It’s a remote storage solution trusted by millions of users, so you can use it confidently.

On top of that, it’s free (you only have to pay when you want to keep private repos and your team is bigger than four members).

Vocabulary

Some of these terms might be familiar to you, while some might be totally new. Either way, you should know them by heart if you plan to work with Git and GitHub in the future.

  • Commit: An individual change to a project. It’s similar to the “Save” option in Microsoft Word or Google Docs, but it shows the date and the author of the commit. A useful rule of thumb: any time you commit, write a short message explaining what you have done.
  • Diff: The difference between two commits.
  • Push: When you send one or more commits to a remote repository. It happens when you’re working on a project, and you want your contribution to be reviewed and eventually added to the project.
  • Clone: A copy of a repository stored on your hard drive.
  • Fork: A clone of a repository stored in your account storage space.
  • Branch: A copy of a repository that doesn’t affect the master copy. It’s a useful feature when a number of developers are working on the same project. A developer can work on one branch without adding any changes to the master.
  • Pull Request: A proposed change carried on a fork or a branch that is submitted to be reviewed by the team members.

Git and GitHub Installation

I assume that your computer runs on Mac or Windows. If you are a Mac user, download the Git version you need here. Windows users may download Git version for their operating system here.

Creating an account on GitHub is similar to creating an account on Facebook. You can do it in no time.

Basic Commands

Now it’s time to use Git and GitHub like a pro. Open a terminal of your choice (Git Bash or Powershell) and write the following line to check if Git is correctly installed:

$ git --version

Once Git is correctly installed, you have to personalize it. The following two lines of code will configure your username and email address:

$ git config --global user.name “Your Name”
$ git config --global user.email “example@mail.com”

Create a Repository

At this stage, you have installed and configured Git. Create the first repository to start your work. Go to the GitHub dashboard and click on the + button in the top right bar. For learning purposes, I’ve created a test project without initializing the project with a README file. Hit the Create repository button, and voila! Your first repo is online!

Clone a Repository

Follow the next steps to clone a repository on your computer:

  • Go to the repository to get cloned and copy the HTTPS address.
  • Create a directory to host the clone. Use the mkdir <folder name> command in your terminal.
  • Git clone <HHTPS address> is the command to clone the repo onto your hard drive.
  • If you have accurately followed all the steps and your repo is empty, you will receive the following message “You appear to have cloned an empty repository.”

Add a File to the Repo

Follow the next steps to add a file to your local repository:

  • Go to the local repository and create a file in that directory. Let’s use an HTML snippet called “Table1.html.”
  • Make sure that your working folder in the command line is the local repository.
  • Write the command git add <filename> (in our case, Table1.html) or git add . to add all the files to the repo.
  • To make sure you add the file, write git status.
  • Commit the file staged in your local repo with git commit -m “Your message”.
  • Finally, write git push origin master in the command-line text area. Now, the file (in our case, Table1.html) should be in your GitHub repo.

Working Collaboratively

A huge advantage of Git is the possibility of working collaboratively. Being part of a team implies working individually on a particular feature and getting feedback from colleagues. Git allows for this type of work.

Let’s suppose you have a group of files—the master project. You develop a new feature based on the master project and start adding your own contribution. Next, you submit your work for review by your collaborators.

In Git terminology, you work on a branch without affecting the master (aka, the agreed-upon files). If you use the command line, you create a branch by typing this command:

git checkout -b new_feature 

(replace new_feature with your favorite title).

From now on, you’ll work on your branch and rest assured that your work doesn’t interfere with the master copy. You can merge your branch with this command:

git merge new_feature

Wrapping Up

Working with Git and GitHub isn’t simple.

This guide features only a limited number of basic commands, but many others are available to streamline your work. Don’t worry if you don’t write the correct commands on the first try; it has happened to every beginner.

You will get accustomed to working with Git and the command line after hours of practice. The advantages of Git are significant, so your endeavors will pay off in the long run.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *