Notes on GIT:
What is GIT?
o GIT is a version control tool which helps developers to track changes in code
over time and work together without breaking off the things.
o Without git we manage projects like:
final_version.zip
final_final_version.zip …
o With git:
Every change is recorded and you know who changed what.
You can easily undo the mistakes.
Many individuals confuse between Git and GIthub.
Difference between Git and GitHub is:
o Git: It is a version control tool that tracks code changes locally.
o Github: It is an online platform that hosts Git repositories and enables
collaboration.
Architecture of Git:
o Working Directory:
It is the working folder/ current/ actual folder in where you create,
delete and edit files.
Git watches this folder but changes are not yet saved.
o Staging area:
It is a temporary holding area.
You choose what files should go in the next commit.
Helps you commit what changes should go in next commit.
Basically, it is a temporary storage which consists of files to be
committed.
o Local Repository:
It is the Git Database stored in the .git folder.
It contains all the commits, history, branches etc…
Once committed, changes are permanently saved.
What are branches in Git?
o Branches are the parallel version of a repository that lets you work on new
feature or bug fixing without affecting the main code.
o In easy way, think the branch as a separate timeline of your project. You can
experiment, add features, and fix bugs and then combine it later.
Some of basic git commands:
o git init:
It is the first and most basic command of the git.
‘git init’ command initializes the your project folder into a git
repository.
It creates a hidden folder named ‘.git’ which manages and tracks all
changes in your repository.
Syntax/ How to write command: git init
o git status:
‘git status’ command shws the current state of your git repository.
Basically it tells you what is going on in your repository.
It helps to identify:
which files have changed but not yet staged.
which files are staged and are ready to be committed.
which files are untracked.
which branch you are in
whether your branch is ahead/behind the remote branch.
Syntax: git status
o git add:
‘git add’ command add all the new and modified files in directories
and sub derictories of your Repository into staging area.
Syntax:
git add . (It adds the new and modified files from your
repository into staging area, but deleted files are not added.)
git add -A (it adds all the changes including new, modified
and deleted files into staging area.)
git add file_name (it adds the specified file into staging area.)
o git commit:
‘git commit’ is used to save the files from the staged area into the Git
history or local repository.
git commit takes the snapshot of the staged files and saves to git
history.
The ‘git commit’ command takes message before storing data to local
repository. This message helps in remembering what changes were
made in that commit.
Synatx:
git commit -m “your commit message”
o git log:
It lists all the commits you have made. It shows all your commit
history.
What it shows
Commit hash (ID)
Author name and email
Date and time of commit
Commit message
Syntax: git log
o git config:
It is used to set and view Git configuration settings such as name,
email, editor etc…
git config --global [Link] “name”
This command sets the author name for commits. This name
appears in the git commit history.
git config --global [Link] “email:
This command sets the author email for commits. Email
appears in the commit logs/commit history.
Here --global means applies for all the repositories. Without --global
means applies for only current repository.
o git remote:
It connects and manages the connection between your local
repository and remote repository (GitHub repository).
Let say your remote repository is on GitHub, how to connect? here is
the syntax:
git remote add remote_name URL
remote_name: by which name your remote connection will
be identified.
URL: URL to identify/locate your remote GitHub repository.
Example: git remote add origin git@[Link]:nihal-dev-
web/[Link]
Before understanding push and pull command, we need to understand the SSH key
generation.
o The SSH keys help GitHub verify that the person pushing or pulling code is an
authorized user, without using a username and password.
o There is a pair of SSH key generated:
Private Key: Stays on your computer
Public Key: Stays on GitHub
o When a pull or push operation is performed GitHub sends a challenge and
your system signs it using your private key and GitHub verifies it using the
public key. If verification succeeds then allows to perform operation.
o To generate the public and private ssh key visit the following website or check
the below video link:
GitHub Official Documentation:
[Link]
with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent
Video Link:
[Link]
Basic Git Commands(Continue):
o git push:
It saves your local git commits to a remote repository that you
connected using git remote.
Syntax:
git push remote_connection_name branch_name
Here branch_name is optional, if you provide branch name it will push
commits in that specific branch if not provided branch_name then it
push commits of current branch in which you are working.
Example: git push origin main
o git pull:
It is opposite command of git push.
It downloads all the latest changes from your remote repository to
your local repository.
Syntax: git pull remote_connection_name branch_name
Example: git pull origin main
It pulls all the latest changes in main branch into your local repository
and origin is your remote connection name which created using git
remote.
o Note: You do-not have to execute git remote command every time
you perform git push or pull, it is only one time process and then it
is maintained for ever until you explicitly removed the connection.
o git diff:
It shows he exact change between your working directory files and
staging area files.
It tells us what has been changed in files line by line.
Syntax:
git diff
Example:
Let us say you have a file [Link].
[Link]:
Hello World
Welcome to Git
you changed [Link] and updated content is:
Hello World
Welcome to Git
Git is powerful and easy
Changes are not yet staged.
run git diff, result is;
diff --git a/[Link] b/[Link]
index e69de29..4b825dc 100644
--- a/[Link]
+++ b/[Link]
@@ -1,2 +1,3 @@
Hello World
Welcome to Git
+Git is powerful (this tells that this line was added.)
Here + or – does not indicates the exclusion or inclusion
basically they symbols are used to indicate the files.
o These were some common and basic commands which are required to work
with git.
Basic commands of git to work with branches:
o git branch:
It is used to list, create, re-name or delete a branch.
Listing branches:
Syntax: git branch (lists all the branches)
Creating a branch:
Syntax: git branch branch_name (Creates a branch of
specified branch name)
Modifying Branch name;
Syntax: git branch -m old_branch_name new_branch_name
(changes the old branch name to new branch name)
Deleting a branch:
Syntax: git branch -d branch_name (Delets a specific branch)
o git checkout/git switch
It is used to switch between branches.
git checkout is a older command and is sill supported in git and git
switch is new and modern command supported in newer versions.
Syntax of git checkout:
git checkout branch_name (switches to specific branch)
Syntax of git switch:
git switch branch_name (switches to specific branch.)
git switch -c branch_name (creates a specific branch and
switches to it.)
Issue during switching the branch is that before switching between branches you
have to compulsorily commit the changes and then only you can switch otherwise git
does not allows to switch.
Suppose you are working in one branch and other developer is working with other
branch, if other developer needs your help then you have to switch the branch, but
without committing your changes you cannot switch. If you have now completed
your changes/work and without verifying changes you cannot switch. To solve this
problem there is command called ‘git stash’.
What is git stash?
o git stash temporarily saves uncommitted changes and cleans the working
directory, allowing safe branch switching.
o How it works
Saves your changes in a hidden stack
Resets working directory to last commit
Lets you switch branches freely
You can restore your work later
o Syntax: git stash
o To List all stashes command is: git stash list
o To apply/retrieve stash without deleting it from the stack command:
git stash apply stash_id (this id is given to each stash in the stack to
identify uniquely and which stash you want to load in your branch)
o To apply/retrieve stash and remove it from stack:
git stash pop (pop the top most stash and load changes to your
current branch).
Understand Git Clone command.
o git clone is used to create a local copy of an existing remote Git repository
(like GitHub).
o When you run git clone:
Downloads the entire repository
Copies all files, branches, and commit history
Automatically sets the remote name as origin
Creates a ready-to-use local repository
o Syntax:
git clone URL
o Example:
git clone git@[Link]:nihal-dev-web/[Link]
Understanding Git Merge command:
o It is used to merge two different branches.
o For example, there are two branches main branch and login branch.
o As a developer you were working on login branch and you completed your
login functionality and tested it and verified that it worked properly.
o Now login branch is to be merged with main branch to integrate the login
functionality to main app.
o To do this git merge command is used.
o Syntax: git merge branch_name
o Now if we want to merge login branch with main branch steps are:
Switch to target branch(the branch which will receive changes), in this
case main branch, using git switch/git checkout command.
Run the git merge command in main branch: git merge login
This merges the login branch to main branch.
o On merging branch, git creates a merge commit, git opens a editor to write a
commit message for merge.
o Generally, git opens VIM editor by default.
o How to enter commit message in vim editor:
Vim is a default text editor in linux.
To insert/write message press insert key. Then you will enter in insert
mode.
Write your commit message and press esc/escape key.
Write :wq (colon wq) and press enter this will save your changes and
exit vim editor.
You can also refer online sources to understand how to work with vim
editor.
Understanding about .gitignore:
o .gitignore is a special file in a Git repository that tells Git which files or
directories to ignore and not track.
o Git will not stage, commit, or push the files listed in .gitignore.
o Useful for temporary, sensitive, or generated files.
o Why .gitignore is important?
Avoid committing unnecessary files (e.g., logs, compiled files)
Keep sensitive information safe (e.g., passwords, API keys)
Reduce repository size
Prevent merge conflicts for system-specific files
o .gitignore only works for untracked files.
o If a file is already committed, adding it to .gitignore does not remove it from
Git.
These are the basic things/commands/concepts to be known to work with git and
github.
How to create repository on github:
Step 1: Log in to GitHub
1. Open GitHub in your browser.
2. Sign in with your GitHub account.
3. If you don’t have an account, create one first.
Step 2: Click on “New Repository”
1. On the top-right corner, click the ➕ (plus) icon.
2. Select “New repository” from the dropdown.
Step 3: Enter repository details
1. Repository name → give a unique name (e.g., my-first-repo).
2. Description (optional) → short explanation of the repo.
3. Visibility → choose:
o Public → anyone can see
o Private → only you (or collaborators) can see
4. Initialize with README → optional, but recommended to create a README
automatically.
5. Optionally, add .gitignore or license.
Step 4: Create the repository
Click “Create repository”.
✅ Now your GitHub repository is ready.
For Learning More About GIT following resources/videos can be
referred:
o [Link] (Chai or
Code)
o [Link]
tKUJTWPi&si=BA1Y--BGGKHgv5Yk (Code with Harry)
o [Link]
TePXwIln&si=VDZXN__tKbg-5yo_ (Code with Harry)
o [Link] (Apna
College)
o [Link] (Free Code
Camp)