DEV Community

Cover image for Introduction to Git & GitHub
Prolayjit Biswas
Prolayjit Biswas

Posted on

3

Introduction to Git & GitHub

Introduction :

Git and GitHub are powerful tools for version control and collaboration. They are essential for modern software development. Git and GitHub are indispensable tools for developers, providing version control, collaboration features and community support. They have revolutionized software development.


What is Git ?

Git is a version control system designed to handle everything from small to very large projects with speed and efficiency. It allows multiple developers to work on a project simultaneously without interfering with each other's work. It also helps developers track changes in their code and revert to previous versions when needed. It is used locally on a computer.

  • Repository : A storage space where project files and their history are stored.
  • Commit : A snapshot of repository at a specific point in time.
  • Branch : A separate line of development. Branches allow you to work on different features or fixes without affecting the main codebase.
  • Merge : Combining changes from one branch into another.
  • Clone : Creating a copy of a repository from a remote server to local machine.
  • Pull : Fetching and integrating changes from a remote repository to local repository.
  • Push : Sending local changes to the remote repository.

What is GitHub ?

GitHub is a cloud-based platform for hosting Git repositories. It enables collaboration, code sharing, and open-source contributions. It provides a web-based interface and additional features such as :

  • Collaboration Tools : Issues, pull requests, and project management tools for teamwork.
  • Social Coding : You can follow other developers, star repositories, and watch projects to stay updated.
  • Documentation : Markdown support for README files, wikis, and project pages.
  • Pull Requests: Propose changes and review code.
  • Issue Tracking: Manage bugs and feature requests

Workflow : Using Git with GitHub

Initial Setup :

  • Git Installation :

First, click on this link to download git on the system : https://git-scm.com/downloads

  • Pre-requisite :

Having an IDE like VS Code , if not download from here : https://code.visualstudio.com/

After the instalation run the below command on git bash to ensure that the installation is done successfully.

git --version
Enter fullscreen mode Exit fullscreen mode
  • GitHub Setup :

Login on GitHub and create a repository.


Git Configuration : git config

After creating an account on GitHub, use the same Username and Email adress in the bash to configure the Git. Actually, it a process of linking git with GitHub.We use git config command for this :

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
Enter fullscreen mode Exit fullscreen mode

To check the configuration settings :

git config --list
Enter fullscreen mode Exit fullscreen mode

Cloning of a Git Repository : git clone

To see a copy of a GitHub repository in our local system, we use the command git clone :

`git clone https://github.com/username/repository.git`
Enter fullscreen mode Exit fullscreen mode

For example, in the code below:

Username : Prolayjit-B14

Repository Name : is C_Programming

git clone https://github.com/Prolayjit-B14/C_Programming
Enter fullscreen mode Exit fullscreen mode

git clone “GitHub Repository URL”


Initializing a Git Repository : git init

After creating a repository on GitHub, use the git init command to initialize Git in local directory :

git init
Enter fullscreen mode Exit fullscreen mode

Change Directory : cd

The cd command is used to navigate between directories in the command line.

cd folder-name
Enter fullscreen mode Exit fullscreen mode

Example: This moves into the Documents folder.

cd Documents
Enter fullscreen mode Exit fullscreen mode

Adding Files : git add .

The git add . command is used to add files to Git repository, use the this command. This stages changes, making them ready for commit :

git add <file-name>
Enter fullscreen mode Exit fullscreen mode

For adding all files at a time :

git add .
Enter fullscreen mode Exit fullscreen mode

Track Changes : git status

The git status command is used to tract the files in the repository in the local system :

git status
Enter fullscreen mode Exit fullscreen mode

Commit Message : git commit

This git commit command records changes to the repository after adding files. It saves a snapshot of the current project state :

git commit -m "Commit message"
Enter fullscreen mode Exit fullscreen mode

For an example, when the commit message is initial commit, the command would be :

git commit -m "Initial commit"
Enter fullscreen mode Exit fullscreen mode

A Git commit message should be clear, concise, and descriptive.


Commit History : git log

The git log command shows the commit history of the current active repository :

git log
Enter fullscreen mode Exit fullscreen mode

*Push Changes : `git push`*

The git push command pushes Changes from a Specific Branch to a Remote Repository :

git push <remote-name><branch-name>
Enter fullscreen mode Exit fullscreen mode

For an example :

git push origin main
Enter fullscreen mode Exit fullscreen mode

Here’s what each part means :

git push → Pushes changes to the remote repository.

origin → Refers to the remote repository (GitHub, etc.).

main → The branch name that is pushing to.


Download Changes : git fetch

This command downloads changes from a remote repository but does not merge them into your local branch :

git fetch origin
Enter fullscreen mode Exit fullscreen mode

Switch Branch : git checkout

The git checkout command is used to switch branches, restore files, create new branches in Git :

git checkout branch-name
Enter fullscreen mode Exit fullscreen mode

Combines Changes : git merge

The git merge command combines changes from one branch into another :


git merge branch-name
Enter fullscreen mode Exit fullscreen mode

Pull Changes : git pull

Thi git pull command is used to download and merge the latest changes from the remote repository into local branch :

git pull <remote-name><branch-name>
Enter fullscreen mode Exit fullscreen mode

For an example :

git pull origin main
Enter fullscreen mode Exit fullscreen mode

Here’s what each part means :

git pull → Fetches and integrates changes from the remote repository.

origin → Specifies the remote repository (usually GitHub, GitLab, etc.).

main → The branch you want to update from the remote.


Delete Files : git rm

The git rm command is used to remove files from the Git repository and working directory :

git rm filename
Enter fullscreen mode Exit fullscreen mode

Conclusion

Git and GitHub have revolutionized software development by enabling seamless collaboration, efficient version control, and secure code management. While Git provides the core version control functionalities, GitHub enhances it with cloud-based tools for teamwork and continuous integration. Together, they form an essential ecosystem for developers and open-source contributors.


GitHub Cheat Sheet


Here are few of my repositories :

  1. Event Management Website : https://github.com/Prolayjit-B14/Hackentine
  2. Project Showcase Website : https://github.com/Prolayjit-B14/Project-IoT
  3. Education Platform : https://github.com/Prolayjit-B14/Data-Daynamos
  4. Indian Classical Music : https://github.com/Prolayjit-B14/Bug-Fixes

If you like it, connect with me here :

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay