Outils pour utilisateurs

Outils du site

Panneau latéral




Ham Radio






Git CheatSheet

A Quick Git reminder. This will grow as I go.


Teaching Git Who It's Dealing With

$ <in>git config --global user.name "Olivier Mehani"</in>
$ <in>git config --global user.email me@example.com</in>

Caching Passwords in Memory

$ <in>git config --global credential.helper cache</in>

Coloured Output

$ <in>git config --global color.ui auto</in>

More Insight When Resolving Merges

As explained here, a diff3 style for conflicts shows both parents, thus allowing to understand the conflict and fix it better. Also, vimdiff is good.

$ <in>git config --global merge.conflictstyle diff3</in>
$ <in>git config --global merge.tool vimdiff</in>

List Tags Containing a Specific Commit

Using git tag.

$ <in>git tag --contains COMMIT</in>
[tag list]

New Branch Without History

The easiest (i.e. almost one line) solution is

$ <in>git checkout --orphan $NEWBRANCH; git rm -rf .</in>

Another, more tedious way of doing it, by pointing HEAD to a non-existing branch, and removing the current index so nothing is staged yet. As seen here.

$ <in>git symbolic-ref HEAD refs/heads/$NEWBRANCH</in>
$ <in>rm .git/index</in>

Find Content in the Whole Repo and History

As seen here:

$ <in>git grep <regexp> $(git rev-list --all)</in>

Remove Content from Repo

Rewriting History

Using git filter-branch(1), or BFG. Also, see here.

$ <in>git filter-branch --force --prune-empty --tree-filter "rm -f UNWANTEDFILE"</in>
$ <in>git reflog expire --expire=all</in> # Forget references to previous checkouts
$ <in></in>

Removing blobs

As seen here:

You can remove the file objects. Just unpack any packs and rm .git/objects/12/345… Use git show to walk from > commit to tree to file.

git fsck will complain about it forever, of course.

Alternatively, wait for sha1 to be fully broken…

Merge a Branch as a Subdirectory of Another

As explained in git-filter-branch(1), via Zrajm.

$ <in>SUBDIR_NAME="the\/subdir"</in>
$ <in>BRANCH_NAME=newbranch</in>
$ <in>git filter-branch --index-filter \
    'git ls-files -s | \
        sed "s-\t-&'"$SUBDIR_NAME"'/-" | \
        GIT_INDEX_FILE=$GIT_INDEX_FILE.new git update-index --index-info && \
    ' "$BRANCH_NAME"</in>

Copy Subset of Files with History

Using git-format-patch and git-am, adapted from here.

src$ <in>export PATHS=dir1/ dir2/file</in>
src$ <in>git format-patch $(git log --oneline $PATHS | sed 's/ .*//' | tail -n 1)^..HEAD $PATHS</in>
src$ <in>cd $DST</in>
dst$ <in>git am $SRC/*.patch</in>

If paths are clean subdirectories, git subtree can be of use.

SSL errors

In case of

 Cloning into 'latex-makefile'...
 fatal: unable to access 'https://scm.narf.ssji.net/git/latex-makefile/': SSL certificate problem: Invalid certificate chain
 Clone of 'https://scm.narf.ssji.net/git/latex-makefile/' into submodule path 'latex-makefile' failed
 git config http.sslVerify false

should have worked, but

 GIT_SSL_NO_VERIFY=true git [clone|submodule...]

definitely did.


(Re)create per-repo SSH keys

As discussed here.

Add ssh_config entry

$ <in>export GASERVER=server</in>
$ <in>export GAUSER=user</in>
$ <in>export GAPATH=/path</in>
$ <in>export GASANEPATH=${GAPATH//\//.2F}</in>
$ <in>ssh-keygen -t rsa -f  ~/.ssh/git-annex/key.git-annex-$GASSHHOSTNAME</in>
$ <in>cat << EOF >> ~/.ssh/config
# Added manually for git-annex
Host git-annex-$GASSHHOSTNAME
  Hostname $GASERVER
  IdentityFile ~/.ssh/git-annex/key.git-annex-$GASSHHOSTNAME
  IdentitiesOnly yes
  StrictHostKeyChecking yes
$ <in>ssh-copy-id -i ~/.ssh/git-annex/key.git-annex-$GASSHHOSTNAME $GAUSER@$GASERVER</in>
$ <in>git remote add ${GASERVER/.*/} ssh://${GAUSER}@git-annex-${GASSHHOSTNAME}:${GAPATH}</in>
$ <in>git config remote.${GASERVER/.*/}.annex-ignore false</in>

Configure server

After the 'ssh-copy-id stage', the key can be used to get a full session. This needs to be limited on the server, by prepending the following to the newly added key in '.ssh/authorized_keys', replacing 'GAPATH' by the value of '$GAPATH':

command="GIT_ANNEX_SHELL_DIRECTORY='GAPATH' ~/.ssh/git-annex-shell",no-agent-forwarding,no-port-forwarding,no-X11-forwarding,no-pty ssh-rsa...

Reinstall the ssh_config entries

In case the file get overwritten, but the keys are still there.

$ <in>for KEY in ~/.ssh/git-annex/*.pub; do
    export KEY=${KEY/.pub/}
    export GASSHHOSTNAME=${KEY/*git-annex-/}
    grep -q $GASSHHOSTNAME ~/.ssh/config && continue
    export GASERVER=`echo ${GASSHHOSTNAME} | sed 's/-[a-zA-Z0-9]\+_.*//'`
    export GAUSER=${GAUSERPATH/_*/}
    export GAPATH=${GAUSERPATH/*_/}
    export GAPATH=${GAPATH//.2F//}
    cat << EOF >> ~/.ssh/config
Host git-annex-$GASSHHOSTNAME
  Hostname $GASERVER
  IdentityFile ~/.ssh/git-annex/key.git-annex-$GASSHHOSTNAME
  IdentitiesOnly yes
  StrictHostKeyChecking yes

git-annex-metadata shortcuts

        ameta = annex metadata
        ametalist = !git annex metadata | sed -n 's/.*\\s\\([^\\s]\\+=.*\\)/\\1/p' | sort | uniq
tips/git.txt · Dernière modification: 2016-05-11 10:47 de shtrom