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

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

Caching Passwords in Memory

$ git config --global credential.helper cache

Coloured Output

$ git config --global color.ui auto

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.

$ git config --global merge.conflictstyle diff3
$ git config --global merge.tool vimdiff

List Tags Containing a Specific Commit

Using git tag.

$ git tag --contains COMMIT
[tag list]

New Branch Without History

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

$ git checkout --orphan $NEWBRANCH; git rm -rf .

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.

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

Find Content in the Whole Repo and History

As seen here:

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

Remove Content from Repo

Rewriting History

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

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

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.

$ SUBDIR_NAME="the\/subdir"
$ BRANCH_NAME=newbranch
$ 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"

Copy Subset of Files with History

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

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

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

$ export GASERVER=server
$ export GAUSER=user
$ export GAPATH=/path
$ export GASANEPATH=${GAPATH//\//.2F}
$ ssh-keygen -t rsa -f  ~/.ssh/git-annex/key.git-annex-$GASSHHOSTNAME
$ 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
$ ssh-copy-id -i ~/.ssh/git-annex/key.git-annex-$GASSHHOSTNAME $GAUSER@$GASERVER
$ git remote add ${GASERVER/.*/} ssh://${GAUSER}@git-annex-${GASSHHOSTNAME}:${GAPATH}
$ git config remote.${GASERVER/.*/}.annex-ignore false

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.

$ 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 par shtrom