Git
Version Control (also called Source Control) is a system that tracks changes to files over time so that you can recall specific versions later.
GIT full form is “Global Information Tracker,” a powerful version control system widely used for software development and other collaborative projects. GIT allows multiple developers to work on a project simultaneously while ensuring that their changes do not interfere with one another.
- consider git as tree
- trunk/master is one n has branches.
🚀 Git Branching Workflow (Git Flow)
This model uses a set of dedicated branches for managing features, releases, and hotfixes, making it ideal for managing large-scale software projects.
🔵 0. Main / Master / Trunk Branch
-
Purpose: Holds production-ready code.
-
Behavior: Continuously integrated and contains official release history.
-
Rule: Only stable code gets merged into this branch.
🟡 1. develop Branch
-
Purpose: Acts as the integration branch for features.
-
Usage: All features and changes are first merged into
developbefore going tomain. -
Command:
🟢 2. Feature Branches
-
Purpose: For developing new features or enhancements.
-
Base Branch:
develop -
Merge Target:
develop -
Naming:
feature/<feature-name> -
Commands:
🔴 3. Release Branches
-
Purpose: Prepares the codebase for production release.
-
Base Branch:
develop -
Merge Target:
mainanddevelop -
Naming:
release/<version> -
Commands:
-
After Testing:
🟠 4. Hotfix Branches
-
Purpose: Quickly fix bugs in production.
-
Base Branch:
main -
Merge Target:
mainanddevelop -
Naming:
hotfix/<issue-name> -
Commands:
-
After Fixing:
🔐 What is a Git SSH Key?
An SSH key is a cryptographic credential used to securely connect to remote repositories (like GitHub, GitLab, Bitbucket) without entering your username and password every time.
It uses:
-
Public Key: Shared with the remote (like Bitbucket).
-
Private Key: Stored securely on your machine. Never share it.
This setup allows Git to authenticate securely when pushing/pulling code.
How to Generate and Use a Git SSH Key
- ssh-keygen -t ecdsa -C "your_email@example.com"
- add public key to bucket
- eval "$(ssh-agent -s)"
- ssh-add /path/to/your/private_key
================================================================
SETUP
Configuring user information used across all local repositories
>git config --global user.name “[firstname lastname]” -- set a name that is identifiable for credit when review version history
>git config --global user.email “[valid-email]” -- set an email address that will be associated with each history marker
>git config --global color.ui auto -- set automatic command line coloring for Git for easy reviewing
=================================================
🧱 1. Initializing a Repository
🔹 Create a New Git Repository
-
This creates a
.gitfolder inside the directory. -
It initializes a new main branch (or master, depending on config).
-
It’s a one-time setup for a new project.
🔄 2. Cloning a Repository
🔹 Clone an Existing Remote Repo
-
Clones the entire codebase, including its
.githistory, into a new local directory. -
Typically used to start working on existing GitHub/Bitbucket projects.
⚙️ 3. Git Configuration
Git lets you configure settings at 3 levels:
view all configgit config --list --show-origin
==================================================================
📌 What git init Does
Initializes a new Git repository in your project folder.
👉 It creates a hidden .git directory that stores:
-
Commit history
-
Branch information
-
Configuration
-
Metadata
==================================================================
🌿 git branch – Branch Management in Git
==================================================================
What is HEAD?
HEAD is a pointer that refers to the current branch or current commit you are working on.
🔹 Example
If you are on main branch:
git branch
Output:
* main
develop👉HEAD→ points tomain🔹 Detached HEAD
When you checkout a specific commit:
git checkout a3f5c2d👉 HEAD points to a commit, not a branch
👉 This is called Detached HEAD state==================================================================
What is
git reflog?
git reflogshows the history of HEAD movements.It records:
Branch switches
Commits
Resets
Rebase
Even deleted commits can be found here 🔥
==================================================================
🔹 What is a Fork?
A fork is a personal copy of someone else's repository on GitHub.
It allows you to:
-
Experiment safely
-
Make changes
-
Contribute to open-source projects
🔹 What is Clone?
👉 git clone downloads a repository from GitHub to your local machine.
Command:
git clone https://github.com/user/repo.git
Used when:
-
You want to work locally
-
You have access to the repo
==================================================================
git add – What It Does
git add is used to move changes from your working directory to the staging area (also called the index).
👉 In simple words:
It tells Git, “Include these changes in the next commit.”
| Command | What It Does | When To Use |
|---|---|---|
git add file.txt | Stages a specific file | When you want to commit only one file |
git add file1 file2 | Stages multiple specific files | When committing selected files |
git add . | Stages new, modified & deleted files from current directory | Most commonly used in daily work |
git add -A | Stages all changes in entire repository | When working from different directories |
git add -u | Stages only modified & deleted files (not new files) | When you don't want to include new files |
git add -p | Interactive staging (hunk by hunk) | When you want fine control over changes |
==================================================================
🔖 What is a Tag in Git?
A Git tag is a marker that points to a specific commit in your Git history — typically used to mark version releases like v1.0, v2.1, etc.
Creates a label (tag) for a specific commit.
Used mostly for version releases like v1.0, v2.1, etc.
👉 Tags do NOT change code.
👉 They just mark a commit.
1️⃣ Lightweight Tag
A lightweight tag is simply a pointer to a commit.
It does not store extra information.
Example
git tag v1.0
This creates a tag pointing to the current commit.
Characteristics
-
Just a reference to a commit
-
No metadata
-
No message
-
Not recommended for official releases
Think of it like a bookmark.
Example concept:
Commit A ---- Commit B ---- Commit C
↑
v1.0
2️⃣ Annotated Tag
An annotated tag stores extra information.
It is stored as a full object in Git.
Example
git tag -a v1.0 -m "First stable release"
This creates a tag with:
-
tag name
-
message
-
tagger name
-
date
Characteristics
-
Contains metadata
-
Has a message
-
Can include GPG signature
-
Recommended for production releases
Example:
Tag: v1.0
Message: First stable release
Author: rk
Date: 2026
git log
git log is used to view the commit history of a Git repository.🔹 .gitignore
📌 What is .gitignore?
A special file used to tell Git which files or folders to ignore (not track).
👉 Ignored files will NOT appear in git status
👉 Used for temporary, log, build, or secret files
🔹 Basic Example
Create a file named:
.gitignore
Example content:
git Status
📌 What git status Does
Shows the current state of your repository, including:
-
Modified files
-
Staged files
-
Untracked files
-
Current branch
📌 What git clean Does
Removes untracked files from your working directory.
⚠️ It does NOT remove tracked files.
⚠️ It permanently deletes files (cannot undo easily).
=========================================================
git stash
📌 What git stash Does
Temporarily saves (shelves) your uncommitted changes so you can switch branches or work on something else.
👉 It stores changes in a stack-like structure.
=========================================================
git revert and git reset
🔁 Git Revert – "Undo, but keep the history"
➤ What it does:
-
Creates a new commit that reverses the changes made by an earlier commit.
-
Does not delete the original commit.
-
Used when you want to undo a change safely, especially in shared/public branches.
➤ Analogy:
Think of it like writing something in a notebook, then adding a new note that says:
“Ignore the previous page, it was wrong.”
You're not tearing out the old page—you’re adding a new page to cancel it out.
➤ Example:
This creates a new commit that undoes the last commit.
🔄 Git Reset – "Rewind history"
➤ What it does:
-
Moves the pointer of your branch (HEAD) backward in time to a previous commit.
-
You can choose whether to keep your file changes or discard them depending on the flag used:
-
--soft: Keep changes staged -
--mixed: Keep changes in working directory (unstaged) -
--hard: Discard all changes (⚠ deletes!)
-
➤ Analogy:
It's like going back in time and rewriting the past—as if the later commits never happened.
➤ Example:
This removes the most recent commit and all its changes.
==================================================================
🔀 Git Merge vs Git Rebase
🟦 Git Merge
✅ What it does:
Combines changes from one branch into another by creating a merge commit that ties together the histories of both branches.
➤ Example:
This creates a commit like:
-
Creates a merge commit
F -
Keeps the original branch structure
-
Useful in team environments to preserve history
🟨 Git Rebase
✅ What it does:
Moves or "replays" your branch commits on top of another branch, rewriting history to make it linear.
➤ Example:
This turns:
Into:
-
No merge commits
-
Cleaner, linear history
-
Changes commit hashes because it rewrites commits
==================================================================
🧾 git diff — Compare Changes in Git
The git diff command is used to show the differences between:
-
Your working directory and the staging area
-
The staging area and the last commit
🍒 git cherry-pick — Apply Specific Commits from Another Branch
git cherry-pick is used to selectively apply a specific commit from one branch to another. It reapplies the changes introduced by the chosen commit(s) as a new commit on your current branch.
=============================================================
🌐 git remote — Manage Remote Repositories in Git
The git remote command lets you connect your local repository to remote repositories (like GitHub, GitLab, Bitbucket, etc.). This is how you push your local changes to the cloud and pull updates from collaborators.
🛠️ Example Workflow
✅ 1. Add a Remote
-
originis the name of the remote (a convention) -
The URL is the remote repository's address
🔍 2. View Remotes
🚀 3. Push to Remote
Pushes your local main branch to the remote named origin.
📥 4. Pull from Remote
Gets the latest changes from the remote main branch.
=======================================================================
Git fetch
🔄 git fetch — Download Changes from Remote Without Merging
The git fetch command downloads commits, files, and refs from a remote repository into your local repo, but does not automatically merge those changes into your working branch.
git pull = git fetch + git merge
🧠 Key Idea:
Use
git fetchwhen you want to see what's new in the remote before integrating it into your local branch.
📥 What git fetch Does:
-
Gets the latest commit history and branch updates from the remote.
-
Updates your local references (e.g.,
origin/main) to match the remote. -
Does NOT modify your working directory or staging area.
🚀 git push — Upload Your Local Changes to a Remote Repository
The git push command is used to upload local commits to a remote repository. It updates the remote branch with changes made in your local branch.
Git checkout
🔹 GitHub
📌 What is GitHub?
GitHub is a cloud-based platform that hosts Git repositories and allows developers to:
-
Store code online
-
Collaborate with teams
-
Manage projects
-
Track issues
-
Automate workflows
It is owned by Microsoft (since 2018).
Your Laptop (Git) → Internet → GitHub (Remote Repo)
Git Cheat Sheet
1. Git configuration
- Git config
Get and set configuration variables that control all facets of how Git looks and operates.
Set the name:
$ git config --global user.name "User name"
Set the email:
$ git config --global user.email "himanshudubey481@gmail.com"
Set the default editor:
$ git config --global core.editor Vim
Check the setting:
$ git config -list - Git alias
Set up an alias for each command:
$ git config --global alias.co checkout
$ git config --global alias.br branch
$ git config --global alias.ci commit
$ git config --global alias.st status
2. Starting a project
- Git init
Create a local repository:
$ git init - Git clone
Make a local copy of the server repository.
$ git clone
3. Local changes
- Git add
Add a file to staging (Index) area:
$ git add Filename
Add all files of a repo to staging (Index) area:
$ git add* - Git commit
Record or snapshots the file permanently in the version history with a message.
$ git commit -m " Commit Message"
4. Track changes
- Git diff
Track the changes that have not been staged: $ git diff
Track the changes that have staged but not committed:
$ git diff --staged
Track the changes after committing a file:
$ git diff HEAD
Track the changes between two commits:
$ git diffGit Diff Branches:
$ git diff< branch 2> - Git status
Display the state of the working directory and the staging area.
$ git status - Git show Shows objects:
$ git show
5. Commit History
- Git log
Display the most recent commits and the status of the head:
$ git log
Display the output as one commit per line:
$ git log -oneline
Displays the files that have been modified:
$ git log -stat
Display the modified files with location:
$ git log -p - Git blame
Display the modification on each line of a file:
$ git blame <file name>
6. Ignoring files
- .gitignore
Specify intentionally untracked files that Git should ignore. Create .gitignore:
$ touch .gitignore List the ignored files:
$ git ls-files -i --exclude-standard
7. Branching
- Git branch Create branch:
$ git branchList Branch:
$ git branch --list Delete a Branch:
$ git branch -dDelete a remote Branch:
$ git push origin -deleteRename Branch:
$ git branch -m - Git checkout
Switch between branches in a repository.
Switch to a particular branch:
$ git checkout
Create a new branch and switch to it:
$ git checkout -bCheckout a Remote branch:
$ git checkout - Git stash
Switch branches without committing the current branch. Stash current work:
$ git stash
Saving stashes with a message:
$ git stash save ""
Check the stored stashes:
$ git stash list
Re-apply the changes that you just stashed:
$ git stash apply
Track the stashes and their changes:
$ git stash show
Re-apply the previous commits:
$ git stash pop
Delete a most recent stash from the queue:
$ git stash drop
Delete all the available stashes at once:
$ git stash clear
Stash work on a separate branch:
$ git stash branch - Git cherry pic
Apply the changes introduced by some existing commit:
$ git cherry-pick
8. Merging
- Git merge
Merge the branches:
$ git merge
Merge the specified commit to currently active branch:
$ git merge - Git rebase
Apply a sequence of commits from distinct branches into a final commit.
$ git rebase
Continue the rebasing process:
$ git rebase -continue Abort the rebasing process:
$ git rebase --skip - Git interactive rebase
Allow various operations like edit, rewrite, reorder, and more on existing commits.
$ git rebase -i
9. Remote
- Git remote
Check the configuration of the remote server:
$ git remote -v
Add a remote for the repository:
$ git remote addFetch the data from the remote server:
$ git fetch
Remove a remote connection from the repository:
$ git remote rm
Rename remote server:
$ git remote rename
Show additional information about a particular remote:
$ git remote show
Change remote:
$ git remote set-url - Git origin master
Push data to the remote server:
$ git push origin master Pull data from remote server:
$ git pull origin master
10. Pushing Updates
- Git push
Transfer the commits from your local repository to a remote server. Push data to the remote server:
$ git push origin master Force push data:
$ git push-f
Delete a remote branch by push command:
$ git push origin -delete edited
11. Pulling updates
- Git pull
Pull the data from the server:
$ git pull origin master
Pull a remote branch:
$ git pull - Git fetch
Download branches and tags from one or more repositories. Fetch the remote repository:
$ git fetch< repository Url> Fetch a specific branch:
$ git fetch
Fetch all the branches simultaneously:
$ git fetch -all
Synchronize the local repository:
$ git fetch origin
12. Undo changes
- Git revert
Undo the changes:
$ git revert
Revert a particular commit:
$ git revert - Git reset
Reset the changes:
$ git reset -hard
$ git reset -soft:
$ git reset --mixed
13. Removing files
- Git rm
Remove the files from the working tree and from the index:
$ git rm <file Name>
Remove files from the Git But keep the files in your local repository:
$ git rm --cached
STAGE & SNAPSHOT
Working with snapshots and the Git staging area
>git status show modified files in working directory, staged for your next commit
>git add [file] -- add a file as it looks now to your next commit (stage)
>git reset [file] -- unstage a file while retaining the changes in working directory
>git diff diff of what is changed but not staged
>git diff --staged diff of what is staged but not yet committed
>git commit -m “[descriptive message]” -- commit your staged content as a new commit snapshot
BRANCH & MERGE
Isolating work in branches, changing context, and integrating changes
>git branch list your branches. a * will appear next to the currently active branch
>git branch [branch-name] create a new branch at the current commit
>git checkout [branch] switch to another branch and check it out into your working directory
$ git checkout -b iss53
Switched to a new branch "iss53">git merge [branch] merge the specified branch’s history into the current one
>git log show all commits in the current branch’s history
INSPECT & COMPARE
Examining logs, diffs and object information
>git log show the commit history for the currently active branch
>git log branchB..branchA show the commits on branchA that are not on branchB
>git log --follow [file] show the commits that changed file, even across renames
>git diff branchB...branchA show the diff of what is in branchA that is not in branchB
>git show [SHA] show any object in Git in human-readable format
SHARE & UPDATE
Retrieving updates from another repository and updating local repos
>git remote add [alias] [url] add a git URL as an alias
>git fetch [alias] fetch down all the branches from that Git remote
>git merge [alias]/[branch] merge a remote branch into your current branch to bring it up to date
>git push [alias] [branch] Transmit local branch commits to the remote repository branch
>git pull fetch and merge any commits from the tracking remote branch
REWRITE HISTORY
Rewriting branches, updating commits and clearing history
>git rebase [branch] apply any commits of current branch ahead of specified one
>git reset --hard [commit] clear staging area, rewrite working tree from specified commit
TEMPORARY COMMITS
Temporarily store modified, tracked files in order to change branches
>git stash Save modified and staged changes
>git stash list list stack-order of stashed file changes
>git stash pop write working from top of stash stack
>git stash drop discard the changes from top of stash stack
>git checkout chgbrnch -- change branch
>git checkout -b newbrnch -- create new branch n move but not yet push
Comments
Post a Comment