![]() |
VOOZH | about |
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.
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.
It consists of 4 parts:
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
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 <repository-url>This commands downloads the entire repository including its history, branches, and files.
git clone https://github.com/username/projectNow you have a full copy of the project and can start contributed locally.
git initThis 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.
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 maingit merge feature-branchgit add .git commitBranches 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-nameCreating a new Branch without switching to it.
git branchOnce a branch has been emerged or no longer needed, you can delete it to keep your repo clean and organized.
git branch -d branch-nameDeletes the branch if it has already been merged.
git branch -D branch-nameDelete the branch regardless of its merge status
git push origin --delete branch-nameRemoves the branch from the remote repository (e.g., GitHub).
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 fetchDownload update from the default remote (usually origin)
git fetch origingit fetch origin feature-branchFetches only the feature-branch from the remote.
git log HEAD..origin/mainIt shows changes to your main branch that aren't not in your local branch.
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 stashSaves 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 listDisplays stashes like stash@{0}, stash@{1}, etc.
git stash applyRestores the changes but keeps the stash in the list.
git stash apply stash@{1}git stash popgit stash clearIt removes all saved stashes.
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.
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.
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 masterOne 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 stashWhichever files are present in the staging area, it will move that files to stash before committing it.
git stash popWhenever we want files for commit from stash we should use this command.
git stash clearBy 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 statusgit 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 diffThe '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 branchNow, merge contents with your myBranch By:
git merge myBranch (writing in master branch)This merger makes a new commit.
Another way
git rebase myBranchThis merges the branch with the master in a serial fashion. Now,
git push origin masterTo 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 -fGit 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 |
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.