git

git tutorial from scratch to expert:

🚀 What is Git?

Git is a version control system that developers use to track changes in code and collaborate with others on software projects.

getting started: installation

  • Windows/Mac/Linus:
Download Git
  • Verify Installation:
  • git --version

🧾 basic concept

  • Repository (Repo): Directory tracked by Git
  • Commit: A snapshot of your code
  • Branch: Independent line of development
  • Clone: Copy of a repository from remote to local

🔶 Step-by-Step Basic Usage

1️⃣ Initialize Repository

git init

2️⃣ Add Files

git add file_name

3️⃣ Commit Changes

Changes git commit -m "initial commit"

4️⃣ Check Status

git status

5️⃣ View Commit History

git log

6️⃣ Set Git Identity

  • git config --global user.name "Your Name"
  • git config --global user.email "your.email@example.com
  • 🌍 Working with Remote Repositories (GitHub, GitLab)

    Clone Remote Repository

    git clone repository_URL

    Push Changes

    git push origin branch_name

    Pull Updates

    git pull origin branch_name

    🌳 Branching and Merging

    Create New Branch

    git branch branch_name

    Switch Branches

    git checkout branch_name

    Create and Switch

    git checkout -b branch_name

    Merge Branches

    git merge branch_name

    Delete Branch

    git branch -d branch_name

    ✂️ Resolving Conflicts

    • If merging causes conflicts, Git will indicate it.
    • Resolve manually, then:

    git add resolved_file

    git commit -m "Resolved conflict"

    🔁 Undoing Changes

  • Undo last commit (keep changes):
  • git reset --soft HEAD~1

  • Discard unstaged changes:
  • git checkout -- file_name

    🙎‍♂️ Advanced Git Usage

    1️⃣ Stashing Changes

    git stash
    git stash pop

    2️⃣ Rebase

    Rewrite commit history:

    git checkout feature_branch
    git rebase main

    3️⃣ Cherry-Pick

    Apply a specific commit from one branch to another:

    git cherry-pick commit_hash

    4️⃣ Tagging

    Create tags to mark versions:

    git tag -a v1.0 -m "version 1.0 release"
    git push --tags

    5️⃣ Amend Commit

    Modify last commit message or content:

    git commit --amend -m "New commit message"

    🧹 Cleaning Repository

  • Remove untracked files:
  • git clean -f

  • Remove untracked directories too:
  • git clean -fd

    📚 Best Practices

    • Write meaningful commit messages.
    • Commit often.
    • Keep branches small and focused.
    • Regularly sync branches with main.

    🔥 Git Expert Commands & Workflow

    1️⃣ Interactive Rebase

    Squash commits, reorder, or edit history:

    git rebase -i HEAD~3

    2️⃣ Submodules

    Include external repositories within your repo:

    git submodule add repo_url
    git submodule update --init --recursive

    3️⃣ Bisect (Finding Bugs)

    Identify the commit causing a bug:

    git bisect start
    git bisect bad
    git bisect good good_commit_hash

    4️⃣ Aliases

    Create shortcuts for commands:

    git config --global alias.st "status"

    5️⃣ Hooks

    Run scripts automatically at specific points:

  • pre-commit
  • commit-msg
  • pre-push
  • (Create custom scripts in .git/hooks/ directory.)