Git

    HOW-TOs use:

    - http://gitimmersion.com/
    - https://learngitbranching.js.org/
    - https://try.github.io
    

    Configure

    git config --global user.name "Laurent Parmentier"
    git config --global user.email laubosslink@society-lbl.com
    

    Disable SSL check

    git config http.sslVerify "false"
    

    Note: Disable https non-valide certificate check for example

    Do not consider rights (chmod)

    git config core.fileMode false
    

    Note: Don't consider chmod of file. Which sometime caused a commit with all files of repo..

    Flow

    Git flow

    Branches

    Here is a good example of how to use branch in a development projet.

    Git branches

    See also Interesting Command: git-flow.

    Diffs

    Examples:

    # changes between branches
    git diff master..contact-form
    
    # changes between commits
    git diff 0023cdd..fcd6199
    

    Git diffs

    Git submodule

    This functionnality permits to have a git project in an other. To know more about see the official documentation here.

    Add a submodule

    It permit to add the external git project to the current.

    git submodule add ssh://user@sub.domain.tld:port/project.git directory

    Update a submodule

    git submodule update <path>

    if you have a lot of submodule, you could also update all in one time :

    git submodule foreach git pull remote branch

    Notice: It could happens problem if there is different key in submodule project. You will need to load all different private keys.

    Git crypt

    • Install: git-crypt init
    • Add gpg pub: git-crypt add-gpg-user USER_ID
    • Configure (.gitattributes): secretfile filter=git-crypt diff=git-crypt
    • Unlock: git-crypt unlock
    • List users: find .git-crypt/ -name *.gpg -exec sh -c 'basename {} | cut -d\. -f1 | xargs gpg --list-key' \;

    source

    Clone

    Permit to retrieve a repository.

    git clone ssh://user@sub.domain.tld:port/repo.git

    Choosing private key

    Choosing private key without ssh-agent((usefull on server)).

    Based on http://superuser.com/a/912281 and http://superuser.com/a/920849

    If you have git v2.3+:

    	GIT_SSH_COMMAND="ssh -i ~/.ssh/id_rsa_example" git clone example
    

    Else you need to create a script:

    /usr/local/bin/ssh_for_git

    #!/bin/sh
    
    ssh -i "/home/user/.ssh/id_rsa2" $@
    

    Note: I've try to use shift, and retrieve id_rsa from parameter $1 but does not work.

    export GIT_SSH=/usr/local/bin/ssh_for_git
    

    Revert/Rebase

    TODO study..

    • http://serebrov.github.io/html/2014-01-04-git-revert-multiple-recent-comments.html
    • https://git-scm.com/blog/2010/03/02/undoing-merges.html
    • https://git-scm.com/book/en/v2/Git-Branching-Rebasing

    Revert one merge:

    git revert -m 1 SHA_MERGE
    

    Trick

    Cleanest way to resolve conflict

    git mergetool
    

    See: http://git-scm.com/docs/git-mergetool

    Undo modification of one file (current working / no commit)

    git checkout -- FILE
    

    Create empty branch

    git checkout --orphan NEWBRANCH
    

    Retrieve files from another branch

    Remark: will override the current one if exists

    git checkout FROM_BRANCH -- PATH
    
    git show BRANCH:PATH
    

    Retrieve specific commit and apply

    git cherry-pick origin/master HASH_COMMIT
    

    Create a patch file for a commit

    git format-patch -1 HASH_COMMIT
    

    Note: -1 represent the last 1((just himself)) patches from HASH_COMMIT

    Last date that a file has been changed through a commit

    git log -1 --pretty="format:%ci" PATH
    

    Presence of file in commits

    git log --folow -- PATH
    

    Summary of changements in a commit

    git diff --name-status HASH_COMMIT
    

    Signed all existing commits

    git rebase --exec 'git commit --amend --no-edit -n -S' -i development
    

    source

    Delete local branch that do not exists remotely

    Use with caution, you could delete local work.

    git fetch --prune
    git branch -r | awk '{print $1}' | egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) | awk '{print $1}' | xargs git branch -d
    

    source

    Rebase all from the beginning

    git rebase -i --root
    

    source

    Templates of gitignore

    Project: https://github.com/github/gitignore

    This project contains a lot of template of gitignore adapt for different project((joomla, wordpress, ...))

    Retrieve files/directory from a repo

    git archive --remote=ssh://user@dmain.tld/repo.git HEAD | tar xvf - readme.md
    

    Note: does not work with Github

    Stash a file

    git stash push PATH
    

    Note: use --patch for an interactive mode (with multiple files)

    Change a file to an older commit

    git stash
    git rebase -i <old_commit-1>
    # put `edit` to the commit you want to change
    git stash apply
    git add <files to include>
    git commit --amend --no-edit
    git rebase --continue
    

    source

    Point a branch to a specific commit

    git checkout <branch>
    git reset --hard <commit>
    

    source