User Tools

Site Tools


git

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
git [2018/04/23 10:32]
admin created
git [2018/06/08 19:39] (current)
admin
Line 2: Line 2:
  
 /​etc/​gitconfig (específica del sistema, en windows en el $HOME del usuario) /​etc/​gitconfig (específica del sistema, en windows en el $HOME del usuario)
-  git config --system --list+  ​>git config --system --list
  
 ~/​.gitconfig ~/​.gitconfig
 o o
 ~/​.config/​git/​config (específicas del usuario) ~/​.config/​git/​config (específicas del usuario)
-  git config --global --list+  ​>git config --global --list
   ​   ​
 .git/config (específica del repositorio). .git/config (específica del repositorio).
-  git config --list+  ​>git config --list
  
  
Line 16: Line 16:
  
 Valores que se pueden cambiar: user.name, user.email, core.editor Valores que se pueden cambiar: user.name, user.email, core.editor
-  git config --global core.editor "'​C:/​Program Files/​Notepad++/​notepad++.exe'​ -multiInst ..."+  ​>git config --global core.editor "'​C:/​Program Files/​Notepad++/​notepad++.exe'​ -multiInst ..."
  
 +====== Operaciones básicas ======
 +===== Iniciar repositorio =====
 +  >git init
 +===== Clonar un repositorio =====
 +  >git clone https://​github.com/​libgit2/​libgt2
 +se puede especificar el directorio destino con
 +  >git clone https://​github.com/​libgit2/​libgt2 mlibgit
 +===== Añadir ficheros al repositorio =====
 +  >git add *.c
 +===== Borrar ficheros del staging =====
 +  >git rm --cached README
 +
 +===== Commit =====
 +  >git commit -m 'Texto para el commit'​
 +Si no se especifica la opción -m se lanzará el editor por defecto para guardar en un fichero temporal el mensaje del commit. Con el flag -a se realizará commit de todos aquellos ficheros que estén siendo registrados,​ aunque no estén stageados
 +
 +===== Comprobar el estado de los ficheros =====
 +  >git status
 +  On branch master
 +  ​
 +  No commits yet
 +  ​
 +  nothing to commit (create/​copy files and use "git add" to track)
 +
 +
 +  >echo 'My project'​ > README
 +  >git status
 +  On branch master
 +  ​
 +  No commits yet
 +  ​
 +  Untracked files:
 +  (use "git add <​file>​..."​ to include in what will be committed)
 +  ​
 +        README
 +  ​
 +  nothing added to commit but untracked files present (use "git add" to track)
 +
 +
 +  >git add README
 +  >git status
 +  On branch master
 +  ​
 +  No commits yet
 +  ​
 +  Changes to be committed:
 +  (use "git rm --cached <​file>​..."​ to unstage)
 +  ​
 +        new file:   ​README
 +
 +Suponiendo un fichero CONTRIBUTING.md existe y está comiteado, tras modificarlo lo que saldrá del status es lo siguiente:
 +
 +  >git status
 +  On branch master
 +  Changes to be committed:
 +  (use "git reset HEAD <​file>​..."​ to unstage)
 +  ​
 +        new file:   ​README
 +  ​
 +  Changes not staged for commit:
 +  (use "git add <​file>​..."​ to update what will be committed)
 +  (use "git checkout -- <​file>​..."​ to discard changes in working directory)
 +  ​
 +        modified: ​  ​CONTRIBUTING.md
 +
 +
 +Tras esto hay que añadirlo al staging para que a continuación salga lo siguiente:
 +
 +  >git add CONTRIBUTING.md
 +  ​
 +  >git status
 +  On branch master
 +  Changes to be committed:
 +  (use "git reset HEAD <​file>​..."​ to unstage)
 +  ​
 +        modified: ​  ​CONTRIBUTING.md
 +        new file:   ​README
 +
 +Si se modifica el fichero CONTRIBUTING.md el resultado del status es el siguiente
 +  >echo "​huy"​ > CONTRIBUTING.md
 +  >git status
 +  On branch master
 +  Changes to be committed:
 +  (use "git reset HEAD <​file>​..."​ to unstage)
 +  ​
 +        modified: ​  ​CONTRIBUTING.md
 +        new file:   ​README
 +  ​
 +  Changes not staged for commit:
 +  (use "git add <​file>​..."​ to update what will be committed)
 +  (use "git checkout -- <​file>​..."​ to discard changes in working directory)
 +  ​
 +        modified: ​  ​CONTRIBUTING.md
 +
 +El fichero que se comitearia sería el primero. Si se quiere comitear el segundo hay que hacer otro git add
 +
 +  git add CONTRIBUTING.md
 +  ​
 +  git status
 +  On branch master
 +  Changes to be committed:
 +  (use "git reset HEAD <​file>​..."​ to unstage)
 +  ​
 +        modified: ​  ​CONTRIBUTING.md
 +        new file:   ​README
 +
 +existe una manera de sacar estos listados de manera abreviada:
 +
 +  >git status --short
 +  M  CONTRIBUTING.md
 +  A  README
 +  MM fichero1
 +  ?? fichero2
 +   M fichero3
 +
 +Los ficheros que no se han registrado en el staging aún tienen un ??. Los nuevos ficheros que se han añadido al staging tienen una A, los modificados tienen una M. Además hay dos columnas, la primera o izquierda indica el estado del staging, y la segunda la del working tree. De esta manera el fichero3 está modificado en el working directory pero no está en el staging, mientras que el contriguting.md está modificado en el working tree y está en el staging, mientras que el fichero1 está modificado en el working tree, stageado y vuelvo a modificar sin stagear
 +
 +==== Ignorar ficheros ====
 +
 +  >cat .gitignore
 +Las líneas en blanco o que empiecen por # son ignoradas. ​
 +Las expresiones regulares se realizan con patrones glob.
 +Los patrones que empiezan por / evitan la recursividad. El resto de / especifican un directorio.
 +La ! niega el patrón
 +
 +==== Comparar ficheros ====
 +
 +  >git diff
 +  diff --git a/​src/​app/​app.component.ts b/​src/​app/​app.component.ts
 +  index c41599e..8c1cf1c 100644
 +  --- a/​src/​app/​app.component.ts
 +  +++ b/​src/​app/​app.component.ts
 +  @@ -6,5 +6,5 @@ import { Component } from '​@angular/​core';​
 +     ​styleUrls:​ ['​./​app.component.css'​]
 +   })
 +   ​export class AppComponent {
 +  -  title = 'Tour of Heroes';​
 +  +  title = 'Tour of Heroes 2';
 +   }
 +
 +Compara lo que hay en el staging area con el working directory. Si a continuación se añade el fichero no habrá ninguna diferencia
 +  >git add src\app\app.component.ts
 +  >git diff
 +  >
 +Si se quiere ver lo que va a entrar en el commit hay que compara con el stagging
 +  >git diff --staged
 +  diff --git a/​src/​app/​app.component.ts b/​src/​app/​app.component.ts
 +  index c41599e..8c1cf1c 100644
 +  --- a/​src/​app/​app.component.ts
 +  +++ b/​src/​app/​app.component.ts
 +  @@ -6,5 +6,5 @@ import { Component } from '​@angular/​core';​
 +     ​styleUrls:​ ['​./​app.component.css'​]
 +   })
 +   ​export class AppComponent {
 +  -  title = 'Tour of Heroes';​
 +  +  title = 'Tour of Heroes 2';
 +  }
 +
 +Para elegir otra herramienta para ver las diferencias,​ se pueden listar las disponibles mediante:
 +  >git difftool --tool-help
 +  >git difftool --tool=winmerge
 +
 +En el fichero .git/config habrá que añadir las siguientes líneas
 +  [diff]
 +  tool = winmerge
 +  [difftool "​winmerge"​]
 +  name = "​WinMerge"​
 +  trustExitCode = true
 +  cmd = "/​c/​Program\\ Files\\ \\(x86\\)/​WinMerge/​WinMergeU.exe"​ -u -e -dl \"​Local\"​ -dr \"​Remote\"​ $LOCAL $REMOTE $MERGED
 +  [merge]
 +  tool = winmerge
 +  [mergetool "​winmerge"​]
 +  name = WinMerge
 +  trustExitCode = true
 +  cmd = "/​c/​Program\\ Files\\ \\(x86\\)/​WinMerge/​WinMergeU.exe"​ -u -e -dl \"​Local\"​ -dr \"​Remote\"​ $LOCAL $REMOTE $MERGED
 +y su uso se realizará de la siguiente forma:
 +  >git difftool --staged
 +
 +==== Borrado de ficheros ====
 +
 +Para borrar un fichero hay que quitarlo del staging y hacer commit
 +  >rm PROJECTS.md
 +  >git rm PROJECTS.md
 +  >git commit
 +Si ya se había metido en el index lo que hay que hacer es utilizar el flag -f
 +Si lo que se quiere es eliminarlo del staging pero no del working tree, lo que hay que hacer es utilizar la opción --cached
 +
 +==== Visión de históricos ====
 +
 +  >git log
 +  >git log -p (lista diferencias entre commits)
 +  >git log -2 (muestra únicamente los últimos dos)
 +  >git log --pretty=oneline (muestra commits resumidos uno por línea)
 +  >git log --pretty=format:"​%h %s" --graph (muestra en formato de gráfico el historial de commits)
 +  >git log --since=2.weeks (muestra los commits desde hace 2 commits. Funciona con --until y como valores fechas "​2018-01-01"​ o "3 years 1 day 3 minutes ago". Los flags --author --grep. Por defecto son or. Si se quiere and hay que utilizar --all-match. -S muestra únicamente los commits que hay añadido o modificado esa cadena
 +
 +===== Undos =====
 +  >git commit --amend
 +Hace commit del staging area y modifica para ello el último commit. En caso de que no haya nada, lo único que hace es cambiar el mensaje de commit
 +
 +Si lo que se quiere es quitar del staging un fichero, git status muestra cómo hacerlo
 +  >git status
 +  >git reset HEAD CONTRIBUTING.md
 +
 +Para dejar el fichero como estaba tras el último commit lo que hay que hacer es lo siguiente:
 +  >git checkout -- CONTRIBUTING.md
 +
 +===== Repositorios remotos =====
 +  >git remote
 +  origin
 +  ​
 +  >git remote -v
 +  origin https://​gitlab.com/​Aquiles1184/​angular-tour-of-heroes.git (fetch)
 +  origin https://​gitlab.com/​Aquiles1184/​angular-tour-of-heroes.git (push)
 +
 +Esto muestra todos los repositorios remotos configurados a los que se puede hacer push. Para añadir uno nuevo hay que utilizar el comando:
 +  >git remote add minombre https://​github.com/​paulboone/​ticgit
 +Esto añadirá las dos líneas del ejemplo anterior
 +
 +Para descargar todos los cambios se realiza:
 +  >git fetch nombre remoto
 +El comando clone automáticamente configura el master local para registrar la rama master remota en el servidor.
 +  >git pull
 +descarga los datos y trata de hacer merge en la misma rama local
 +
 +Para subir los cambios:
 +  >git push origin master
 +Este comando sube la rama master al servidor apuntado por origin
 +
 +
 +Para ver informacion del repositorio remoto
 +  >git remote show origin
 +  * remote origin
 +  Fetch URL: https://​gitlab.com/​Aquiles1184/​angular-tour-of-heroes.git
 +  Push  URL: https://​gitlab.com/​Aquiles1184/​angular-tour-of-heroes.git
 +  HEAD branch: master
 +  Remote branch:
 +    master tracked
 +  Local ref configured for 'git push':
 +    master pushes to master (up to date)
 +
 +Para renombrar un repositorio remoto la sintaxis es:
 +  >git remote rename pb paul
 +
 +Para borrar un repositorio remoto
 +  >git remote rm paul
 +
 +===== Tags =====
 +  >git tag
 +Muestra todas las etiquetas del proyecto. También es posible buscar con caracteres comodín
 +  >git tag -l "​v1.8.5*"​
 +
 +===== Branches =====
 +Para crear un branch ​
 +  >git checkout -b nombreBranch [tag]
 +Si [tag] tiene algo se creará un branch de ese tag. En caso contrario se creará un branch con lo que se tenga en ese momento en el working tree
git.1524472351.txt.gz · Last modified: 2018/04/23 10:32 by admin