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 enables version tracking, branching, and seamless teamwork.
- GitHub adds cloud hosting, pull requests, issues, and collaboration tools.
- Together, they power open-source development and large-scale project management.
git and GitHubIntroduction 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.
- Eliminates the need for folders like MyProject, MyProjectWithFeature1, etc.
- Makes rolling back or comparing versions quick, safe, and effortless.
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
- Repository- A directory where git monitors your project files and records their revision history.
- Clone- It created a local copy of a remote repository on your machine.
- Stage - It Selects specific changes that you want Git to include in the next snapshot.
- Commit- It records the staged changes as a permanent version in the project history.
- Branch- Lets you develop new features or experiment without affecting the main project.
- Merge- Integrates changes from one branch into another.
- Pull- It fetches and applies updates from a remote repository to your local one.
- Push- It Uploads your local commits to a remote repository
Git Repository Structure
It consists of 4 parts:
- Working directory: This is your local directory where you make the project (write code) and make changes to it.
- 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.
- 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.
- 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.
- Working with existing repos (git clone)- When you are collaborating on a project that already exists on the remote platform like GitHub, you don't need to start from scratch . Instead you can clone the repository to your local machine using:
git clone <repository-url>
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.
- Started a new Repository (git init) - If you are beginning a brand new project you use:
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.
- Switch the branch you want to merge into your changes
git checkout main
- Merge Changes from another branch into the current branch
git merge feature-branch
- If there are merge conflicts resolve them manually, then stage the changes
git add .
- Commit the merge if conflicts were resolved manually
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.
- Force delete a local branch(unmerged changes)
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 .
- Fetch all branch from the remote
git fetch
Download update from the default remote (usually origin)
- Fetch from a specific remote
git fetch origin
- Fetch from a specific branch
git fetch origin feature-branch
Fetches only the feature-branch from the remote.
- View changes after fetching
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.
- Stash your current changes
git stash
Saves your changes and reverts your working directory to a clean state.
- Stash with a custom message(recommended)
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.
- Apply the most recent stash
git stash apply
Restores the changes but keeps the stash in the list.
git stash apply stash@{1}- Apply and delete a stash in one step
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.
- By git remote add origin http_url: remote means the remote central repository. Origin corresponds to your central repository which you need to define (hereby giving HTTPS URL) in order to push changes to GitHub.
- Via SSH: connect to Linux or other servers remotely.
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 "[email protected]"
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 [email protected] -> 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
- Untracked files: Files Git is not tracking yet (not added to the staging area).
- Changes not staged for commit: You modified files after staging them earlier, so the new changes need to be staged again.
- Changes ready to be committed: Files that are staged and ready to be committed (after committing, they can be pushed to the remote repository).
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.
- Contribute by forking the repository on GitHub.
- Apply your desired changes in the forked project.
- Submit a pull request for the project owner to review, suggest improvements, or merge.
Also Check:
Which command creates a new Git repository.
-
git init
-
Cd..
-
Rmdir
-
mkdir
Explanation:
For making a new git repository git init is used while if we want to create a new directory mkdir is used.
To check the status of the staged area we can use
-
git check
-
git status
-
git givestatus
-
git checkstatus
Explanation:
git status gives the status of the staged area.
-
Updating a branch
-
Creating a branch
-
Deleting a branch
-
Switching between branch
Explanation:
For creating a branch git branch command is used.
What is the difference between git and GitHub?
-
git is a service while Github is a software
-
git is software while Github is a service
-
They both are software
-
They both provide services
The feature that allows copying the entire repository and running on our local machine is_____.
-
Merging
-
Cloning
-
Pushing
-
Pulling
Explanation:
Cloning is the correct answer rest terms are used for files or code or pull requests but not for the repository.
Which command is used for moving to a particular commit?
Explanation:
Correct syntax for command is git checkout so option (a) is correct.
What is the meaning of reverting commits?
-
Adding new commits in the repository
-
Changing the name of the last commit
-
Reversing the changes done by the commit
-
Changing the position of the head with given commit id
Explanation:
Reverting commits is similar to undo operation so it reverse the changes done by the commit.
Quiz Completed Successfully
Your Score : 2/7
Accuracy : 0%
Login to View Explanation
1/7
1/7
< Previous
Next >
Explore
How To Become
Roadmap
Interview Preparation
Project Ideas
Certification