{"id":604,"date":"2023-07-19T15:03:47","date_gmt":"2023-07-19T15:03:47","guid":{"rendered":"https:\/\/blogs.ua.es\/jpm33\/?p=604"},"modified":"2023-12-12T10:09:03","modified_gmt":"2023-12-12T10:09:03","slug":"git-coomo-hacer-un-parche-con-los-cambios-actuales","status":"publish","type":"post","link":"https:\/\/blogs.ua.es\/jpm33\/2023\/07\/19\/git-coomo-hacer-un-parche-con-los-cambios-actuales\/","title":{"rendered":"GIT: C\u00f3mo hacer un parche con los cambios actuales"},"content":{"rendered":"<h1>Introducci\u00f3n<\/h1>\n<p>El caso por ver en esta entrada puede ser conocido por muchos usurios veteranos de git y por los n\u00f3veles que no hayan conocido c\u00f3mo solucionarlo.<\/p>\n<p>Estamos trabajando en una rama de trabajo, y queremos cambiar a la principal para hacer un pull, pero &#8230; no quermos perder o descartar los cambios, \u00bfqu\u00e9 hacer? Existen soluciones usar git stash o en IntelliJ hacer un shelf, pero c\u00f3mo dice el t\u00edtulo y si quiero almacenar TODOS los cambios que hay en el working tree en un fichero, un parche.<\/p>\n<p><!--more--><\/p>\n<p><strong>Procedimiento: Prueba de concepto<\/strong><\/p>\n<p>Vamos a crear un repositorio vacio. En este se crear\u00e1 un fichero <strong>fichero1.txt<\/strong> vacio. Se a\u00f1adir\u00e1 al git index y se har\u00e1 un commit.<\/p>\n<pre><code>\u276f git init\r\nInitialized empty Git repository in C:\/path\/to\/demoGit\/.git\/\r\n\r\n\u276f notepad fichero1.txt\r\n\u276f git add .\r\n\u276f git commit -m \"commit 1\"\r\n[master (root-commit) 035a95f] commit 1\r\n 1 file changed, 2 insertions(+)\r\n create mode 100644 fichero1.txt\r\n<\/code><\/pre>\n<p>A continuaci\u00f3n se crear\u00e1n dos ficheros: <strong>fichero2.txt<\/strong> y <strong>fichero3.txt<\/strong>. El working tree se queda con el primer fichero modificado y los siguientes marcados como <strong>??<\/strong> porqu\u00e9 no han sido a\u00f1adidos al stagging area.<\/p>\n<pre><code>\u276f notepad fichero2.txt\r\n\u276f notepad fichero3.txt\r\n\u276f git show HEAD\r\nAuthor: XXXXXXXXX \r\nDate:   Wed Jul 19 16:37:05 2023 +0200\r\n\r\ndiff --git a\/fichero1.txt b\/fichero1.txt\r\nnew file mode 100644\r\nindex 0000000..139597f\r\n--- \/dev\/null\r\n+++ b\/fichero1.txt\r\n@@ -0,0 +1,2 @@\r\n+\r\n+\r\n\u276f git status -s\r\n M fichero1.txt\r\n?? fichero2.txt\r\n?? fichero3.txt\r\n<\/code><\/pre>\n<p>Llegados a este punto es cuando queremos hacer el parche. Podemos usar el comando <code>git diff &gt; miparche.patch<\/code>\u00a0pero en este caso, s\u00f3lo a\u00f1adir\u00e1 al parche <strong>fichero1.txt<\/strong> porqu\u00e9 los otros dos no est\u00e1n en el index, debemos a\u00f1adir los ficheros<br \/>\nal stagging y usar la opcion <code>--cached<\/code>, para ver &#8220;las diferencias reales&#8221; con el commit anterior. Al terminar podemos descartar los cambios con <code>git reset<\/code>.<\/p>\n<pre><code>\u276f git add .\r\n\u276f git status -s\r\nM  fichero1.txt\r\nA  fichero2.txt\r\nA  fichero3.txt\r\n\u276f git diff --cached &gt; miparche.patch\r\n\u276f git reset\r\nUnstaged changes after reset:\r\nM       fichero1.txt\r\n\u276f git status -s\r\n M fichero1.txt\r\n?? fichero2.txt\r\n?? fichero3.txt\r\n?? miparche.patch\r\n<\/code><\/pre>\n<p>El primer <code>git status<\/code> nos muestra el estado &#8220;real&#8221; del working tree, el segundo de c\u00f3mo debemos dejarlo para hacer el parche como queremos.<\/p>\n<h1>Resultado<\/h1>\n<p>El contenido del parche queda as\u00ed:<\/p>\n<pre><code>\r\ndiff --git a\/fichero1.txt b\/fichero1.txt\r\nindex 139597f..c280f49 100644\r\n--- a\/fichero1.txt\r\n+++ b\/fichero1.txt\r\n@@ -1,2 +1 @@\r\n-\r\n-\r\n+Este es el ficherto 1\r\n\\ No newline at end of file\r\ndiff --git a\/fichero2.txt b\/fichero2.txt\r\nnew file mode 100644\r\nindex 0000000..f4a2e78\r\n--- \/dev\/null\r\n+++ b\/fichero2.txt\r\n@@ -0,0 +1 @@\r\n+Este es el ficherto 2\r\n\\ No newline at end of file\r\ndiff --git a\/fichero3.txt b\/fichero3.txt\r\nnew file mode 100644\r\nindex 0000000..26d3cb2\r\n--- \/dev\/null\r\n+++ b\/fichero3.txt\r\n@@ -0,0 +1 @@\r\n+Este es el ficherto 3\r\n\\ No newline at end of file\r\n<\/code><\/pre>\n<p>Espero que el ejemplo sea claro y pueda resultar de ayuda.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introducci\u00f3n El caso por ver en esta entrada puede ser conocido por muchos usurios veteranos de git y por los n\u00f3veles que no hayan conocido c\u00f3mo solucionarlo. Estamos trabajando en una rama de trabajo, y queremos cambiar a la principal &hellip; <a href=\"https:\/\/blogs.ua.es\/jpm33\/2023\/07\/19\/git-coomo-hacer-un-parche-con-los-cambios-actuales\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":3080,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[135,10656],"tags":[6236,236729,281],"class_list":["post-604","post","type-post","status-publish","format-standard","hentry","category-linux","category-windows-2","tag-git","tag-linux","tag-windows"],"_links":{"self":[{"href":"https:\/\/blogs.ua.es\/jpm33\/wp-json\/wp\/v2\/posts\/604","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.ua.es\/jpm33\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.ua.es\/jpm33\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.ua.es\/jpm33\/wp-json\/wp\/v2\/users\/3080"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.ua.es\/jpm33\/wp-json\/wp\/v2\/comments?post=604"}],"version-history":[{"count":2,"href":"https:\/\/blogs.ua.es\/jpm33\/wp-json\/wp\/v2\/posts\/604\/revisions"}],"predecessor-version":[{"id":628,"href":"https:\/\/blogs.ua.es\/jpm33\/wp-json\/wp\/v2\/posts\/604\/revisions\/628"}],"wp:attachment":[{"href":"https:\/\/blogs.ua.es\/jpm33\/wp-json\/wp\/v2\/media?parent=604"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.ua.es\/jpm33\/wp-json\/wp\/v2\/categories?post=604"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.ua.es\/jpm33\/wp-json\/wp\/v2\/tags?post=604"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}