= git tips
{{tag>git}}
== git fetch VS git pull
* git pull = git fetch (+ git checkout) + git merge
* modo seguro:git fetch origin
git log --oneline main..origin/main
git checkout main
git log origin/main
git merge origin/main
* [[https://www.atlassian.com/es/git/tutorials/syncing/git-fetch]]
* [[https://www.atlassian.com/es/git/tutorials/using-branches/git-checkout]]
* [[https://www.atlassian.com/es/git/tutorials/using-branches/git-merge]]
* [[https://www.atlassian.com/es/git/tutorials/syncing/git-pull]]
== ramas
git checkout && git branch -m # o git branch -m
git push -u origin # y comprobar en remoto que todo ha ido bien
git push origin : # elimina la rama vieja del remoto!
git ck -b rama_destino
git cherry-pick .. # commit1^..commit2 para incluir commit1 en el movimiento
git push origin rama_destino # envíamos los cambios
git push origin :rama_origen # elimina la rama mal colocada
git pull --rebase origin develop
git tag [-l]
git show
git tag -a -m "" # tag local annotated
git push origin # subir tag a remoto
git -d # borrar tag local
git push --delete origin # borrado tag en remoto
git remote update origin --prune
git branch -a
/via: [[https://stackoverflow.com/questions/36358265/when-does-git-refresh-the-list-of-remote-branches]]
git branch nueva-rama
git reset HEAD~ --hard
git checkout nueva-rama
# deshaz el útlimo commit, pero deja los cambio disponibles
git reset HEAD~ --soft
git stash
# muevete a la rama correcta
git checkout nombre-de-la-rama-correcta
git stash pop
git add . # or add individual files
git commit -m "your message here";
# ahora tus cambios estan en la rama correcta
git checkout nombre-de-la-rama-correcta
# coge el último commit de master
git cherry-pick master
# borralo de master
git checkout master
git reset HEAD~ --hard
== pequeños cambios en commits (no push)
git add .
git commit --amend --no-edit
git commit --amend
== retroceder o cambiar el pasado(rebase/reset)
git reflog # cada entrada tiene un HEAD@{index}
git reset HEAD@{index}
git rebase -i HEAD^ # y marcar con reword
git rebase -i ^ # con ^ se incluye el que indiquemos
git rebase -i HEAD~n # los últimos n
# marcar con SQUASH aquellos que queramos fusionar, dejando el mayor ancestro como destinatario de todos ellos
# git push origin -f # fuerza la subida de los cambios
git rebase -i HEAD^ # y marcar con drop
git reset HEAD~1
== ficheros
git checkout
# busca el hash del commit anterior de cuando se cambio el archivo
git log
# usa las flechas para moverte para arriba y abajo en la historia
# una vez que encontraste el commit, guarda su hash
git checkout [hash guardado] -- path/to/file
# la version anterior del archivo estará en tu index
git commit -m "Waw, no tienes que hacer copiar-pegar para deshacer"
git log --oneline --max-count=10
git diff-tree --no-commit-id --name-only -r
git show --pretty="" --name-only
git log --follow -p --
git log --stat
* https://git-scm.com/docs/git-log
git whatchanged []
* [[https://git-scm.com/docs/git-whatchanged]]
* mantenido por razones históricas
=== diferencias entre commits
* (ohshitgit) diferencias de ficheros en **staged**:git diff --staged
* diferencia de ficheros entre dos commits:git diff --name-only SHA1 SHA2
* generar ZIP con ficheros cambiados entre 2 commits:git archive --output=file.zip HEAD $(git diff --name-only SHA1 SHA2)
* /via:[[https://www.solucionex.com/blog/ficheros-modificados-entre-dos-commits-con-git]]
=== recuperar
* localizar fichero: git log --
* recuperar fichero: git checkout --
* [[https://recoverit.wondershare.es/file-recovery/recover-files-from-local-repository-git.html]]
== búsquedas
git log --
git log --follow -p --
git log --name-status -- "**"
* ''--'' indica a git que lo que viene a continuación son rutas de ficheros y no ramas
git log -S"Hello, World!"
/via: [[https://www.atlassian.com/git/tutorials/git-log]]