close ×

Git

History & Basics

Git
0

History

What is version control?

Version control is a system that helps track and manage changes to files, especially in software development. It allows multiple people to collaborate on a project without overwriting each other's work and provides a history of modifications. Version control helps in:

  • Tracking Changes: Every modification is recorded, making it easy to revert to previous versions if needed.
  • Collaboration: Multiple developers can work on the same project simultaneously without conflicts.
  • Backup & Recovery: If something goes wrong, you can restore an earlier version.
  • Branching & Merging: Developers can create separate branches for new features or bug fixes, then merge them back into the main project.

Version control systems (VCS) help developers track changes, collaborate efficiently, and manage different versions of a project. There are three main types:

  1. Local Version Control Systems (LVCS):
    • Changes are stored locally on a single computer.
    • Each modification is saved as a patch, making it difficult to collaborate with others.
    • Risk of losing data if the local system fails.
  2. Centralized Version Control Systems (CVCS):
    • A single repository stores all versions, and users must connect to it to access or modify files.
    • Examples: Subversion (SVN), Microsoft Team Foundation Server (TFS).
    • Easier collaboration but has a single point of failure—if the central server crashes, all history could be lost.
  3. Distributed Version Control Systems (DVCS):
    • Each user has a full copy of the repository, allowing offline work and better redundancy.
    • Examples: Git, Mercurial.
    • More flexible and resilient, as changes can be merged from multiple sources.

Table of Contents

  1. History
    • What is version control?
    • Limitations of Version control
    • What was the need of git?
  2. What is git?
  3. Git vs Github?
    • Key Differences
    • Alternatives
  4. Git Lifecycle
  5. Configuration
  6. First Configurations
  7. Working with Remote-repository



Git References


Limitations of Version control

Before Git, developers relied on centralized version control system. It had limitations like single points of failure and restricted offline work. Developers cannot have their own copy of the repository and hence, cannot work offline independently.

These drawbacks led to the rise of Git, which offers a distributed approach for better flexibility and resilience. Git was created to address the challenges of managing code efficiently, especially in large-scale projects with multiple contributors.


What was the need of git?


What is git?

Git is a distributed version control system that helps developers track changes in their code, collaborate efficiently and manage different versions of a project. Git was initially developed by Linus Torvalds in 2005 for managing the Linux kernel, but its advantages quickly made it the industry standard for version control.

Here is why it is widely used:

Meaning of distributed

In Git, distributed means that every developer has a complete copy of the repository, including its entire history, on their local machine. Unlike centralized version control systems, where a single server holds the main repository, Git allows developers to work independently without needing constant access to a central server.


Git vs Github?

Git is the engine, while GitHub is the garage where teams collaborate.

Key Differences:

Feature Git GitHub
Type Software Hosting Service
Function Version control Repository management
Installation Local Cloud-based
Collaboration Command-line based Web inteface with additional tools
Ownership Open-source (Linux foundation) Owned by Microsoft

Alternatives:


Git Lifecycle

Stash


  • git stash = Stash current changes.
  • git stash list = View stashed changes.
  • git stash pop = Apply and remove the latest stash.
  • git stash apply = Apply the latest stash without removing it.
  • git stash drop = Delete a specific stash.

A temporary locker — Temporarily save changes which we’re not ready to commit.

Workspace or
Working Directory

  • git status = Show changes in the working directory and staging area.
  • git diff = View unstaged changes.
  • git checkout --<_filename_> = Discard changes in a file.
  • git clean -fd = Remove untracked files and directories.

This is where we create or modify files. Changes here are not yet tracked by Git.

Index or
Staging Area

  • git status = Show changes in the working directory and staging area.
  • git add <_filenme_> = Stage a file
  • git add . = Stage all changes
  • git reset <_filename_> = Unstage a file
  • git diff --cached = View staged changes

Where we prepare changes before committing.

Local Repository


  • git commit -m "commit_message" = Commit staged changes
  • git log = View commit history
  • git reset --soft Head^ Undo last commit, keep changes staged.
  • git reset --hard Head Reset to last commit, discard all changes

Where committed changes are stored locally.

Upstream or
Remote Repository

  • git remote -v = Show remote URLs
  • git fetch = Get changes from remote without merging
  • git pull = Fetch and merge changes from remote
  • git push = Push local commits to remote
  • git clone <_url_> = Clone a remote repository

Shared repository for collaboration (e.g., GitHub).

Explanation:

Example:

Scenario: We're working on a new feature, but suddenly need to fix a bug on the main branch. Want to stash your current work, switch branches, fix the bug, and then return to the feature.

  1. Start from feature branch (current work). We're now in our workspace, editing files like login.js and style.css.
    git checkout -b feature/login-form
  2. Make some changes. Modified both files but haven’t committed anything yet. Shows modified files in the workspace.
    git status
  3. Stage one file. Now login.js is in the staging area, while style.css is still in the workspace.
    git add login.js
  4. Stash both stages and unstaged changes. This saves our progress (both staged and unstaged) and resets the working directory to match the last commit.
    git stash push -m "WIP: login form"
  5. Switch to main and fix the bug. This commit is now in our local repository.
    git checkout main
    # Fix the bug in app.js
    git add app.js
    git commit -m "Fix: crash on login redirect"
  6. Push the fix to remote. Now our fix is in the remote repository (e.g., GitHub)
    git push origin main
  7. Return to the feature branch
    git checkout feature/login-form
  8. Reapply the stashed changes. Our changes to login.js and style.css are restored to the workspace and staging area as they were.
    git stash pop

Summary:

Git Area What happened
Workspace Edited files like login.js and style.css.
Staging Area Staged login.js before stashing.
Stash Saved both staged and unstaged changes temporarily.
Local Repository Committed the bug fix to the local repo.
Remote Repository Pushed the fix to GitHub (or another remote).


Download and Installation


Configuration

Step1: Generating SSH key pair

  1. Open a terminal (Git Bash on Windows, Terminal on macOS/Linux).
  2. Run the following command to generate a new RSA key pair:
    ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
    Replace "your_email@example.com" with your GitHub email.
  3. When prompted, press Enter to accept the default file location (~/.ssh/id_rsa).
  4. Optionally, enter a passphrase for added security.

Step2: Add the SSH Key to the SSH Agent

  1. Start the SSH agent:
    eval "$(ssh-agent -s)"
  2. Add your private key to the SSH agent:
    ssh-add ~/.ssh/id_rsa

Step3: Add the SSH Key to GitHub

  1. Copy the public key:
    cat ~/.ssh/id_rsa.pub
  2. Log in to GitHub and go to Settings → SSH and GPG keys.
  3. Click New SSH Key, paste the copied key, and save.

Step4: Test the connection

Run:

ssh -T git@github.com

If successful, will see a message like:

Hi username! You've successfully authenticated, but GitHub does not provide shell access.

First Configurations

Purpose Commands
Set Identity
Git requires a username and email for commits.
git config --global user.name "Your Name"
git config --global user.email "your_email@example.com"
Configure Default Branch Name
By default, Git initializes repositories with the master branch. Can change it to main:
git config --global init.defaultBranch main
Configure Line Endings (Windows/Linux Compatibility) If on Windows, set up automatic line ending conversion:
git config --global core.autocrlf true
For macOS/Linux:
git config --global core.autocrlf input
Set Default Editor
Choose preferred text editor for Git commit messages.
git config --global core.editor "code --wait" # VS Code
git config --global core.editor "vim" # Vim
git config --globalcore.editor "nano" # Nano
Enable Credential Caching
To avoid entering credentials repeatedly.
git config --global credential.helper cache
View All Configurations
To check current Git settings
git config --list --show-origin

Use Cases

Connecting with Remote-repository

Here we're assuming that you already have a repository on github and you're ready with the url.

Step1:Cloning the repository

  • In your pc/laptop go to the folder where you want to clone the repository(download all files from github).
  • Open the cmd/terminal at the location
  • Run the below command. Replace your-organization/repository-name with the actual GitHub repository URL.
    git clone https://github.com/your-organization/repository-name.git

Step2: Navigate to the Repository

Move into the cloned repository:

cd repository-name

Step3: Set Up the Remote

Ensure the remote is correctly set:

git remote -v

If needed, add the remote:

git remote add origin https://github.com/your-organization/repository-name.git

Step4: Pull Updates Regularly

To keep local repository updated with the latest changes. Replace main with the default branch name if it's different

git pull origin main

Step5: Handle Merge Conflict

If there are conflicts, resolve them manually, then commit the changes

git add .
git commit -m "Resolved merge conflicts"
git push origin main

Synchronizing local to Remote-repository

Synchronization is very much required when working in a collaborative environment. It ensures that your local changes are up-to-date with the remote repository and that the contributions are integrated with the work of others.

Step1: Check the Status

Before pushing, let's check which files have been modified.

git status

Step2: Add Changes to Staging

Stage all modified files.

git add .

Or, to add specific files

git add filename

Step3: Commit the Changes

Commit the changes with a meaningful message.

git commit -m "Updated files with latest changes"

Step4: Pull Latest Changes from Remote

Ensure that the local branch is up to date before pushing. Replace main with the branch name if different

git pull origin main --rebase

Step5: Push Changes to Remote

Push the updates to the remote repository.

git push origin main

Step6: Verify the Push

Check if the changes are successfully pushed

git log --oneline -n 5

Found This Page Useful? Share It!