Git

🚀 Git Tutorial from Start — With Real-Time Examples

📘 Table of Contents


📌 What is Git?

  • Git is a distributed version control system.
  • Tracks changes in source code during software development.
  • Helps teams collaborate on code.

🧰 Installing Git

For Linux:

sudo apt update
sudo apt install git

For macOS:

brew install git

For Windows:


⚙️ Git Configuration

git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --list  # verify settings

🔄 Basic Git Workflow

git init                     # Initialize a Git repository
git status                   # Check current status
git add file.txt             # Stage a file
git commit -m "Added file"   # Commit with message

🧬 Cloning a Repository

git clone https://github.com/yourusername/project.git

✍️ Making Changes & Committing

echo "Hello Git" > hello.txt
git add hello.txt
git commit -m "Added hello.txt file"

🌿 Branching and Merging

git branch                  # List branches
git branch feature-x        # Create a new branch
git checkout feature-x      # Switch to branch
# Make changes and commit
git checkout main
git merge feature-x         # Merge branch into main

🔄 Real-Time Example: Feature Branch Workflow

Scenario: You are adding a login feature.

git checkout -b feature/login
# Add login logic to login.js
git add login.js
git commit -m "Add login feature"
git push origin feature/login
# Open a Merge Request (MR) on GitLab/GitHub

After MR approval:

git checkout main
git pull origin main
git merge feature/login
git push origin main

📦 Stashing Changes

git stash           # Save uncommitted changes
git pull origin main
git stash pop       # Apply stashed changes

🌐 Working with Remotes

git remote -v                          # Show configured remotes
git remote add origin <repo-url>      # Add remote repository
git push -u origin main               # Push code to remote
git pull origin main                  # Pull latest changes

📜 Git Log and History

git log                                # View commit logs
git log --oneline --graph --all        # Pretty graph format

🔙 Undoing Things

git checkout -- file.txt       # Discard uncommitted changes
git reset HEAD~1               # Undo last commit (keep changes)
git revert <commit-id>         # Revert a specific commit

🚫 .gitignore File

Create a .gitignore file in the project root to ignore files/folders.

# .gitignore example
*.log
node_modules/
.env
.idea/
.DS_Store

🏷️ Git Tags

git tag v1.0                         # Lightweight tag
git tag -a v1.1 -m "Release v1.1"    # Annotated tag
git push origin --tags               # Push tags to remote

✅ Conclusion

  • Git is essential for modern development workflows.
  • Use branches and MRs for cleaner collaboration.
  • Practice using add, commit, push, and pull regularly.
  • Use git status and git log to understand project state.

🙌 Happy Coding with Git! Keep committing and stay versioned!