Git Notes

Git Overview

  • Git is a version control system.
  • Git helps us to keep track of code changes.
  • Git is used to collaborate on code.
  • Git help us to Manage projects with Repositories

Version Control System (VCS)

  • Git allows multiple developers to work on a project simultaneously without interfering with each other.
  • It keeps track of changes made to the source code over time, making it easier to collaborate and manage different versions of a project.

What is GitHub?

  • Git is not the same as GitHub.
  • GitHub is a web-based platform that provides hosting for software development and version control using Git.
  • GitHub makes tools that use Git.
  • GitHub offers a range of features that enhance the development workflow.
  • Key features: Issues, Discussions, Pull requests, Notifications, Labels, Actions, Forks, Projects.

Configure Git

Download Git: https://www.git-scm.com/

Check version:

git --version

Set global variables (Required):

git config --global user.name "<USER_NAME>"
git config --global user.email "<USER_EMAIL>"

Check configuration:

git config --list

Set up Git Repository

# Create project directory
mkdir NewProject
cd NewProject

# Initialize repo with main branch
git init --initial-branch=main
# OR for older versions:
git init
git checkout -b main

Get help: git --help or git commit -help

Push Local Repository to GitHub

  1. Create a Repository on GitHub.
  2. Copy the HTTPS link.
# Add remote origin
git remote add origin https://github.com/im-Rajat/Git.git

# Push main branch and set upstream
git push --set-upstream origin main
Tip: -u is shorthand for --set-upstream.

Git Staging Environment

Files can be Tracked (added to repo) or Untracked.

# Stage specific file
git add filename
# Check status
git status
git status --short
# Stage all files
git add . or git add -A
# Unstage changes
git reset

Git Commit

# Standard commit
git commit -m "Your Message"

# View history
git log

Commit without Staging

Automatically stages tracked files and commits them.

git commit -a -m "Message"

Git Branch

Branches allow you to work on different parts of a project without impacting the main branch.

Action Command
List branchesgit branch
Create & Switchgit checkout -b new_branch
Switch branchgit switch branch_name
Delete branchgit branch -d branch_name
Rename branchgit branch -m old new

Merging

# 1. Switch to target (main)
git checkout main

# 2. Merge source branch
git merge source_branch

Git Pull

PULL
=
FETCHDownload history
+
MERGECombine changes
git pull origin main

Git Push

# 1. Stage
git add .
# 2. Commit
git commit -m "Message"
# 3. Push
git push origin main

Git Restore & Clean

Restoring Files

# Discard changes in working dir
git restore .

# Unstage file (keep changes)
git reset filename

# DANGEROUS: Reset to last commit
git reset --hard

Cleaning Untracked Files

# Dry run (See what deletes)
git clean -n

# Force delete untracked
git clean -f

# Include directories
git clean -fd

Deleting & Renaming

Delete file & stage deletion: git rm filename.txt
Rename file & stage change: git mv old_name.txt new_name.txt

Diff & Patch

  • git diff - Changed but not staged
  • git diff --staged - Staged but not committed
  • git diff > patch.patch - Create patch file
  • git apply patch.patch - Apply patch file

Logging

  • git log - Full history
  • git log --oneline - Compact history
  • git log --follow filename - History of specific file
  • git show <SHA> - Details of specific commit

Use .gitignore

Use this file to ignore sensitive info, personal notes, or system files.

# Remove files from cache (stop tracking) without deleting from disk
git rm --cached filename

Git Stash

Temporarily store changes not ready for commit (clean working dir).

git stash
git stash list
git stash pop (Apply & Remove)
git stash apply (Keep in stack)
git stash drop
git stash clear

Update Forked Branch

# 1. Add upstream remote
git remote add upstream <original_repo_url>

# 2. Fetch changes
git fetch upstream

# 3. Merge into local
git merge upstream/branch_name

Changing History

Modify previous commit (add staged changes to last commit): git commit --amend
git commit --amend --no-edit (Keep same message)
Interactive Rebase: git rebase -i HEAD~2

GitHub Features

Why?

  • Cloud Repository
  • Collaborative Development
  • Project Management

Management

  • Contributors & Issues
  • Labels & Milestones
  • Projects (Kanban)

Syncing

  • Clone: Copy repo to local
  • Fetch: Download info (no merge)
  • Pull: Fetch + Merge

References