Git and GitHub: Hosting and collaboration

What is Git?

Git is a version control system - an open-source platform - used to keep track of changes in source codes. It is maintained by Linux.

Git lets you:

  • Keep track of code changes in your project
  • clone projects
  • manage projects
  • merge, pull and push projects updates to the code repository.

What is GitHub?

GitHub is a development platform to host source codes/projects and collaborate. Additionally, GitHub serves as a project management tools that may be used to assign tasks to collaborators and monitor the advancement of each contributor. GitHub makes it easier for developers from all over the world to collaborate on projects using git. GitHub is owned by Microsoft.

Although there are other version control systems like Gitlab and BitBucket, GitHub is the most widely used VCS.

To get started using Git and GitHub, ensure you have Git installed on your local machine, configure it, and create a GitHub account. Build a project or select an existing project that that isn't hosted on GitHub and let's begin.

Using Git and GitHub to host personal projects

Create a repository for your project on your GitHub profile by clicking the "+" icon in the top-right corner of your profile and choosing "New Repository" to start a create a new repository.

plus icon for new repo.png

Input a name and description(optional) for your project repository and hit the "Create Repository" button.

create repo.png

It will lead you to the repository page where you can get the repo link. Copy out the repo link as it will be used for the next set of steps. The link is the https link in the box.

copy repo link.png

Now to move on to git, you can make use of the command prompt or terminal. I'll be working on the terminal in this article.

Open up your project terminal in your vscode or preferred IDE.

Steps

  • git init to initialize git for the project folder
  • git add . to add all the project files and folders to the staging environment
  • git status to check the status of all your files
  • git commit -m "commit message"
    
    This is done to keep track of the changes and progress of the work. Always include a message to the commit. For example; git commit -m "first commit" or git commit -m "made website responsive".
  • git branch -M main to specify that we're working on the main branch of the repository
  • git remote add origin repo-link-you-copied to specify that you're adding a remote repository as an origin to your local git repo. The link to be added is the one you previously copied from your repo when you created it.
  • git push -u origin main to push to the GitHub repo.

Go to your GitHub to see that the project repository has been updated with all your files and folders present there. This is how it would like now on your GitHub:

final look after push.png

If you want to add or make some changes to the same project again. Go to your vscode, work on it. Then on your terminal:

  • git add .
    
    or git add index.html if it's only that file you made changes to
  • git commit -m "commit message"
    
  • git push -u origin main
    
    That is all you need to do to host your personal projects on GitHub using Git.

Using Git and GitHub to collaborate or contribute to a team's project

Assuming you're in a team of developers where you've been tasked to work together on a project, you can all contribute to it without having to be in the same location. Git and GitHub aid collaboration by following a few steps and git commands. You'd have been added as a collaborator by the project maintainer using your GitHub email or GitHub username. You receive a URL to the project repository, fork it, clone it, commit, pull, and push it. Don't worry if you're seeing new terms here, I'll explain each of them.

Forking a repository

To fork a repository is to download a complete copy of the repository to your personal computer. Visit the project repo url that you've been sent, click the "fork" tab at the top-right corner.

final look after push (2).png

Click the "Create Fork" button at the bottom. The page that shows up next is your forked version of the repository. Copy the repo link.

Ensure you've navigated to the folder in vscode that you want the project to be in. Go to your project terminal and clone the repository as follows:

git clone copied-repo-link-here

You'll observe that because you cloned the project folder, the files and folders for the project are now automatically present in your project folder.

git checkout -b branch-name

This is done to create a new branch and move to it so that that's where you will work on. This is done to avoid working directly on the repo's main branch and avoid making mistakes on the main branch of the repository.

Work on your own assigned code section. Then:

git add .
git commit -m "I worked on my section"
git push -u origin branch-name

Go to your forked repo on GitHub and you'll see the branch you pushed with the changes you worked on. Do a git pull origin main . This is done to make sure that your code is up to date in case any changes have been made by other collaborators. Go to your forked repo and make a pull request. The repo owner or project maintainer will review your work through GitHub and merge it if it's satisfactory.

NB: Before you begin working on the project again, always run a git pull to update any modifications that others may have made.

For every developer, GitHub is necessary. Since hiring managers and other developers may readily check your projects there, it occasionally serves as a portfolio. To customize your GitHub profile, read through this article I wrote on how to go about it.