An Ultimate Guide to Git and Github (original) (raw)

Last Updated : 29 Nov, 2025

Git is the most widely used version control system, helping developers track changes, collaborate, and manage code effectively. GitHub builds on Git by providing a cloud platform to host, review, and share projects with ease.

git

git and GitHub

Introduction to Git

A Version Control System (VCS) is a tool that tracks and manages all changes made to your project, whether you work alone or in a team. As your project grows and new features are added, it stores every version safely. This allows you to access or restore any version without manually creating separate copies.

Distributed Version Control System means every collaborator (any developer working on a team project)has a local repository of the project in his/her local machine unlike central where team members should have an internet connection to every time update their work to the main central repository.

So, by distributed we mean: the project is distributed. A repository is an area that keeps all your project files, images, etc. In terms of GitHub: different versions of projects correspond to commits.

Git Concepts

Git Repository Structure

It consists of 4 parts:

  1. **Working directory: This is your local directory where you make the project (write code) and make changes to it.
  2. **Staging Area (or index): this is an area where you first need to put your project before committing. This is used for code review by other team members.
  3. **Local Repository: this is your local repository where you commit changes to the project before pushing them to the central repository on GitHub. This is what is provided by the distributed version control system. This corresponds to the .git folder in our directory.
  4. **Central Repository: This is the main project on the central server, a copy of which is with every team member as a local repository.

All the repository structure is internal to Git and is transparent to the developer.

**Some commands which relate to repository structure:

// transfers your project from working directory
// to staging area.
git add .

// transfers your project from staging area to
// Local Repository.
git commit -m "your message here"

// transfers project from local to central repository.
// (requires internet)
git push

Working with existing repos(git clone) and new repos(git init)

Git allows you to manage existing repository as well as new one. You can clone remote repository to work on a project that already exists, or initialize a new repository to begin tracking changes in a fresh project.

git clone

This commands downloads the entire repository including its history, branches, and files.

git clone https://github.com/username/project

Now you have a full copy of the project and can start contributed locally.

git init

This Command will create a new git repository in your current directory. It sets up a . git folder that will track all your changes made to files inside the project.

mkdir my-project
cd my-project
git init

it will create a new folder , cd will redirect to current directory and git init will initializes a new empty repository.

Use git merge Effectively in Git

git merge is used to combine changes from one branch into another. It allows teams to work on different features or fixes in isolation and then bring work together . Merging Preserves the history of both branches and is a key part of collaborative workflows in git.

git checkout main

git merge feature-branch

git add .

git commit

Creation of new Branch

Branches allows you to work on new features or fixes independently without affecting the main codebase. This isolates works and makes collaboration easier.

git branch branch-name

Creating a new Branch without switching to it.

git branch

Deletion of Branches

Once a branch has been emerged or no longer needed, you can delete it to keep your repo clean and organized.

git branch -d branch-name

Deletes the branch if it has already been merged.

git branch -D branch-name

Delete the branch regardless of its merge status

git push origin --delete branch-name

Removes the branch from the remote repository (e.g., GitHub).

Use git fetch Effectively in Git

git fetch is used to download latest changes from a remote repository without automatically merging them into your current branch. It updates your local view of the remote branches, so you can review or merge changes .

git fetch

Download update from the default remote (usually origin)

git fetch origin

git fetch origin feature-branch

Fetches only the feature-branch from the remote.

git log HEAD..origin/main

It shows changes to your main branch that aren't not in your local branch.

Use git stash Effectively in Git

git stash is a handy git command that lets you temporarily save your uncommitted changes(both staged and unstaged) so you can switch branches or perform other tasks without losing your work. Once you're ready, you can reapply those changes exactly as you left them.

git stash

Saves your changes and reverts your working directory to a clean state.

git stash save "WIP: added login form"

Adds a label so you remember what was stashed.

git stash list

Displays stashes like stash@{0}, stash@{1}, etc.

git stash apply

Restores the changes but keeps the stash in the list.

git stash apply stash@{1}

git stash pop

git stash clear

It removes all saved stashes.

GitHub

GitHub basically is a for-profit company owned by Microsoft, which hosts Git repositories online. It helps users share their git repository online, with other users, or access it remotely. You can also host a public repository for free on GitHub.

User share their repository online for various reasons including but not limited to project deployment, project sharing, open source contribution, helping out the community and many such.

Accessing GitHub central repository via HTTPS or SSH

Here, transfer project means transfer changes as git is very lightweight and works on changes in a project. It internally does the transfer by using Lossless Compression Techniques and transferring compressed files. Https is the default way to access GitHub central repository.

If you access GitHub by ssh you don't need to type your username and password every time you push changes to GitHub.

**Terminal commands:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
This does the ssh key generation using RSA cryptographic algorithm.

eval "$(ssh-agent -s)" -> enable information about local login session.

ssh-add ~/.ssh/id_rsa -> add to ssh key.
cat ~/.ssh/id_rsa (use .pub file if not able to connect)
add this ssh key to GitHub.

Now, go to GitHub settings -> new ssh key -> create key

ssh -T git@GitHub.com -> activate ssh key (test connection)

Refresh your GitHub Page.

Working with git - Important Git commands

**Git user configuration (First Step)

git --version (to check git version)
git config --global user.name "your name here"
git config --global user.email "your email here"

These are the information attached to commits.

**Initialize directory

git init

initializes your directory to work with git and makes a local repository. .git folder is made (OR)

git clone http_url

This is done if we have an existing git repository and we want to copy its content to a new place.

**Connecting to the remote repository

git remote add origin http_url/ssh_url

connect to the central repo to push/pull. pull means adopting the changes on the remote repository to your local repository. push merges the changes from your local repository to the remote repository.

git pull origin master

One should always first pull contents from the central repo before pushing so that you are updated with other team members' work. It helps prevent merge conflicts. Here, master means the master branch (in Git).

**Stash Area in git

git stash

Whichever files are present in the staging area, it will move that files to stash before committing it.

git stash pop

Whenever we want files for commit from stash we should use this command.

git stash clear

By doing this, all files from stash area is been deleted.

**Steps to add a file to a remote Repository:

First, your file is in your working directory, Move it to the staging area by typing:

git add -A (for all files and folders)
#To add all files only in the current directory
git add .

**git status

git status

git commit -a -m "message for commit"
-a: commit all files and for files that have been
staged earlier need not to be git add once more
-a option does that automatically.

git push origin master -> pushes your files to
GitHub master branch
git push origin anyOtherBranch -> pushes any
other branch to GitHub.
git log ; to see all your commits

git checkout commitObject(first 8 bits) file.txt->
revert back to this previous commit for file file.txt

Previous commits might be seen through the git log command.

HEAD -> pointer to our latest commit. 

**Ignoring files while committing

In many cases, the project creates a lot of logs and other irrelevant files which are to be ignored. So to ignore those files, we have to put their names in ".gitignore" file.

touch .gitignore
echo "filename.ext" >>.gitignore
#to ignore all files with .log extension
echo "*.log" > .gitignore

Now the filenames written in the .gitignore file would be ignored while pushing a new commit. To get the changes between commits, commit, and working tree.

git diff

The 'git diff' command compares the staging area with the working directory and tells us the changes made. It compares the earlier information as well as the current modified information.

**Branching in Git

create branch ->
git branch myBranch
or
git checkout -b myBranch -> make and switch to the
branch myBranch

Do the work in your branch. Then,

git checkout master ; to switch back to master branch

Now, merge contents with your myBranch By:

git merge myBranch (writing in master branch)

This merger makes a new commit.

**Another way

git rebase myBranch

This merges the branch with the master in a serial fashion. Now,

git push origin master

**To remove or delete a file

To remove. a file from the Git repository we use

git rm “file name”

To remove only from the staging area

git rm –cached “ file name”

**Undoing change

To change all the files to as same as the previous commit then use

git checkout -f

Git Vs GitHub

Git tracks code changes locally, while GitHub hosts Git repositories online for sharing and collaboration. Here's, the differences between Git and GitHub:

Feature Git GitHub
Type Version control system (VCS) Web based Hosting Service
Function Tracks and manages code changes locally Stores Git repositories in the cloud and enables collaboration
Installation Must be installed on your system Accessible through a web browser (no installation needed)
Usage Used for local version control and branching Used for sharing, reviewing, and managing Git projects online
Collaboration Limited to local or manual sharing Real-time collaboration with teams and contributors
Interface Command-line tool (CLI) Web-based UI and also supports Git CLI
Main Purpose Track code history and manage changes locally Centralized hub to host, view, and contribute to Git projects
Offline Support Fully functional offline Requires internet to access remote features

Contributing to Open Source

Open Source allows users worldwide to share opinions, make customizations, and work together to solve issues or build projects. Many companies host their repositories on GitHub to let developers contribute, and some even reward contributors.

Also Check: