cristinafsanz
9/5/2019 - 8:35 AM

Git commands

Algunos comandos usados en proyectos:

Operaciones locales:
working directory -> staging area -> git repository

Crear rama en local

# Situarte en la rama desde donde quieres crear la rama
git checkout develop

# Comprobar que se tienen los últimos cambios
git fetch
git pull

# Crear la rama
git checkout -b feat/new-feature

Añadir al staging area todos los ficheros

# Ver el estado actual de los cambios (qué está en el índice para subir)
git status

# Comprobar en VS Code las diferencias de los ficheros para asegurar que lo que subes es lo que quieres subir

git add .

Quitar un fichero del staging area

git reset HEAD -- <file>

Deshacer cambios de los ficheros que están en el working directory (todavía no subidos al staging area)

git checkout .

Commit para subir ficheros del staging area al git directory

git commit -m "Commit message"

Subir a la misma rama en remoto

git push origin HEAD

Rebase de master a tu rama

# Asegurarte que estás en tu rama 

git status

# Sincronizarte con el remoto

git fetch

git rebase origin/master

# resolver conflictos si los hubiera

git add .

git rebase —continue

# subir la rama rebasada a remoto con -f

git push origin "feature-branch" -f

Squash

http://gitready.com/advanced/2009/02/10/squashing-commits-with-rebase.html

Ejemplo: Combinar los últimos 4 commits tuyos en el primer commit de la lista:

git rebase -i HEAD~4

# Ejemplo (:wq al final para guardar como en vi)
pick 01d1124 Adding license
squash 6340aaa Moving license into its own file
squash ebfd367 Jekyll has become self-aware.
squash 30e0ccb Changed the tagline in the binary, too.

git add <file1> <file2>  # si conflictos

git rebase —continue # si conflictos

git push origin <branchname> -f

Git amend

Ejemplo: quieres añadir algo al último commit

# Haces los cambios

git add <file1> <file2>

git commit --amend

git push origin <branchname> -f

Dejar tu rama como en remoto

git reset —hard origin/rama 

ó

git checkout -B master origin/master

Borrar rama remota y local

git push -d origin feat/feature-branch
git branch -D feat/feature-branch

Borrar todas las ramas locales excepto master

git branch | grep -v "master" | xargs git branch -D

Cambiar rama local y remota

https://multiplestates.wordpress.com/2015/02/05/rename-a-local-and-remote-branch-in-git/

git branch -m feat/MHF-725-params
git push origin :feat/MHF-841-params feat/MHF-725-params
git push origin -u  feat/MHF-725-params

Guardar cambios en stash y recuperarlos

# Guardar
git stash

# Recuperar borrando el stash
git stash pop

# Recuperar manteniendo el stash
git stash apply

Buscar git commit por mensaje

git log --all --grep='Build 0051'

Ver cambios entre 2 commits

git log index.html
git diff 307f6132baddc1778217afa266f992005e8267db a76f3214d316acde9a0b8747744f35faae55e91d

Borrar tags

# Remote:
git push --delete origin tagname

# Local:
git tag --delete tagname

Crear tags

git tag -a 3.26.0 -m "Version 3.26.0”
 
git push origin 3.26.0

Tree-like view of commits

git log --graph --oneline --all

Abort git commit (vi)

:cq

Copy code base for another project

  • Git clone
  • Remove .git
  • Run git init .

Error unrelated histories

git pull --allow-unrelated-histories