Comparar archivos, commits y ramas en Git

John Wachira 21 junio 2022
Comparar archivos, commits y ramas en Git

Este artículo nos enseñará cómo comparar archivos, commits y ramas en Git usando el comando git diff. Usamos el comando git diff para mostrar las disparidades entre los archivos resultantes de dos commits o el estado actual de nuestro repositorio y un commit anterior.

El comando es útil cuando se comparan fuentes de datos.

El comando git diff

Sintaxis:

git diff

De forma predeterminada, el comando mostrará los cambios no confirmados en nuestro repositorio.

El comando nos mostrará todas las líneas de código eliminadas y agregadas en el archivo original. Veamos algunos ejemplos.

commits de Git Diff

Podemos comparar dos commits en la misma rama en el contexto a continuación.

git diff <commit-1-id> <commit-2-id>

Ejemplo:

Aquí está el historial del commit de nuestro repositorio local.

$ git log --oneline
c84e6f9 (HEAD -> main) Third Code Correction
9af4e38 Second Code Correction
a45ca97 First Code Correction
8c1cefc My Commit Message
c5bf6c8 Sixth Commit
3b641e0 Fourth Commit
21ca1e7 Third Commit
b2f7710 Initial commit

En un escenario en el que queríamos ver las diferencias entre nuestras commits de Third Code Correction y Second Code Correction, ¿cómo lo haríamos?

Ejecutaremos el comando git diff y mencionaremos el hash de nuestras dos commits, como se muestra a continuación.

$ git diff 9af4e38 c84e6f9
diff --git a/insert.php b/insert.php
index 985a7af..a5f31c6 100644
--- a/insert.php
+++ b/insert.php
@@ -1,7 +1,6 @@
 <?php
 // Use ls command to shell_exec function
 $output = shell_exec('git');
-
 // Display the list of all files and directories
 echo "<pre>$output</pre>";
 ?>

El resultado anterior muestra una diferencia en la línea 67. Podemos ver que el commit Third Code Correction eliminó una línea de código vacía en la línea 67.

Ramas de diferencias de Git

Para comparar dos ramas en nuestro repositorio, ejecutamos este comando.

git diff <branch1> <branch2>

Ejecutaríamos este comando si quisiéramos comparar la rama master y otra rama llamada dev.7.

git diff master dev.7

Si agregamos dos puntos entre las ramas, Git comparará las últimas commits entre los dos.

Archivos Git Dif

Podemos comparar dos archivos en nuestro repositorio usando el comando git diff en el contexto a continuación.

git diff <path-to-file1> <path-to-file2>

Ejemplo:

$ git diff Head:sample.php HEAD:insert.php
diff --git a/sample.php b/insert.php
index dce9c57..a5f31c6 100644
--- a/sample.php
+++ b/insert.php
@@ -1,14 +1,6 @@
 <?php
-    $a= 23;
-    $nationality = "Dutch";
-    //applying conditions on nationality and age
-    if ($nationality == "Dutch")
-    {
-        if ($a >= 18) {
-            echo "Eligible to vote";
-        }
-        else {
-            echo "Not eligible to vote";
-        }
-    }
+// Use ls command to shell_exec function
+$output = shell_exec('git');
+// Display the list of all files and directories
+echo "<pre>$output</pre>";

Observe cómo especificamos la rama en el comando git diff. Si los archivos están en diferentes ramas, deberá especificar la rama para cada archivo.

John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Artículo relacionado - Git Diff