# GIT Essentials

<figure><img src="/files/bnHbn5XvUyQBZAaEBDja" alt=""><figcaption></figcaption></figure>

This tutorial covers the definition of Git, its basic commands and concepts to help you get started with version control. As you become more familiar with Git, you can explore more advanced features and workflows.

For detailed documentation and specific Git commands, you can use the `git --help` command or refer to the official Git documentation: [Git Documentation](https://git-scm.com/doc).

## What is Git?

**Git** is a distributed version control system that is widely used in software development to manage and track changes in code and project files. It was created by [Linus Torvalds](https://en.wikipedia.org/wiki/Linus_Torvalds) in 2005 and is known for its flexibility, speed, and robust branching and merging capabilities. Git is designed to help multiple collaborators work on the same project, enabling them to make changes independently and then integrate those changes seamlessly.

Git stores the entire project history as a series of snapshots or commits. Each commit represents a specific state of the project at a particular point in time. Developers can work on different branches, allowing for parallel development of features and bug fixes. Once work on a branch is complete, changes can be merged into the main project branch.

## Using Git

Let's walk through a simple example to illustrate how Git works in a real-world scenario. We'll create a Git repository, make changes, and commit those changes.

**Step 1: Initialize a Git Repository**

1. Create a new directory for your project and navigate to it in your terminal.
2. Initialize a Git repository in the directory:

   ```bash
   git init
   ```

**Step 2: Create and Edit Files**

1. Create a new file (e.g., `about.html`) in your project directory:

   ```bash
   touch about.html
   ```
2. Edit the file using a text editor or code editor to add some content. Save the file. (optional)

**Step 3: Stage and Commit Changes**

1. Check the status of your Git repository to see the untracked changes:

   ```bash
   git status
   ```
2. Stage the changes for commit:

   ```bash
   git add about.html
   ```
3. Commit the changes with a meaningful message:

   ```bash
   git commit -m "Added about.html with initial content"
   ```

**Step 4: Viewing Commit History**

You can view the commit history of your project:

```bash
git log
```

The `git log` command displays a list of commits, including commit messages, author information, and commit timestamps.

**Step 5: Create a New Branch**

Suppose you want to work on a new feature. Create a new branch for this feature:

```bash
git checkout -b feature-branch
```

**Step 6: Make Changes and Commit**

Edit the about`.html` file to make changes. Save the file and commit the changes to the feature branch:

```bash
git add about.html 
git commit -m "Added a new feature"
```

**Step 7: Merge Branches**

After completing the feature, you can merge the feature branch into the main branch (usually named "main" or "master"):

```bash
git checkout main 
git merge feature-branch
```

**Step 8: Push to a Remote Repository**

To collaborate with others, you can push your repository to a remote Git hosting service like GitHub:

1. Create a repository on GitHub.
2. Add the remote repository to your local Git repository:

   ```bash
   git remote add origin <remote-repo-url>
   ```
3. Push your local repository to the remote:

   ```bash
   git push -u origin main
   ```

{% hint style="info" %}
Set default upstream branch for a local branch if you cannot push your changes:

```bash
git push --set-upstream <reponame> main
```

{% endhint %}

This example demonstrates how Git can be used for tracking changes, branching, committing, and collaborating with others. Git's flexibility and powerful version control capabilities make it an essential tool for software development and collaborative projects.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dshub.gitbook.io/ds-hub/git/git-essentials.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
