A Comprehensive Guide to Git & GitHub

Introduction

Git :-

Git is a version control system it allows us to manage and keep track of source code history

GIthub :-

Github is a cloud based hosting server .It allows us to store code and manage our git respositories.

1. Git Basics :-

Git allows us to track changes to our codebase using commits.

  1. git init :- It is used to initialise the new repository
  2. git add . :- It is used to stage all the changes.
  3. git commit -m 'commit message' :- It is used to commit changes with a message.
git init
git add .
git commit -m 'Initial commit'

2. Branching and Merging:-

Branches allow us to work on different features or fixes simultaneously

  1. git branch :- It is used to create new branch
  2. git checkout :- It is used to switch between branches.
  3. git merge :- It used to merge code between two branches. while doing this we will face merge conflicts if two branches have made the modification to the same part of the file . First we have to resolve the conflicts before proceeding .
git branch master
git checkout master
git merge main

3. Remote Repositories:-

as we discussed earlier GitHub provides a platform for hosting our Git repositories remotely.

  1. git remote add :- It will add remote repository to our local system
  2. git remote remove :- It will remove the specified repository
  3. git push : It is used to push the code to remote repository
git remote add origin repository_url
git push -u origin main
git remote remove origin

4.Collaboration and Pull Requests :-

GitHub allows for collaboration through pull requests. Fork a repository, make changes, and create a pull request to propose our changes to the original repository owner. For example:

1.git clone :- It is used to clone the entire code on remote repository to our local system

  1. git pull :- If we are working on collaboration project before pushing our code to remote repository first we have to pull the code from remote .
git clone repository_url
git pull