DEV Community

Cover image for A Beginner's Guide on Git & GitHub for Version Control
Musungu (Ruth) Ambogo
Musungu (Ruth) Ambogo

Posted on

A Beginner's Guide on Git & GitHub for Version Control

Introduction

Version control helps developers track code changes and collaborate more effectively. Git and GitHub are tools that make this possible. Git is a version control tool, while GitHub is an online platform that stores projects in the cloud. In this article, you’ll learn the basic Git commands used for version control.

Getting Started

  • Download and install Git from the official website
  • After installation, configure git with name and email address
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com
"
Enter fullscreen mode Exit fullscreen mode
  • On the other hand, for GitHub sign up for an account and set your profile

Git Commands for Version Control

  1. git init - Initializes a directory so Git can start tracking changes
  2. git clone - makes a copy of a GitHub repository to the local machine
  3. git add - adds files to the staging area
  4. git commit - saves the files that were added to the staging area
  5. git push - Sends recent commits from your local repository to GitHub.
  6. git pull - Fetches and updates your local repository with changes from GitHub.

Initializing a git repository

  1. We’ll use a simple example project called my-first-repo to demonstrate how the Git commands work. The steps will be as follows:
  • Create a project folder
  • Navigate to the project folder
  • Then initialize it

  1. Once that's done, we will create a file in the project folder and check its status


3.Adding files to staging
Before saving changes, files must be added to the staging area.
After that, commit the changes to save a snapshot of your work.


4.Pushing to GitHub
To share your project online, you need to push it to GitHub.

  • Create a new repository on GitHub

  • Copy the repository URL
  • Connect your local project

  • Push your code  Your project is now available on GitHub

Git Collaboration and Conflict resolution

You can collaborate effectively on the same project by creating a branch on the repository. Branching allows each collaborator to work on the same project without conflict
The commands that make this possible are

  • git branch: to create a branch
  • git Checkout: to switch to the created branch
  • git merge: to merge the branch into main (or master).

Conclusion

Git and GitHub are essential tools for every developer. With just a few basic commands, you can track your work, collaborate with others, and safely manage your code. By practicing commands like git init,git add,git commit, git push, and git pull, you’ll gain confidence in version control.

Top comments (0)