Skip to content

Welcome to the Git Essentials tutorial! Here, you'll learn how to start using Git effectively. This guide references multiple platforms and books, providing step-by-step instructions to master Git. Ready to dive in? Let's get started! πŸš‚

Notifications You must be signed in to change notification settings

adhikareeprayush/Git-Essentials

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

20 Commits
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ Git Essentials πŸš€

Welcome to the Git Essentials tutorial! Here, you'll learn how to start using Git effectively. This guide references multiple platforms and books, providing step-by-step instructions to master Git. Ready to dive in? Let's get started! πŸš‚

πŸ“š Table of Contents

  1. Why Use Git?
  2. How Does Git Work?
  3. Install and Configure Git
  4. Push Your Code with Git
  5. Make Changes to Files
  6. Important Concepts in Git

πŸ€” Why Use Git?

Version Control

One of the main reasons to use Git is for version control. It helps you track changes in your code. Imagine having multiple versions of your project stored locally. It can get messy and stressful! πŸ˜–

Using Git simplifies this process. You can change the code and create separate versions as commits, stored locally or remotely. This way, you can access different versions of your project easily. Let's visualize this! 🀯

Version Control

Can Git be used only locally? What if my hard disk is damaged or I lose my computer?

For these situations, you can use a Git Provider to store your projects both locally and on the cloud.

To Share Code

Working with a team? Git makes it easy to sync changes among multiple collaborators. 🌍

Sharing Code

Using a Git provider, you can share code with your team effortlessly.

To Collaborate

Git is great for collaboration. Multiple people can work on the same project by being added as collaborators. Git providers establish remote connections for easy code sharing. Git can even merge different codes automatically, though sometimes you'll need to do it manually. 🀝

Open Source

Open Source means the code is publicly available. You can download, modify, create your version, and publish it. A community often works on open source projects, detecting bugs, fixing them, and improving the code. Git features like branches make open source contributions easy.

βš™οΈ How Does Git Work?

Let's explore the technical side of Git. We'll learn how to use Git locally, use Git providers, and understand distributed version control.

Using Git Locally

When using Git locally, there are three stages:

  1. Working Directory: Where you make changes.
  2. Staging Area: Use git add <filename> to stage changes.
  3. Local Repository: Use git commit to save changes.

Git Stages

Using a Git Provider

With a Git provider, use git push to upload changes to a remote repository and git pull to download changes.

Distributed Version Control

In distributed version control, every client has a complete copy of the repository, including all files, snapshots, and history. If something happens to the central repository, each client still has a full copy.

πŸ› οΈ Install and Configure Git

Downloading

Start by installing Git for your OS from Git Downloads.

Installation

Installing Git is straightforward. Follow the prompts and install it.

Configuration

Configure Git using the gitconfig file. Important config files include:

  • Global: ~/.gitconfig or C:\Users\[user]\.gitconfig
  • Local: .git/config

Local settings can overwrite global settings.

Showing the Configuration

Type git config --list in the terminal to view the configuration.

Setting up global configurations:

  • Username: git config --global user.name "Your_name"
  • Email: git config --global user.email "name@example.com"

πŸ“€ Push Your Code with Git

Learn to create a new repository and work with it. We'll use GitHub as our Git provider.

Setting Up a Remote Repo

  1. Sign up and visit GitHub.
  2. Name your repository, choose visibility, add a README, and get started.

Cloning the Remote Repository

  1. Open the GitHub repo.
  2. Click on the green "Code" dropdown and copy the HTTPS URL.
  3. In your terminal, type:
    git clone <HTTPS URL>
    cd <repo-name>
    code .

Creating a File and Staging It

  1. Create and save a file.
  2. Stage the file:
    git add <filename>
    git status
    git commit -m "first commit"
    git status

Pushing File to Remote Repo

  1. Push the changes:
    git push
  2. Check the GitHub repo to confirm the changes.

The .git Folder

The .git folder is the heart of your Git repository. It contains all the metadata and object data Git needs to manage the version history.

Structure of the .git Folder:

  • config: Configuration settings.
  • HEAD: Points to the current branch reference.
  • index: Stores staging area information.
  • logs/: Contains logs of reference updates.
  • objects/: Stores all objects (blobs, trees, commits).
  • refs/: Contains references to commit objects.
  • hooks/: Contains scripts triggered by Git actions.

πŸ”§ Make Changes to Files

Introduction to git status

The git status command provides an overview of your working directory and staging area. It helps track changes and understand your repository's state.

Typical Output:

On branch main
Your branch is up to date with 'origin/main'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    modified:   file1.txt
    new file:   file2.txt

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   file3.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)
    file4.txt

Introduction to git diff

The git diff command shows the differences between various states of your repository.

Basic Usage:

  • Compare working directory to staging area: git diff
  • Compare staging area to last commit: git diff --cached
  • Compare specific file: git diff <file>
  • Compare two commits: git diff <commit1> <commit2>

Example Output:

diff --git a/file1.txt b/file1.txt
index d3ff2c6..f7c3f58 100644
--- a/file1.txt
+++ b/file1.txt
@@ -1,3 +1,4 @@
 Line 1
 Line 2
+New line 3
 Line 4

Introduction to git log

The git log command displays commit history, including commit hash, author, date, and message.

Basic Usage:

  • View commit details: git show <commit-hash>
  • Search for specific commits: git log --grep='your_commit'

Renaming a File

Rename a file with Git using:

git mv current_name new_name

Working with Folders

Git doesn't track empty folders by default. To track an empty folder, create a .gitkeep file inside it.

Undoing Changes

Undo staged changes:

  1. Stage a file: git add example.txt
  2. Check status: git status
  3. Unstage the file: git restore --staged example.txt
  4. Verify: git status

Undo changes to a file:

  1. Modify a file: example.txt
  2. Check status: git status
  3. Revert changes: git restore example.txt

Looking Back in History

Use git log to view commit history. To go back to a specific commit:

git checkout <commit-hash>

To return to the latest version:

git checkout main

Reverting to an Old Change

Revert to a previous commit:

git log --oneline
git revert <commit-id>

πŸ“Œ Important Concepts in Git

Ignoring Files

Use a .gitignore file to exclude specific files or folders from being tracked by Git.

Example .gitignore entry:

example.md

Git Branches

Branches allow you to work on different features or fixes in isolation.

Key Commands:

  • Create a branch: git branch <branch-name>
  • Switch to a branch: git switch <branch-name>
  • Create and switch to a new branch: git switch -c <branch-name>
  • List all branches: git branch
  • Merge a branch: git merge <branch-name>
  • Delete a branch: git branch -d <branch-name>

Good Commit Messages

Good commit messages should:

  • Describe changes precisely
  • Use present tense
  • Be concise (max 50 characters)
  • Include additional details if necessary

Merge Conflicts

Merge conflicts occur when changes from different branches conflict. Carefully review and resolve conflicts to ensure a smooth merge.

Steps to Reproduce Merge Conflict

Merge Conflict

  1. Create a Project and Initialize Git:

    • Create a new project directory.

    • Add an index.html file to the project.

    • Initialize Git in the project directory and add the file to the local repository:

      git init
      git add index.html
      git commit -m "Initial commit with index.html"
  2. Create a New Branch from master (or any current branch c1):

    • Create a new branch named child_branch:

      git branch child_branch
  3. Switch to child_branch:

    • Checkout the child_branch:

      git checkout child_branch
  4. Make Changes in child_branch and Commit:

    • Make any changes to the index.html file in the child_branch.

    • Commit the changes:

      git add index.html
      git commit -m "Changes made to index.html in child_branch"

      Child Brach

  5. Make Changes in master Branch and Commit:

    • Switch back to the master branch:

      git checkout master
    • Make any changes to the index.html file in the master branch.

    • Commit the changes:

      git add index.html
      git commit -m "Changes made to index.html in master branch"

      Master Brach

  6. Merge child_branch into master Branch:

    • While in the master branch, merge the child_branch:

      git merge child_branch
      # This will result in a merge conflict if both branches have changes in index.html
    • You will get merge conflict. Master Brach

Resolving the Merge Conflict

When you try to merge child_branch back into the master branch, a merge conflict will occur because both branches have made changes to the same file, index.html.

  1. Identify the Conflict:

    • Open the conflicting file (index.html) in a text editor. You will see conflict markers like these:

      <<<<<<< HEAD
      Changes made in master branch
      =======
      Changes made in child_branch
      >>>>>>> child_branch
  2. Resolve the Conflict:

    • Edit the file to resolve the conflict by deciding which changes to keep or by combining the changes:

      <!-- Resolved content -->
      Changes made in master branch
      Changes made in child_branch
  3. Mark the Conflict as Resolved:

    • After resolving the conflict, mark the file as resolved:

      git add index.html
  4. Complete the Merge:

    • Commit the merge to complete the process:

      git commit -m "Resolved merge conflict between master and child_branch"

You have successfully resolved the merge conflict and merged child_branch into master!

Congratulations! You've reached the end of the Git Essentials

About

Welcome to the Git Essentials tutorial! Here, you'll learn how to start using Git effectively. This guide references multiple platforms and books, providing step-by-step instructions to master Git. Ready to dive in? Let's get started! πŸš‚

Topics

Resources

Stars

Watchers

Forks