> For the complete documentation index, see [llms.txt](https://cumbucadev.gitbook.io/github-essentials/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cumbucadev.gitbook.io/github-essentials/dia-4/desfazendo-alteracoes-localmente/exemplos-praticos.md).

# Exemplos Práticos

* [Antes do Commit](#antes-do-commit)
  * [Exemplo 1. Desfazer Alterações Não Adicionadas à Área de Staging](#exemplo-1.-desfazer-alteracoes-nao-adicionadas-a-area-de-staging)
  * [Exemplo 2. Remover Arquivos da Área de Staging](#exemplo-2.-remover-arquivos-da-area-de-staging)
* [Desfazendo Commits](#desfazendo-commits)
  * [Exemplo 3. Reverter um Commit Específico](#exemplo-3.-reverter-um-commit-especifico)
* [Alterando o Último Commit](#alterando-o-ultimo-commit)
  * [Exemplo 4. Modificar o Último Commit](#exemplo-4.-modificar-o-ultimo-commit)

## **Antes do Commit**

### **Exemplo 1. Desfazer Alterações Não Adicionadas à Área de Staging**

#### **Passo 1: Criar ou modificar um arquivo**

🔹 **Linux/macOS**

```bash
echo "Alteração" > arquivo.txt
```

🔹 **Windows (CMD/PowerShell)**

```bash
echo Alteração > arquivo.txt
```

#### **Passo 2: Verificar o estado atual do repositório**

Verifique os arquivos modificados que ainda não foram adicionados à área de staging.

```bash
git status
```

Saída esperada:

```sh
Modifications 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:   arquivo.txt
```

#### **Passo 3: Desfazer as alterações no arquivo**

Para restaurar o arquivo para seu estado original, utilize o comando:

```bash
git restore arquivo.txt
```

#### **Passo 4: Verificar novamente o estado do repositório**

Após desfazer as alterações, verifique novamente o estado do repositório.

```bash
git status
```

Saída esperada:

```sh
Nothing to commit, working tree clean
```

### **Exemplo 2. Remover Arquivos da Área de Staging**

#### **Passo 1: Criar ou modificar um arquivo**

🔹 **Linux/macOS**

```bash
echo "Alteração" > arquivo.txt
```

🔹 **Windows (CMD/PowerShell)**

```bash
echo Alteração > arquivo.txt
```

#### **Passo 2: Adicionar o arquivo à área de staging**

Adicione o arquivo à área de staging para prepará-lo para o commit.

```bash
git add arquivo.txt
```

#### **Passo 3: Verificar o estado da área de staging**

Verifique os arquivos que foram adicionados à área de staging.

```bash
git status
```

Saída esperada:

```sh
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
  new file:   arquivo.txt
```

#### **Passo 4: Remover o arquivo da área de staging**

Remova o arquivo da área de staging

```bash
git restore --staged arquivo.txt
```

#### **Passo 5: Verificar novamente o estado da área de staging**

Após remover o arquivo da área de staging, verifique o estado novamente.

```bash
git status
```

Saída esperada:

```sh
Modifications 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:   arquivo.txt
```

## Desfazendo Commits

### Exemplo 3. Reverter um Commit Específico

#### Passo 1: Criar um repositório de exemplo (se ainda não tiver um)

```sh
git init meu-repo
cd meu-repo
```

#### Passo 2: Fazer o commit de um arquivo novo

🔹 **Linux/macOS**

```sh
echo "Primeira versão" > arquivo.txt
git add arquivo.txt
git commit -m "Adicionando arquivo inicial"
```

🔹 **Windows (CMD/PowerShell)**

```sh
echo Primeira versão > arquivo.txt
git add arquivo.txt
git commit -m "Adicionando arquivo inicial"
```

#### Passo 3: Fazer mais três commits adicionando linhas ao mesmo arquivo

Adicione mais uma linha ao arquivo e faça o commit. Faça isso um total de três vez.

🔹 **Linux/macOS**

```sh
echo "Alteração 1" >> arquivo.txt
git add arquivo.txt
git commit -m "Alteração 1 no arquivo"

echo "Alteração 2" >> arquivo.txt
git add arquivo.txt
git commit -m "Alteração 2 no arquivo"

echo "Alteração 3" >> arquivo.txt
git add arquivo.txt
git commit -m "Alteração 3 no arquivo"
```

🔹 **Windows (CMD/PowerShell)**

```sh
echo Alteração 1 >> arquivo.txt
git add arquivo.txt
git commit -m "Alteração 1 no arquivo"

echo Alteração 2 >> arquivo.txt
git add arquivo.txt
git commit -m "Alteração 2 no arquivo"

echo Alteração 3 >> arquivo.txt
git add arquivo.txt
git commit -m "Alteração 3 no arquivo"
```

#### Passo 4: Listar o histórico de commits

```sh
git log --oneline
```

Saída esperada:

```sh
a1b2c3 Alteração 3 no arquivo
d4e5f6 Alteração 2 no arquivo
g7h8i9 Alteração 1 no arquivo
j0k1l2 Adicionando arquivo inicial
```

#### Passo 5: Reverter um commit específico

Suponha que queremos desfazer "Alteração 2 no arquivo". Pegue o hash correspondente e execute o comando:

```sh
git revert d4e5f6
```

O Git abrirá um editor para confirmar a mensagem do commit de reversão. Salve e saia do editor.

#### Passo 6: Verificar o status do repositório

```sh
git status
```

Saída esperada:

```sh
No changes to commit, working tree clean
```

#### Passo 7: Verificar o histórico novamente

```sh
git log --oneline
```

Saída esperada:

```sh
x1y2z3 Revert "Alteração 2 no arquivo"
a1b2c3 Alteração 3 no arquivo
d4e5f6 Alteração 2 no arquivo
g7h8i9 Alteração 1 no arquivo
j0k1l2 Adicionando arquivo inicial
```

#### Passo 8: Verificar o conteúdo do arquivo

🔹 Linux/macOS

```sh
cat arquivo.txt
```

🔹 **Windows (CMD/PowerShell)**

```powershell
type arquivo.txt
```

Saída esperada:

```sh
Primeira versão
Alteração 1
Alteração 3
```

## Alterando o Último Commit

### Exemplo 4. Modificar o Último Commit

#### Passo 1: Criar um repositório e entrar nele

```sh
git init mais-um-repo
cd mais-um-repo
```

#### Passo 2: Fazer o commit de um arquivo novo

🔹 **Linux/macOS**

```sh
echo "Meu texto maneiro" > texto.txt
git add texto.txt
git commit -m "Adicionando texto.txt"
```

🔹 **Windows (CMD/PowerShell)**

```sh
echo Meu texto maneiro > texto.txt
git add texto.txt
git commit -m "Adicionando texto.txt"
```

#### **Passo 3:** Verificar o histórico de commits

```sh
git log
```

```sh
commit 4adbb23d9c7cbdc548e0cf87fd3385c4c3b83f86 (HEAD -> main)
Author: Cumbuca Dev <cumbucadev@gmail.com>
Date:   Fri Feb 21 09:29:00 2025 -0300

    Adicionando texto.txt
```

#### **Passo 4: Criar outro arquivo e esquecê-lo no commit**

Agora, criamos outro arquivo, mas esquecemos de adicioná-lo no commit anterior.

🔹 **Linux/macOS**

```sh
echo "Conteúdo importante" > esquecido.txt
```

🔹 **Windows (CMD/PowerShell)**

```sh
echo Conteúdo importante > esquecido.txt
```

#### **Passo 4: Adicionar o novo arquivo e modificar o último commit**

Agora, adicionamos o novo arquivo e modificamos o último commit para incluí-lo.

```sh
git add esquecido.txt
git commit --amend -m "Adicionando texto.txt e arquivo esquecido"
```

Saída esperada (algo como):

```sh
[main 15b10cf] Adicionando texto.txt e arquivo esquecido
 Date: Fri Feb 21 09:29:00 2025 -0300
 2 files changed, 2 insertions(+)
 create mode 100644 esquecido.txt
 create mode 100644 texto.txt
```

**Passo 5: Verificar o histórico de commits**

Para confirmar que a mensagem foi alterada e o novo arquivo foi incluído no commit, execute:

```sh
git log
```

Saída esperada (algo como):

```sh
commit 15b10cf054dbe24a8fc00422d82304b2528b3acb (HEAD -> main)
Author: Cumbuca Dev <cumbucadev@gmail.com>
Date:   Fri Feb 21 09:29:00 2025 -0300

    Adicionando texto.txt e arquivo esquecido
```

#### **Passo 6: Verificar os arquivos no commit**

Podemos verificar quais arquivos estão incluídos no commit alterado com o seguinte comando:

```sh
git show --name-only
```

Saída esperada:

```sh
ccommit 15b10cf054dbe24a8fc00422d82304b2528b3acb (HEAD -> main)
Author: Cumbuca Dev <cumbucadev@gmail.com>
Date:   Fri Feb 21 09:29:00 2025 -0300

    Adicionando texto.txt e arquivo esquecido

esquecido.txt
texto.txt
```

Agora o commit foi modificado para incluir o arquivo esquecido e tem uma mensagem atualizada.


---

# 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, and the optional `goal` query parameter:

```
GET https://cumbucadev.gitbook.io/github-essentials/dia-4/desfazendo-alteracoes-localmente/exemplos-praticos.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
