Skip to content

Git Guide

cemsarpkaya edited this page Feb 19, 2025 · 10 revisions

🚀 Git Guide & Best Practices

📌 Introduction

Git is a powerful version control system that has become an essential tool in modern software development. Whether you're working on a personal project or collaborating with a large team, Git enables efficient code management, seamless collaboration, and a structured workflow.

What is Git?

Git is a distributed version control system that tracks changes in your codebase, allowing developers to manage project history efficiently. Unlike traditional version control systems, Git offers:

  • Full project history stored locally for easy access
  • Offline development, with the ability to sync changes later
  • Powerful branching and merging, enabling parallel development
  • Enhanced security, ensuring the integrity of every commit

Why Use Git?

Git is widely adopted because it provides:

  • Collaboration: Multiple developers can work on the same project without conflicts
  • Version Tracking: Every change is recorded, making it easy to review, compare, and revert
  • Backup & Recovery: Your code history is stored both locally and remotely
  • Branching Flexibility: Experiment with new features without disrupting the main codebase
  • Security: Cryptographic commit tracking ensures data integrity

This guide will take you through Git’s fundamental concepts, key commands, and best practices, helping you master version control for smoother and more efficient development. 🚀


📜 Git History

Git was created by Linus Torvalds in 2005 to help manage the development of the Linux kernel. Before Git, the Linux development community relied on proprietary tools, which posed issues related to licensing and collaboration. Torvalds designed Git to be fast, distributed, and highly efficient, allowing developers to track and merge changes seamlessly.


🔹 Key Events in Git History:

  • 2005: Git is created by Linus Torvalds for Linux development.
  • 2008: GitHub launches, providing a platform for open-source collaboration.
  • 2011: Git becomes widely adopted across industries beyond open-source projects.
  • 2013: Microsoft starts using Git internally for development.
  • 2018: Microsoft acquires GitHub, reinforcing Git’s dominance in software development.
  • Present: Git remains the industry standard for version control, used by developers and teams worldwide.

With Git, developers can maintain version history, collaborate on shared code, and efficiently manage software projects.


🎉 Git Fun Facts

  • 🔧 Git Was Created by Linus Torvalds
    Git was developed in 2005 by Linus Torvalds, the creator of Linux, after a dispute with BitKeeper, a previous version control system used by the Linux kernel team.

  • 🌍 "Git" Has a Humble Meaning
    Linus Torvalds humorously named Git after the British slang term "git", which means "an unpleasant person." He described it as "the stupid content tracker."

  • ⚡ The Fastest Version Control System
    Git can handle millions of lines of code efficiently. Major companies like Google, Microsoft, and Facebook use Git for large-scale projects.

  • 🚀 GitHub Wasn't Always Around
    GitHub, the most famous Git hosting platform, was founded in 2008, three years after Git was created.

  • 🕒 The Longest Commit in Git History
    A commit in the Linux kernel repository took 3.5 hours to push due to its enormous size!

  • 👀 You Can Time Travel in Git
    Using git reflog, you can restore deleted commits even if they were accidentally removed.

  • 🎭 April Fools' Day Prank in GitHub
    GitHub once played a prank where every user's contribution graph turned into a 3D perspective view on April 1st.

  • 🐙 The Five-Armed Octocat
    GitHub’s mascot is an Octocat—a cat with five octopus-like arms!

  • 📈 Over 420 Million Repositories
    The platform hosts more than 420 million Git repositories created by developers worldwide!


🛠 1️⃣ Setting Up Git

🔹 Install Git

Check if Git is installed:

git --version

If Git is not installed, download it:

  • Windows: Git for Windows
  • Mac: Install via Homebrew
    brew install git
  • Linux: Install via package manager
    sudo apt install git  # Debian-based
    sudo yum install git  # Red Hat-based

🔹 Configure Git for the First Time

Set up your username and email (important for commits):

git config --global user.name "Your Name"
git config --global user.email "[email protected]"

Verify configuration:

git config --list

📄 2️⃣ Essential Git Commands

Command Description Example
git log --oneline Displays a compact commit history git log --oneline
git stash Temporarily saves uncommitted changes git stash
git stash show Shows the changes stored in the most recent stash git stash show
git stash pop Restores the most recent stashed changes and removes them from the stash git stash pop
gcm Shortcut for git checkout main (or git checkout master, depending on the repo) gcm
gl Shortcut for git pull (alias) gl
gp Shortcut for git push (alias) gp
git checkout - Checkout the previous commit or branch git checkout -
git status Shows the status of changes in the working directory git status
git diff Displays differences between the working directory and the last commit git diff
git diff --staged Shows differences between staged files and the last commit git diff --staged
git add <file> Stages a specific file for commit git add filename.txt
git add . Stages all changes in the working directory git add .
git commit -m "message" Commits staged changes with a message git commit -m "Fix bug"
git commit --amend Modifies the last commit (changes message or adds files) git commit --amend
git clone <repo_url> Clones a repository to your local machine git clone https://github.com/user/repo.git
git branch Lists all branches in the repository git branch
git checkout <branch> Switches to a different branch git checkout feature-branch
git merge <branch> Merges a specified branch into the current branch git merge develop
git rebase <branch> Applies commits from one branch onto another git rebase main

🌿 3️⃣ Working with Branches

🔹 Creating a New Branch

git branch feature-branch

Switch to the new branch:

git checkout feature-branch

Shortcut (create + switch in one command):

git checkout -b feature-branch

🔹 Deleting a Branch

After merging, remove the branch:

git branch -d feature-branch

🔄 4️⃣ Merging & Rebasing

🔹 Merging a Branch

git checkout main
git merge feature-branch

🔹 Handling Merge Conflicts

  1. Git marks conflicts in the affected files.
  2. Manually resolve the conflicts in the code.
  3. Add the resolved files:
    git add filename
  4. Complete the merge:
    git commit -m "Resolved merge conflicts"

🔹 Rebasing a Branch

To move feature branch commits on top of main:

git checkout feature-branch
git rebase main

If conflicts occur, resolve them, then continue:

git rebase --continue

⚠ 5️⃣ Undoing Changes

🔹 Undo the Last Commit (Keep Changes)

If you made a commit but want to undo it while keeping the changes in your working directory (i.e., your files aren't lost), use the following command:

git reset --soft HEAD~1
  • HEAD~1: Refers to the previous commit (one commit before the current one).
  • This command uncommits the last commit, but keeps the changes staged for commit. It's like you didn't commit yet, but your changes are ready to be committed again.

🔹 Undo Last Commit (Discard Changes)

If you want to completely remove the last commit and discard any changes you made, use this command:

git reset --hard HEAD~1
  • --hard: This option will reset both your staging area and working directory, discarding all changes in the last commit.
  • Be cautious with this command, as any changes made in the last commit will be lost completely.

🔹 Revert a Specific Commit

If you want to undo a specific commit (not necessarily the last one), and create a new commit that undoes it, use:

git revert commit-hash
  • commit-hash: The hash of the commit you want to revert. (i.e f80e1af)
  • This command doesn't remove the commit from the history but creates a new commit that reverses the changes made in the specified commit.
  • It’s useful when you need to "undo" a commit in a way that’s visible in the commit history, preserving the integrity of your version control.

🙈 6️⃣ Excluding files while staging: .gitignore

While working locally, you might use some files in your working directory that you don't want to stage (like object files, executables, .env files with passwords in it, etc.). You can make git not see those files by putting their names into files with .gitignore extension.

Examples:

# files with .o extension (sub-directories are also affected)
*.o

# don't ignore foo.o
!foo.o

# ignore only in this directory (don't ignore in sub-directories)
# for example if there exists another bar.out in a sub-folder named b, b/bar.out will be staged but ./bar.out won't.
/bar.out

Important!: Make sure to append file names that has sensitive information (like passwords) to .gitignore, or while staging exclude them yourself.


⚡ 7️⃣ Git Best Practices

Write Meaningful Commit Messages
Example:
"Fixed login issue in user authentication"
"Update file"

Pull Before You Push
Always pull the latest changes before pushing new commits:

git pull origin main

Use Branches for Features & Fixes
Always work on a separate feature branch:

git checkout -b feature-new-ui

Write Good Pull Requests

  • Clearly explain what the changes do.
  • Attach screenshots if needed.
  • Assign reviewers before merging.

🔍 8️⃣ Additional Resources

📖 Official Git Documentation: https://git-scm.com/doc
📖 GitHub Documentation: https://docs.github.com/en
📖 Git Cheatsheet (PDF): https://education.github.com/git-cheat-sheet-education.pdf


🎯 Next Steps

1️⃣ Review this guide and apply these Git practices.
2️⃣ Each team member should add at least one additional Git topic or tip.

👥 Team Members

📌 Milestone Report

💬 Communication Plan

📋 Meeting Agendas

📅 Meeting Notes

📂 Backend Subgroup Meeting Notes

📂 Frontend Subgroup Meeting Notes

📂 Mobile Subgroup Meeting Notes

📚 Lecture Notes

🛠️ Team Best Practices

✍️ Guidance

❗ Issues

🚀 Project

🧱 Diagrams

👩‍💼 User Scenarios

Click to Expand ⬇️

🗂️ Templates

Clone this wiki locally