Configuration

git config --global user.name "John Doe"
git config --global user.email "johndoe@bidon.ca"
git config --list

Remotes

Remotes are other git repositories. Typically, we have our main remote git server, and sometimes we also track an upstream git repository.

List remotes:

git remote -v

Add a remote:

git remote add origin git@lab.example.org:extensions/test.git

Remove a remote (this has no impact whatsover locally, it can be later re-added):

git remote rm origin

Rename a remote:

git remote rename origin upstream

After adding a remote, fetch changes to be able to see all branches and tags (this does not have any impact locally, unlike a ‘git pull’):

git fetch --all

Diff, log

git show [hash]
git show [tag]

Only for a given file:

git show [hash]  path/to/file

Show revision tree:

git log --all --graph --decorate --oneline

Branches

Checkout a specific branch:

git checkout name-of-branch

Create a new branch:

git checkout -b name-of-branch

Push that branch to a remote:

git push origin name-of-branch

From there, we can do a merge-request (Gitlab) or pull-request (Github) to upstream.

Patches (not really useful when using Gitlab/Github)

Check if it applies cleanly:

git apply --check /tmp/foo.patch

Apply the patch:

git apply /tmp/foo.patch

For drupal.org, attribute a commit to the patch author (replace with correct ‘username’ and numeric user ID):

git commit --author="username <username@1234567.no-reply.drupal.org>"

If the patch was created by “format-patch” (contains author information):

git am /tmp/foo.patch

More information: [http://progit.org/book/ch5-3.html](Pro Git: Maintaining a Project), page 130

Hacks

Merge a git repository in a sub-directory

In weird cases where you have your main repository and want to import a sub-repo into your main one:

cd ./other/repo/
mkdir templates
git mv CRM templates/
git commit -m "moving to templates subdir" -a
git filter-branch --index-filter \
'git ls-files -s | sed "s-CRM/-templates/CRM/-" | GIT_INDEX_FILE=$GIT_INDEX_FILE.new \
git update-index --index-info &&
mv $GIT_INDEX_FILE.new $GIT_INDEX_FILE' HEAD

Then move back to the root of your main git repository and pull:

cd ../../
git pull

The delete the .git in “other/repo/.git”.

See: http://stackoverflow.com/questions/614229/can-i-move-the-git-directory-for-a-repo-to-its-parent-directory

Books