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É
#!/bin/sh
AUTHOR="<name>"
AUTHOR_EMAIL="<mail>"**
COMMITER_NAME="<name>"
COMMITTER_EMAIL="<mail>"
COMMIT="HEAD"
git filter-branch -f --env-filter "GIT_AUTHOR_NAME='${AUTHOR}'; GIT_AUTHOR_EMAIL='${AUTHOR_EMAIL}'; GIT_COMMITER_NAME='${COMMITER_NAME}'; GIT_COMMITTER_EMAIL='${COMMITTER_EMAIL}';" ${COMMIT}
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
[color]
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
[core]
whitespace=fix,-indent-with-non-tab,trailing-space,cr-at-eol
[alias]
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
Reverting
-
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 belowgit revert <rev>
-
re-checkout
<file>
, overwriting any local changesgit checkout <file>
-
re-checkout all files, overwriting any local changes. This is most similar to
"svn revert"
if you’re used to Subversion commands$ 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.
$ 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.git reset --hard ORIG_HEAD
or
$ 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.
$ git reset --soft HEAD^
-
redo previous commit, including changes you’ve staged in the meantime. Also used to edit commit message of previous commit.
$ git commit --amend
Stashing
-
save your local modifications to a new stash (so you can for example
"git svn rebase"
or"git pull"
)$ git stash git stash save <optional-name>
-
restore the changes recorded in the stash on top of the current working tree state
$ git stash apply
-
restore the changes from the most recent stash, and remove it from the stack of stashed changes
$ git stash pop
Remotes
-
prune deleted remote-tracking branches from “git branch -r” listing
git remote prune <remote>
-
add a remote and track its master
$ git remote add -t master -m master origin git://example.net/git.git/
-
show information about the remote server.
git remote show <remote>
-
Track a remote branch as a local branch.
git checkout -b <local branch> <remote>/<remote branch>
Example:
$ git checkout -b myfeature origin/myfeature
Submodules
-
add the given repository at the given path. The addition will be part of the next commit.
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.git submodule update [--init]
-
Executes the given command within each checked out submodule.
git submodule foreach <command>
Removing submodules
- delete the relevant line from the
.gitmodules
file. - delete the relevant section from
.git/config
. - run
git rm --cached path_to_submodule
(no trailing slash). - commit and delete the now untracked submodule files.
Updating submodules
To update a submodule to a new commit:
-
update submodule:
$ cd <path to submodule> $ git pull
-
commit the new version of submodule:
$ cd <path to toplevel> $ git commit -m "update submodule version"
-
check that the submodule has the correct version
$ 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.
Archive
-
Will export expanded tree as tar archive at given path
$ git archive master | tar -x -C /somewhere/else
-
Will export archive as bz2
$ git archive master | bzip2 > source-tree.tar.bz2
-
Will export as zip
$ git archive --format zip --output /full/path master
Git Instaweb
-
gestion
git instaweb --httpd=webrick [--start | --stop | --restart]
Environment Variables
-
Your full name to be recorded in any newly created commits. Overrides user.name in
.git/config
GIT_AUTHOR_NAME, GIT_COMMITTER_NAME
-
Your email address to be recorded in any newly created commits. Overrides user.email in
.git/config
GIT_AUTHOR_EMAIL, GIT_COMMITTER_EMAIL
-
Location of the repository to use (for out of working directory repositories)
GIT_DIR
-
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.
GIT_WORKING_TREE
Changing history
Change author for all commits with given name
$ git filter-branch --commit-filter '
if [ "$GIT_COMMITTER_NAME" = "<Old Name>" ];
then
GIT_COMMITTER_NAME="<New Name>";
GIT_AUTHOR_NAME="<New Name>";
GIT_COMMITTER_EMAIL="<New Email>";
GIT_AUTHOR_EMAIL="<New Email>";
git commit-tree "$@";
else
git commit-tree "$@";
fi' HEAD
Ignore changes on tracked files
$ git update-index --assume-unchanged
Licence
Creative Commons Paternité – Partage à l’Identique 3.0 non transcrit