Difference between revisions of "GIT"

From Hugo Villeneuve
Jump to: navigation, search
 
(31 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
= Branches =
 
= Branches =
   
Ce script peut-être utile pour créér une branche locale, l'enregistrer sur le serveur remote et ensuite la faire suivre (track):
+
==Pour créér une branche locale à partir d'une branche remote existante==
  +
  +
{|
  +
|||||||
  +
| style="background: black; color: #0C0" | <tt> $> git branch -t branch_name origin/branch_name</tt>
  +
|}
  +
  +
==Ce script peut-être utile pour créér une branche locale, l'enregistrer sur le serveur remote et ensuite la faire suivre (track):==
   
 
<source lang="bash">
 
<source lang="bash">
Line 16: Line 23:
 
git checkout -b ${branch_name}
 
git checkout -b ${branch_name}
 
git push origin ${branch_name}
 
git push origin ${branch_name}
  +
  +
# For git < 1.7
  +
git branch --track ${branch_name} origin/${branch_name}
  +
  +
# For git 1.7:
 
git branch --set-upstream ${branch_name} origin/${branch_name}
 
git branch --set-upstream ${branch_name} origin/${branch_name}
  +
  +
# For git 1.8
  +
git branch -u origin/${branch_name}
 
</source>
 
</source>
  +
  +
=== Simplification===
  +
  +
<source lang="bash">
  +
git checkout -b <branch_name>
  +
git push -u <remote-name> <branch_name>
  +
</source>
  +
   
 
Pour effacer la branche distance (remote):
 
Pour effacer la branche distance (remote):
Line 25: Line 48:
 
| style="background: black; color: #0C0" | <tt> $> git push origin :${branch_name} </tt>
 
| style="background: black; color: #0C0" | <tt> $> git push origin :${branch_name} </tt>
 
|}
 
|}
  +
  +
  +
= Multiple remote repos =
  +
  +
<source lang="bash">
  +
git remote add github-myrepo https://github.com/me/myrepo.git
  +
</source>
  +
  +
In order to fetch from all the configured remotes and update tracking branches, but not merge into HEAD, do:
  +
  +
<source lang="bash">
  +
git remote update
  +
</source>
  +
   
 
= Appliquer des patches récalcitrantes =
 
= Appliquer des patches récalcitrantes =
Line 33: Line 70:
 
|}
 
|}
   
= Changer le nom de l'auteur dans un commit =
+
= Changer le nom de l'auteur dans un commit (à vérifier, ne fonctionne pas) =
   
 
{|
 
{|
Line 53: Line 90:
 
{|
 
{|
 
|||||||
 
|||||||
| style="background: black; color: #0C0" | <tt> $> git svn clone --stdlayout --no-metadata -A ~/projets/vcs/svn-to-git-migration/users.txt https://subversion.assembla.com/svn/hugovil/ hugovil </tt>
+
| style="background: black; color: #0C0" | <tt> $> git svn clone --stdlayout --no-metadata -A ~/projets/vcs/svn-to-git-migration/users.txt http://svn.domain.com/repos myrepos</tt>
 
|}
 
|}
   
Line 103: Line 140:
 
$> git remote rm origin
 
$> git remote rm origin
   
Push changes to new remote repository (example Assembla)
+
Push changes to new remote repository
$> git remote add origin git@git.assembla.com:hugovil.git
+
$> git remote add origin git@git.hugovil.com:repos/myrepos.git
 
$> git checkout master
 
$> git checkout master
 
$> git push --tags
 
$> git push --tags
Line 123: Line 160:
 
Branch bozo set up to track remote branch bozo from origin.
 
Branch bozo set up to track remote branch bozo from origin.
 
Switched to a new branch 'bozo'
 
Switched to a new branch 'bozo'
  +
  +
= Rebase root commit =
  +
  +
Taken from:
  +
  +
http://stackoverflow.com/questions/2246208/change-first-commit-of-project-with-git/2322798#2322798
  +
  +
git rebase -i allows you to conveniently edit any previous commits, except for the root commit. The following commands show you how to do this manually.
  +
  +
<source lang="bash">
  +
# tag the old root, "git rev-list ..." will return the hash of first commit
  +
git tag root `git rev-list HEAD | tail -1`
  +
  +
# switch to a new branch pointing at the first commit
  +
git checkout -b new-root root
  +
  +
# make any edits and then commit them with:
  +
git commit --amend
  +
  +
# check out the previous branch (i.e. master)
  +
git checkout @{-1}
  +
  +
# replace old root with amended version
  +
git rebase --onto new-root root
  +
  +
# you might encounter merge conflicts, fix any conflicts and continue with:
  +
# git rebase --continue
  +
  +
# delete the branch "new-root"
  +
git branch -d new-root
  +
  +
# delete the tag "root"
  +
git tag -d root
  +
</source>
  +
  +
=List all authors of a particular git project=
  +
<source lang="bash">
  +
git log --format='%aN' | sort -u
  +
</source>
  +
  +
=Nettoyage vieille références=
  +
<source lang="bash">
  +
git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d
  +
  +
git for-each-ref --format="%(refname:short)" 'refs/tags' | xargs git tag -d
  +
</source>
  +
  +
Aussi:
  +
  +
<source lang="bash">
  +
git fetch --prune
  +
</source>
  +
  +
=Prévention erreur: Warning: No xauth data; using fake authentication data for X11 forwarding=
  +
  +
Cela se produit lors d'une opération sur un dépôt auquel on accède par SSH (ex: git pull).
  +
  +
Pour le régler, ajouter cette section à ~/.ssh/config:
  +
  +
<source lang="bash">
  +
Host git.hugovil.com
  +
ForwardX11 no
  +
</source>
  +
  +
= Comment forcer un rebase lors d'un pull =
  +
  +
Exemple avec la branche master:
  +
  +
$> git config branch.master.rebase true
  +
  +
Comment le faire globalement, pour toutes les branches:
  +
$> git config --global branch.autosetuprebase always
  +
  +
  +
=Intégration d'un repo A dans un repo B en conservant l'historique=
  +
  +
Travailler à partir d'une copie:
  +
$> git clone repo-a repo-a.copie
  +
$> cd repo-a.copie
  +
  +
Effacer l'origine:
  +
$> git remote rm origin
  +
  +
Déplacer les fichiers dans un sous-dossier 'dossier-a':
  +
$> git filter-branch --index-filter 'git ls-files -s | sed "s-\t\"*-&dossier-a/-" | GIT_INDEX_FILE=$GIT_INDEX_FILE.new git update-index --index-info && mv "$GIT_INDEX_FILE.new" "$GIT_INDEX_FILE" ' HEAD
  +
  +
Créér nouveau clône du projet principal:
  +
$> git clone ssh://<project-repo> /tmp/project
  +
$> cd /tmp/project
  +
  +
Ajouter le sous-projet en tant que remote:
  +
$> git remote add -f package /tmp/package
  +
  +
Combiner le nouveau remote:
  +
$> git merge package/master
  +
  +
= Rebase =
  +
Utiliser
  +
  +
$> git --rebase-merges
  +
  +
= Configuration emails multiples =
  +
  +
Dans le fichier ${HOME}/.gitconfig, ajouter:
  +
  +
<source lang="bash">
  +
[user]
  +
name = Hugo Villeneuve
  +
email = hvilleneuve@compagnie.com
  +
[includeIf "gitdir:projets/prive/"]
  +
path = .gitconfig-prive
  +
</source>
  +
  +
Et définir ${HOME}/.gitconfig-prive
  +
  +
<source lang="bash">
  +
[user]
  +
email = hvilleneuve@prive.com
  +
</source>

Latest revision as of 18:52, 14 December 2022

Branches

Pour créér une branche locale à partir d'une branche remote existante

$> git branch -t branch_name origin/branch_name

Ce script peut-être utile pour créér une branche locale, l'enregistrer sur le serveur remote et ensuite la faire suivre (track):

#!/bin/bash
# git-create-branch <branch_name>

if [ $# -ne 1 ]; then
    echo 1>&2 Usage: $0 branch_name
    exit 1
fi

branch_name=$1

git checkout -b ${branch_name}
git push origin ${branch_name}

# For git < 1.7
git branch --track ${branch_name} origin/${branch_name}

# For git 1.7:
git branch --set-upstream ${branch_name} origin/${branch_name}

# For git 1.8
git branch -u origin/${branch_name}

Simplification

git checkout -b <branch_name>
git push -u <remote-name> <branch_name>


Pour effacer la branche distance (remote):

$> git push origin :${branch_name}


Multiple remote repos

git remote add github-myrepo https://github.com/me/myrepo.git

In order to fetch from all the configured remotes and update tracking branches, but not merge into HEAD, do:

git remote update


Appliquer des patches récalcitrantes

$> git am -3 --ignore-whitespace <name.patch>

Changer le nom de l'auteur dans un commit (à vérifier, ne fonctionne pas)

$> exec git commit --amend --author="Prénom Nom <prenom.nom@hugovil.com>" -C HEAD

Pour retrouver l'historique

$> git reflog

Conversion SVN à GIT

Cloner le répertoire SVN:

$> git svn clone --stdlayout --no-metadata -A ~/projets/vcs/svn-to-git-migration/users.txt http://svn.domain.com/repos myrepos

If you want to keep other remote branches in your repo, you want to create a local branch for each one manually. If you don't do this, the branches won't get cloned in the final step

 $> git checkout -b local_branch remote_branch

Tags are imported as branches. You have to create a local branch, make a tag and delete the branch to have them as tags in git. To do it with tag "v1":

 $> git checkout -b tag_v1 remotes/tags/v1
 $> git checkout master
 $> git tag v1 tag_v1
 $> git branch -D tag_v1

Ce script permet d'automatiser l'importation des tags:

#!/bin/bash

if [ ${#} -ne 1 ]; then
    echo "Missing tag name"
    exit 1
fi

TAG=${1}

# Tags are imported as branches. You have to create a local branch, make a tag
# and delete the branch to have them as tags in git. To do it with tag "v1":
git checkout -b tag_${TAG} remotes/tags/${TAG}
git checkout master
git tag ${TAG} tag_${TAG}
git branch -D tag_${TAG}

Clone your GIT-SVN repo into a clean git repo:

 $> git clone dest_dir-tmp dest_dir
 $> rm -rf dest_dir-tmp
 $> cd dest_dir

The local branches that you created earlier from remote branches will only have been copied as remote branches into the new cloned repository. For each branch you want to keep:

 $> git checkout -b local_branch origin/remote_branch

Finally, remove the remote from your clean git repo that points to the now deleted temp repo:

 $> git remote rm origin

Push changes to new remote repository

 $> git remote add origin git@git.hugovil.com:repos/myrepos.git
 $> git checkout master
 $> git push --tags
 $> git push origin master
 $> git checkout branch1
 $> git push origin branch1
 $> ...

When cloning and to track remote branch:

 $> git branch -a
 * master
 remotes/origin/HEAD -> origin/master
 remotes/origin/master
 remotes/origin/bozo
 $> git checkout -b bozo origin/bozo
 Branch bozo set up to track remote branch bozo from origin.
 Switched to a new branch 'bozo'

Rebase root commit

Taken from:

http://stackoverflow.com/questions/2246208/change-first-commit-of-project-with-git/2322798#2322798

git rebase -i allows you to conveniently edit any previous commits, except for the root commit. The following commands show you how to do this manually.

# tag the old root, "git rev-list ..." will return the hash of first commit
git tag root `git rev-list HEAD | tail -1`

# switch to a new branch pointing at the first commit
git checkout -b new-root root

# make any edits and then commit them with:
git commit --amend

# check out the previous branch (i.e. master)
git checkout @{-1}

# replace old root with amended version
git rebase --onto new-root root

# you might encounter merge conflicts, fix any conflicts and continue with:
# git rebase --continue

# delete the branch "new-root"
git branch -d new-root

# delete the tag "root"
git tag -d root

List all authors of a particular git project

git log --format='%aN' | sort -u

Nettoyage vieille références

git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d

git for-each-ref --format="%(refname:short)" 'refs/tags' | xargs git tag -d

Aussi:

git fetch --prune

Prévention erreur: Warning: No xauth data; using fake authentication data for X11 forwarding

Cela se produit lors d'une opération sur un dépôt auquel on accède par SSH (ex: git pull).

Pour le régler, ajouter cette section à ~/.ssh/config:

Host git.hugovil.com
   ForwardX11 no

Comment forcer un rebase lors d'un pull

Exemple avec la branche master:

 $> git config branch.master.rebase true

Comment le faire globalement, pour toutes les branches:

 $> git config --global branch.autosetuprebase always


Intégration d'un repo A dans un repo B en conservant l'historique

Travailler à partir d'une copie:

 $> git clone repo-a repo-a.copie
 $> cd repo-a.copie

Effacer l'origine:

 $> git remote rm origin

Déplacer les fichiers dans un sous-dossier 'dossier-a':

 $> git filter-branch --index-filter 'git ls-files -s | sed "s-\t\"*-&dossier-a/-" | GIT_INDEX_FILE=$GIT_INDEX_FILE.new git update-index --index-info && mv "$GIT_INDEX_FILE.new" "$GIT_INDEX_FILE" ' HEAD

Créér nouveau clône du projet principal:

 $> git clone ssh://<project-repo> /tmp/project
 $> cd /tmp/project

Ajouter le sous-projet en tant que remote:

 $> git remote add -f package /tmp/package

Combiner le nouveau remote:

 $> git merge package/master

Rebase

Utiliser

 $> git --rebase-merges

Configuration emails multiples

Dans le fichier ${HOME}/.gitconfig, ajouter:

    [user]
	name = Hugo Villeneuve
	email = hvilleneuve@compagnie.com
    [includeIf "gitdir:projets/prive/"]
	path = .gitconfig-prive

Et définir ${HOME}/.gitconfig-prive

    [user]
	email = hvilleneuve@prive.com