# Git

## Git

### Estados de archivos <a href="#git-configuracion" id="git-configuracion"></a>

* Modificado (modified).
* Preparado (staged).
* Confirmado (committed).

### Configuración <a href="#git-configuracion" id="git-configuracion"></a>

Obtener configuración del repositorio.

```shell
git config --list
```

Configuración de usuario.

```shell
# Globalmente
git config --global user.name "<firstname-lastname>"
git config --global --get user.name

# Repositorio específico
cd <repository-folder>
git config user.name "<firstname-lastname>"
git config --get user.name
```

Configuración de correo electrónico.

```shell
# Globalmente
git config --global user.email "<email>"
git config --global --get user.email

# Repositorio específico
cd <repository-folder>
git config user.email "<email>"
git config --get user.email
```

Establecer color automático de la línea de comandos para Git (facilita la revisión).

```shell
git config --global color.ui auto
```

### Repositorios <a href="#git-repositorios" id="git-repositorios"></a>

Inicializar un directorio existente como un repositorio de Git.

```shell
git init
```

Clonar repositorio a través de una URL.

```shell
git clone <url>
```

### Modificaciones locales <a href="#git-modificaciones-locales" id="git-modificaciones-locales"></a>

Visualizar cambios realizados localmente.

```shell
git status
```

Agregar archivos modificados localmente.

```shell
# Agregar archivo
git add <file>
# Agregar todos los archivos modificados
git add .
```

Confirmar cambios realizados localmente.

```shell
git commit -m "<descriptive-message>"
```

Listar confirmaciones (commits) realizadas.

```sh
git log --pretty=oneline
git log --all --graph --pretty=short -n 5
git log --stat
```

### Modificaciones remotas <a href="#git-modificaciones-remotas" id="git-modificaciones-remotas"></a>

Obtener repositorios remotos.

```sh
git remote -v
```

Identificar diferencias entre los repositorios local y remoto.

```sh
git fetch
git diff main origin/main
```

Fusionar repositorio local con los cambios del repositorio remoto.

```sh
git merge
```

Enviar cambios realizados localmente a repositorio remoto.

```shell
git push
```

Traer cambios de un repositorio remoto a repositorio local.

```shell
git pull
```

### Ramas (branches) locales

Visualizar ramas locales.

```sh
git branch
```

Crear una rama local.

```sh
git branch <branch-name>
```

Cambiar de rama localmente.

```sh
git checkout <branch-name>
```

Crear y cambiar a rama creada localmente.

```sh
git checkout -b <branch-name>
```

Eliminar una rama local.

```sh
git branch -d <branch-name>
```

### Ramas (branches) remotas

Visualizar ramas remotas.

```sh
git branch -r
```

Crear rama en repositorio remoto y enviar los cambios realizados localmente a la nueva rama remota.

```sh
git push origin <branch-name>
```

Crear rama localmente y actualizar desde repositorio remoto.

```sh
git checkout main
git checkout -b origin/<branch-name>
git pull origin <branch-name>
```

Eliminar una rama remota.

```sh
git push origin --delete <branch-name>
```

Git mantiene una lista local de ramas (branches) remotas para cada usuario, en caso de inconsistencias desde un usuario es posible actualizar este listado.

```sh
git remote update origin --prune
```

### Fusionar (merge)

Fusionar rama con la rama activa actualmente.

```sh
git branch
git merge <branch-name>
```

### Restaurar (restore) y revertir (revert)

Restaurar archivo modificado (modified).

```sh
git restore <file>
```

Restaurar archivo preparado (staged).

```sh
git restore --staged <file>
```

Revertir confirmación (commit) realizado.

```sh
git log --pretty=oneline -n 10
git revert <commit-hash>
```

## GitHub CLI

### Repositorios <a href="#github-cli-repositorios" id="github-cli-repositorios"></a>

Clonar repositorio.

```shell
gh repo clone <repository>
```


---

# 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://cheatsheet.mrw0l05zyn.cl/git.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.
