Trucs et astuces sur GIT

Envoyer un diff coloré par mail (utilisation de html)

Mise dans l’historique GIT d’un ensemble de fichier/dossiers du même niveau

Lors de l’import en masse de données dans GIT, principalement des usages “exotiques” il peut être acceptable de commiter différents fichiers et dossiers séparément.

Parmi ces usages, on peut citer :

  • conserver des photos
  • historiser un dossier maildir (1 fichier par mail + dossiers avec noms composés pour aplatir une arborescence)
  • sauvegarder des fichiers principalement binaires
  • et autres …

A noter que du fait du fonctionnement interne de git, notamment de la staging, commiter séparément de gros volumes de données permet d’accélérer l’import.

$ for i in *; do git add "${i}"; git commit "${i}" -m "ajout fichier ${i}"; done

Changer le nom d’auteur d’une liste de commits

Version inline

$ git filter-branch -f --env-filter "GIT_AUTHOR_NAME='<name>'; GIT_AUTHOR_EMAIL='<mail>'; GIT_COMMITER_NAME='<name>'; GIT_COMMITTER_EMAIL='<mail>';" HEAD

Version script bash — NON TESTÉ





Suppression du fichier dans tout l’historique

$ git filter-branch --index-filter 'git rm --cached --ignore-unmatch B_ANCIEN/DUT2A/STAGE_DUT_TOUT.tar' HEAD

Explications :

  • rm : suppression
  • --cached : les informations conservées dans l’arbre des révisions
  • --ignore-unmatch : ne pas prendre en compte ce qui ne correspond pas = supprimer ce qui correspond …
  • HEAD: à appliquer à partir de la tête de l’arbre

Configuration habituelle

Fichier de config global : ~/.gitconfig

	ui = auto
[color "branch"]
	current = yellow reverse
	local = yellow
	remote = green
[color "diff"]
	meta = yellow bold
	frag = magenta bold
	old = red bold
	new = green bold
	whitespace = red reverse
[color "status"]
	added = yellow
	changed = green
	untracked = cyan
	a = add -p
	ci = commit
	co = checkout
	st = status
	dc = diff --cached
	df = diff
	lg = log -p
	lol = log --graph --decorate --pretty=oneline --abbrev-commit
	lola = log --graph --decorate --pretty=oneline --abbrev-commit --all
	ls = ls-files

	# Show files ignored by git:
	ign = ls-files -o -i --exclude-standard


  • reverse commit specified by <rev> and commit the result. This does not do the same thing as similarly named commands in other VCS’s such as "svn revert » or "bzr revert", see below

    bash git revert <rev>

  • re-checkout <file>, overwriting any local changes

    bash git checkout <file>

  • re-checkout all files, overwriting any local changes. This is most similar to "svn revert" if you’re used to Subversion commands

    bash $ git checkout .

Fix mistakes / Undo

  • abandon everything since your last commit; this command can be DANGEROUS. If merging has resulted in conflicts and you’d like to just forget about the merge, this command will do that.

    bash $ git reset --hard

  • undo your most recent successful merge and any changes that occurred after. Useful for forgetting about the merge you just did. If there are conflicts (the merge was not successful), use "git reset --hard" (above) instead.

    bash git reset --hard ORIG_HEAD `


    bash $ git reset --hard origin/master

  • forgot something in your last commit ? That’s easy to fix. Undo your last commit, but keep the changes in the staging area for editing.

    bash $ git reset --soft HEAD^

  • redo previous commit, including changes you’ve staged in the meantime. Also used to edit commit message of previous commit.

    bash $ git commit --amend


  • save your local modifications to a new stash (so you can for example "git svn rebase" or "git pull")

    bash $ git stash git stash save <optional-name>

  • restore the changes recorded in the stash on top of the current working tree state

    bash $ git stash apply

  • restore the changes from the most recent stash, and remove it from the stack of stashed changes

    bash $ git stash pop


  • prune deleted remote-tracking branches from “git branch -r” listing

    bash git remote prune <remote>

  • add a remote and track its master

    bash $ git remote add -t master -m master origin git://

  • show information about the remote server.

    bash git remote show <remote>

  • Track a remote branch as a local branch.

    bash git checkout -b <local branch> <remote>/<remote branch>


$ git checkout -b myfeature origin/myfeature 


  • add the given repository at the given path. The addition will be part of the next commit.

    bash git submodule add <remote_repository> <path/to/submodule>

  • Update the registered submodules (clone missing submodules, and checkout the commit specified by the super-repo). —init is needed the first time.

    bash git submodule update [--init]

  • Executes the given command within each checked out submodule.

    bash git submodule foreach <command>

Removing submodules

  1. delete the relevant line from the .gitmodules file.
  2. delete the relevant section from .git/config.
  3. run git rm --cached path_to_submodule (no trailing slash).
  4. commit and delete the now untracked submodule files.

Updating submodules

To update a submodule to a new commit:

  1. update submodule:

    bash $ cd <path to submodule> $ git pull

  2. commit the new version of submodule:

    bash $ cd <path to toplevel> $ git commit -m "update submodule version"

  3. check that the submodule has the correct version

    bash $ git submodule status

If the update in the submodule is not committed in the main repository, it is lost and doing git submodule update will revert to the previous version.


  • Will export expanded tree as tar archive at given path

    bash $ git archive master | tar -x -C /somewhere/else

  • Will export archive as bz2

    bash $ git archive master | bzip2 > source-tree.tar.bz2

  • Will export as zip

    bash $ git archive --format zip --output /full/path master

Git Instaweb

  • gestion

    bash git instaweb --httpd=webrick [--start | --stop | --restart]

Environment Variables

  • Your full name to be recorded in any newly created commits. Overrides in .git/config

  • Your email address to be recorded in any newly created commits. Overrides in .git/config

  • Location of the repository to use (for out of working directory repositories)

  • Location of the Working Directory - use with GIT_DIR to specifiy the working directory root or to work without being in the working directory at all.


Changing history

Change author for all commits with given name

$ git filter-branch --commit-filter '
	if [ "$GIT_COMMITTER_NAME" = "<Old Name>" ];
		GIT_AUTHOR_NAME="<New Name>";
		GIT_AUTHOR_EMAIL="<New Email>";
		git commit-tree "$@";
		git commit-tree "$@";
	fi' HEAD

Ignore changes on tracked files

$ git update-index --assume-unchanged


logo creative common by-sa 3.0 Creative Commons Paternité – Partage à l’Identique 3.0 non transcrit