> For the complete documentation index, see [llms.txt](https://42-guide.gitbook.io/42-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://42-guide.gitbook.io/42-guide/piscine-life/shell00/git-commit.md).

# GiT commit

**Objective:**

The goal of this exercise is to create a shell script that displays the IDs of the last 5 commits in your Git repository. This exercise will help you understand how to interact with Git from the command line and extract specific information from the Git log.

**Instructions:**

1. **Create a Shell Script Named `git_commit.sh`:**
   * In the `ex05/` directory, create a file named `git_commit.sh`.
   * This script should output the commit IDs (hashes) of the last 5 commits in your Git repository.
2. **Format the Output:**
   * The output should list the commit IDs, one per line.
   * Example output:

     ```bash
     baa23b54f0adb7bf42f2628ddd0a6ed4f587e114
     2f52d74b138f7a80eea844969e8dc5483b531ac1
     905f53499656771334f53f59bb984fc297747011
     5ddc84744f415b3fcb72d08fcb333e19c3a27076
     e94d0b448c03ec633f16d84d63beaeff9ae7e7be
     ```

**Detailed Steps:**

1. **Create the `git_commit.sh` File:**
   * Open your terminal and navigate to the `ex05/` directory.
   * Create a file named `git_commit.sh`:

     ```bash
     touch git_commit.sh
     ```
2. **Write the Script:**
   * Open the `git_commit.sh` file in a text editor and add the following script:

     ```bash
     #!/bin/sh
     git log -5 --format='%H'
     ```
   * This script does the following:
     * `git log -5`: Retrieves the last 5 commits from the Git log.
     * `--format='%H'`: Specifies that only the commit IDs (hashes) should be displayed.
3. **Make the File Executable:**
   * After saving the file, make it executable by running:

     ```bash
     chmod +x git_commit.sh
     ```
4. **Test the Script:**
   * Run the script to see if it produces the expected output:

     ```bash
     ./git_commit.sh
     ```

**What You Will Learn:**

* **Git Log Command:** You will learn how to use the `git log` command to retrieve commit history.
* **Formatting Git Output:** You will understand how to format the output of Git commands to extract specific information (e.g., commit IDs).
* **Shell Scripting:** You will gain experience in writing simple shell scripts to automate tasks.

**Useful Resources:**

* [**Git Log Documentation**](https://git-scm.com/docs/git-log)**:** Official documentation for the `git log` command.
* [**Git Commit IDs**](https://git-scm.com/book/en/v2/Git-Basics-Viewing-the-Commit-History)**:** A guide to understanding Git commit IDs and viewing commit history.
* [**Shell Scripting Basics**](https://www.shellscript.sh/)**:** A beginner's guide to shell scripting.
* [**Git from the Command Line**](https://www.atlassian.com/git/tutorials/git-bash)**:** A tutorial on using Git from the command line.

**Conclusion:**

This exercise helps you understand how to interact with Git from the command line and extract specific information from the Git log. By completing this exercise, you will gain practical experience in using Git commands and writing shell scripts, which are essential skills for any developer working with version control systems.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://42-guide.gitbook.io/42-guide/piscine-life/shell00/git-commit.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.
