A Beginner's Guide to Git and Github

A Beginner's Guide to Git and Github

Git and Github are essential tools to programmers. They keep track of changes in a file and also encourages collaboration between programmers.

Think of Git and Github as a time machine that can go back in time to provide information on what happened, how it happened, and who caused a change in the past.

In this article, you'll learn how to:

  • Install and configure git
  • Create a local git repository
  • Create a remote repository
  • Push files to github

What is git?

git.jpg Git is open-source software that tracks changes made to a file. It records snapshots of changes made. Linus Torvalds developed git in 2005.

A Software that tracks changes made to a file is known as Version Control System (VCS). There are various version control systems, like Subversion, Mercurial, but git is the most widely used.

Reasons why you should use git

  • Git makes it easy to view the timeline of changes made to a project.
  • Git encourages collaboration among developers.
  • Git allows you to view, restore and make changes to the previous version of a code.
  • Git lets you push projects to a remote repository.

Download, install and configure git

Click on one of the links below to download and install git on your device

Now open up git and run your first git command.

git --version

The command above will return the current version of git on your device.

The next step is to configure git. Git has to know who you are, therefore include your username and email address. Git uses this information to document the contributor whenever there is a change to the code.

git config --global user.name "myusername"
git config --global user.email "myemail@mail.com"

Hurray! You've successfully installed git and run your first git command. Now the real work begins.

Working with git

Let's imagine you have a project and you want to track this project with version control. Follow the steps below to start working with git

Step 1: Initialize a git repository

A git repository is the .git/ folder inside a project. It tracks and records all the various versions of the project. The .git/ folder is hidden by default on your machine.

To initialize a git repository in a project, open the project with git or open git and change directory cd to the project root directory.

cd /Users/user/my_project

Next, initialize a git repository in the project by running the command below

git init

At this point, there is nothing for git to track because you've not told git the files you want to track.

Step 2: Add files

Run the command git add to specify the files you want git to track.

git add index.html
git add style.css
git add app.js

Adding files one after the other can be time-consuming. The command below adds all file in your project folder at once:

git add .

If don't want to add a file, simply remove the file by running this command git rm --cached filename. For example

git rm --cached style.css

Now, you've specified the files you want git to track. The next step is to save all the changes with a commit.

Step 3: Commit changes

A "git commit" command saves all the changes to the local repository. It captures a snapshot of a project along with a description from the user and the user identity.

To commit the tracked files to the local repository, use the commit command along with a short message -m "short message". For example

git commit -m "Initial commit"

Make sure your commit message is in the present tense.

Now, git has a record of the current state of your project. If you make any change to the file and want to commit that version to git, you have to run the git add command and git commit command with a git message describing the change.

Note that the project is located on your machine and can be accessed only through your machine. This is where Github comes in. Github hosts your project on the web, making it easy to access from any device and by anyone(if you choose to keep your repository public).

What is Github?

github.jpg Github is a code hosting platform for version control and collaboration. Github has all the benefits of git and even more.

Reasons why you should use GitHub

  • Github hosts your repository for free on the web.
  • Github encourages collaboration with other developers around the world.
  • Github lets you clone projects you love to your machine and start working on them immediately.
  • You can never lose your file on Github except you intentionally delete it.
  • Github allows you to contribute to any open source projects of your choice.
  • Github is a community of over a 56million developers. It is a great place to showcase your work to the world and learn from others.

Join Github and create a remote repository

Creating a GitHub account is very simple. Go to github.com and signup with your email address and password.

Github.png

To create a GitHub repository, follow the steps below:

Step 1: Go to the top right corner of your GitHub homepage. Click on the plus button. Next, click on the new repository.

New repository.png

New Repository2.png

Step 2: Input a name for your repository

Repository name.png

Step 3: Initialize the repository with a README file.

A README file contains important information about your project. It's a form of documentation that gives a detailed explanation of your project.

Add a READ Me.png

Step 4: Click the "create repository" button.

Create Repository.png

You've created a remote repository. It's time to push your project.

Push your project to GitHub

To push your project to GitHub, make sure you are currently in the project directory and you've committed all changes, then run the command below on git.

git branch -M main
git remote add origin https://github.com/yourusername/repositoryname.git
git push -u origin main

Since this is your first time pushing to GitHub, GitHub will require you to validate your credentials by asking you to log in. Next, refresh your GitHub page and, you will see your project on Github.

If you make any change to your local repository and you would like to push the change to GitHub, you don't have to initialize a repository again since you've done that already. Just add the changes using the git add command, commit your changes and push to Github.

Other git commands you should know.

  • git status lets you know the changes that are tracked and those that are not.
  • git log displays the history of commits on your local repository.
  • git clone downloads a copy of a remote repository to your machine.
  • git pull pulls the latest update from a remote repository to the local version on your machine.
  • git branch lets you create or delete a branch

Conclusion

Git and Github are tools you will work with as a developer. They may seem hard to learn on the first try, but with constant practice, you will be comfortable using these tools. I hope this article was helpful. If you have any questions, feel free to ask in the comment section below.

Working with Git and Github 1.png

Resources