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
# Initialize repo with main branch
# OR for older versions:
mkdir NewProjectcd NewProject# Initialize repo with main branch
git init --initial-branch=main# OR for older versions:
git initgit checkout -b main
Get help: git --help or git commit -help
Push Local Repository to GitHub
- Create a Repository on GitHub.
- Copy the HTTPS link.
# Add remote origin
# Push main branch and set upstream
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 statusgit status --short
# Stage all files
git add . or git add -A
# Unstage changes
git reset
Git Commit
# Standard commit
# View history
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 branches | git branch |
| Create & Switch | git checkout -b new_branch |
| Switch branch | git switch branch_name |
| Delete branch | git branch -d branch_name |
| Rename branch | git branch -m old new |
Merging
# 1. Switch to target (main)
# 2. Merge source branch
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
# 2. Commit
# 3. Push
git add .# 2. Commit
git commit -m "Message"# 3. Push
git push origin main
Git Restore & Clean
Restoring Files
# Discard changes in working dir
# Unstage file (keep changes)
# DANGEROUS: Reset to last commit
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)
# Force delete untracked
# Include directories
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 stagedgit diff --staged- Staged but not committedgit diff > patch.patch- Create patch filegit apply patch.patch- Apply patch file
Logging
git log- Full historygit log --oneline- Compact historygit log --follow filename- History of specific filegit 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 stashgit stash listgit stash pop (Apply & Remove)git stash apply (Keep in stack)git stash dropgit stash clearUpdate Forked Branch
# 1. Add upstream remote
# 2. Fetch changes
# 3. Merge into local
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 --amendgit 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