User Tools

Site Tools


informatica:git

Differences

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

Link to this comparison view

Next revision
Previous revision
informatica:git [2013/12/30 07:29] – created javiinformatica:git [2022/01/06 20:19] (current) jose
Line 1: Line 1:
-===== git =====+====== git ====== 
 + 
 +===== Tags ===== 
 + 
 +1. Listar los commits de un repositorio 
 + 
 +<code> 
 +git log --pretty=oneline 
 +</code> 
 + 
 +2. Marcar con una etiqueta (en este ejemplo "v1.0") el commit deseado: 
 + 
 +<code> 
 +git tag -a v1.0 947c168ba5cc2c40dabe15b1a140e0a90f29f3c3 
 +</code> 
 + 
 +3. Subir las etiquetas al repositorio remoto: 
 + 
 +<code> 
 +git push origin v1.0 
 +</code> 
 + 
 +===== Iniciar repositorio en remoto ===== 
 + 
 +Given a project in our local file system: 
 +  /home/user/manhattan_project 
 + 
 +1. Create the git repository: 
 +  git init /home/user/manhattan_project 
 + 
 +2. Do the first commit 
 +  cd /home/user/manhattan_project 
 +  git add * 
 +  git commit -m "Initial commit" 
 + 
 +3. Create the "bare" repository: 
 +  git clone --bare /home/user/manhattan_project /tmp/manhattan_project.git 
 + 
 +4. Move the directory to its final location using SSH 
 +  scp -rv /tmp/manhattan_project.git user@git.example.com:/git/repositories/ 
 +   
 +===== Subir un cambio ===== 
 + 
 +  git add <fichero> 
 +  git commit -m "cambio realizado" 
 +  git push 
 +===== Añadir rama branch ===== 
 +  git pull 
 +  git checkout -b test 
 +  git push origin test 
 +Listar ramas: 
 +<code> 
 +git branch -a 
 +  master 
 +* test 
 +  remotes/origin/HEAD -> origin/master 
 +  remotes/origin/master 
 +  remotes/origin/test 
 +</code> 
 +Cambiar de rama: 
 +<code> 
 +git checkout master 
 +A Dockerfile_ok 
 +A Dockerfile_test 
 +Switched to branch 'master' 
 +Your branch is up to date with 'origin/master'
 +</code> 
 + 
 +===== Servidor git acceso claves ssh ===== 
 + 
 +http://git-scm.com/book/en/v2/Git-on-the-Server-Setting-Up-the-Server 
 + 
 +0. (Opcional) Instalo paquetes 
 + 
 +  sudo aptitude install git 
 + 
 +1. Crear usuario git con el minimo de permisos posible 
 + 
 +  sudo vim /etc/shells 
 + 
 +Y anyadir, si no existe, la siguiente linea: 
 + 
 +  /usr/bin/git-shell 
 + 
 +Seguimos con la creacion del usuario "git": 
 + 
 +<code> 
 +sudo adduser git 
 +sudo chsh git 
 +</code> 
 + 
 +Teclear: 
 + 
 +  /usr/bin/git-shell 
 + 
 +Y pulsar "enter" 
 + 
 +<code> 
 +sudo su git 
 +cd 
 +mkdir .ssh && chmod 700 .ssh 
 +touch .ssh/authorized_keys ; chmod 600 .ssh/authorized_keys 
 +</code> 
 + 
 +2. Genero claves en el cliente y las subo al servidor 
 + 
 +<code> 
 +ssh-keygen -t rsa 
 +scp /home/user/.ssh/id_rsa.pub remote.example.com:/tmp 
 +</code> 
 + 
 +3. Volcar las claves: 
 + 
 +<code> 
 +sudo su git 
 +cat /tmp/id_rsa.pub >> ~/.ssh/authorized_keys 
 +</code> 
 + 
 +===== Volver a un commit previo ===== 
 + 
 +Escenario: 
 + 
 +* Queremos volver a un commit previo 
 +* Queremos hacer un nuevo commit indicando "se ha vuelto al commit XXXX" 
 + 
 +1. Identificar el commit ID al que queremos volver: 
 + 
 +  git log 
 + 
 +En este ejemplo: 
 + 
 +  commit c501b1d989d780ab17412fc646b62bfe24aa24d5 (tag: v0.10.0) 
 + 
 +2. Volver a ese commit en la copia local: 
 + 
 +  git reset --hard c501b1d989d780ab17412fc646b62bfe24aa24d5 
 + 
 +3. **TODO**: clarificar qué hace exactamente este paso 
 + 
 +  git reset --soft HEAD@{1} 
 + 
 +4. Hacer un nuevo commit 
 + 
 +  git commit -m "Reverting to the state of the project at c501b1d989d780ab17412fc646b62bfe24aa24d5" 
 + 
 +5. Subir los cambios al repositorio remoto 
 + 
 +  git push 
 + 
 +===== Comandos sueltos =====
  
 http://stackoverflow.com/questions/3258243/git-check-if-pull-needed http://stackoverflow.com/questions/3258243/git-check-if-pull-needed
  
-  git remote update+  * To bring your remote refs up to date: 
 +<code> 
 +git remote update 
 +</code> 
 +  * Will tell you whether the branch you are tracking is ahead, behind or has diverged. If it says nothing, the local and remote are the same.   
 +<code> 
 +git status -uno 
 +</code>   
 +  * Will show you the commits in all of the branches whose names end in master (eg master and origin/master). 
 +<code> 
 +git show-branch *master 
 +</code> 
 +  * Subir tags a remoto 
 +<code> 
 +git push --tags origin 
 +</code> 
 +  * Descargar un tag determinado: 
 +<code> 
 +git clone -b 'v1.0' --single-branch --depth 1 https://github.com/Kedu-SCCL/redmine-automation 
 +</code>
  
-To bring your remote refs up to date:+===== Hooks ===== 
 + 
 +==== Desplegar el contenido de un repositorio tras recibir un commit ==== 
 + 
 +1. Crear el hook 'post-receive' en el lado servidor: 
 + 
 +  sudo vim /srv/git/test.git/hooks/post-receive
      
-  git status -uno+Con el siguiente contenido: 
 + 
 +<code> 
 +#!/bin/sh 
 +git --work-tree=/srv/www/test --git-dir=/srv/git/test.git checkout -f 
 +</code> 
 + 
 +  chmod +x /srv/git/test.git/hooks/post-receive
      
-Will tell you whether the branch you are tracking is ahead, behind or has diverged. If it says nothing, the local and remote are the same.+2Hacer un commit
  
-  git show-branch *master+Resultado: el directorio "/srv/www/test" tendria que tener el contenido de del repositori "/srv/git/test.git" expandido 
 + 
 +==== Enviar un correo tras recibir un commit ==== 
 + 
 +1. Crear el hook 'post-receive' en el lado servidor: 
 + 
 +  sudo vim /srv/git/test.git/hooks/post-receive
      
-Will show you the commits in all of the branches whose names end in master (eg master and origin/master).+Con el siguiente contenido: 
 + 
 +<code> 
 +#!/bin/bash 
 + 
 +# Please adjust this setting 
 +RECIPIENTS="user1@example.com,user2@example.com" 
 +GIT="/usr/bin/git" 
 +SENDMAIL="/usr/sbin/sendmail" 
 +export USER_EMAIL=$($GIT log -1 --format=format:%ae HEAD) 
 +REPOSITORY=${PWD##*/}  
 +MAIL_CONTENT=`$GIT log --name-status HEAD^..HEAD` 
 +SUBJECT=$USER_EMAIL" updated git repo '"$REPOSITORY"'" 
 + 
 +$SENDMAIL "$RECIPIENTS" <<EOF 
 +subject:$SUBJECT 
 +from:$USER_EMAIL 
 +$MAIL_CONTENT 
 +EOF 
 +</code> 
 + 
 +  chmod +x /srv/git/test.git/hooks/post-receive 
 +   
 +2. Hacer un commit 
 + 
 +Resultado: el directorio "/srv/www/test" tendria que tener el contenido de del repositori "/srv/git/test.git" expandido 
 + 
 +==== GITHUB ==== 
 +Vamos a Settings "SSH and GPG keys" y añadimos la clave. 
 + 
 +Creamos el repositorio en GITHUB. Mejor no inicializar con README.md Allí pone las instrucciones: 
 +<code> 
 +git init . 
 +git add . 
 +git commit -m "primer commit" 
 +git remote add origin git@github.com:iwanttobefreak/docker-selenium.git 
 +git push -u origin master 
 +</code> 
 + 
 +====== Borrar un fichero ====== 
 + 
 + 
informatica/git.1388388559.txt.gz · Last modified: 2015/04/13 20:19 (external edit)