Skip to content

Instantly share code, notes, and snippets.

@llafuente
Last active October 22, 2015 10:06
Show Gist options
  • Save llafuente/9270237 to your computer and use it in GitHub Desktop.
Save llafuente/9270237 to your computer and use it in GitHub Desktop.
Git diary, tutorial & config
[color]
ui = auto
[core]
editor = vim
autocrlf = input
fileMode = false
[merge]
tool = diffmerge
ff = true
[alias]
co = checkout
br = branch
ci = commit
st = status
df = difftool -y
unstage = reset HEAD --
last = log -1 HEAD
mergebranch = merge --no-ff
y = diff "@{yesterday}"
ls = log --oneline --decorate
lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative --all
lgn = "!f() { git log --graph --pretty=format:'%Cred%H%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --date=relative $@ | git name-rev --stdin --name-only ; } ; f"
ahead = log origin/master..HEAD --oneline
stu = "!sh -c \"git fetch origin; git status\""
mg = merge --ff-only
rmall = !sh -c \"git diff --diff-filter=D --name-only -z | xargs -0 git rm\"
new = !sh -c 'git log $1@{1}..$1@{0} "$@"'
sortdiff = !sh -c 'git diff "$@" | grep "^[+-]" | sort --key=1.2 | uniq -u -s1'
pack = !sh -c 'git archive --format=tar.gz -o git log $1@{1}..$1@{0} "$@"'
changes = log --name-status --oneline live/master..HEAD
cat = !sh -c \"git show $2:$1\"
ap = apply --index
pa = format-patch --stdout
update-library = merge --squash -s subtree --no-commit
[push]
default = upstream
[branch]
#remove if git flow
autosetuprebase = always
[diff]
guitool = kdiff3
tool = kdiff3
renames = copies
[mergetool]
keepBackup = false
[difftool "diffmerge"]
cmd = diffmerge $LOCAL $REMOTE
[mergetool "diffmerge"]
cmd = diffmerge --merge -t1=\"Existing file in tree\" -t2=\"Base and result file\" -t3=\"New changes to apply\" --result=$MERGED $LOCAL $BASE $REMOTE
[difftool "kdiff3"]
cmd = /usr/bin/kdiff3 $BASE $LOCAL $REMOTE -o $MERGED
keepBackup = false
trustExitCode = false
#Loop all subfolder and do a git status, usefull when you have many repos (even softlinked) :)
for i in $(ls -d */);
do
pushd .
echo "ENTER ${i}"
echo
cd ${i}
git status
popd
done
// fix the history of all github projects renaming users...
var request = require('request');
var d = new Date();
d.setTime(d.getTime() - (1000 * 60 * 60 * 24 * 90)); // everything since 3 months
request({
url: 'https://api.github.com/users/llafuente/repos',
headers: {
'User-Agent': 'Awesome-Octocat-App'
}
}, function (error, response, body) {
body = JSON.parse(body);
var repos = [];
body.forEach(function(repo) {
if (repo.owner.login == "llafuente" && repo.fork == false && d.getTime() < new Date(repo.updated_at).getTime()) {
repos.push([repo.name, repo.ssh_url]);
}
});
console.log(repos);
require("async").each(repos, function(repo, done) {
console.log(repo);
require('child_process').exec("git clone --bare " + repo[1], function(error, stdout, stderr) {
require('child_process').exec("/bin/sh history-fix.sh " + repo[0], function(error, stdout, stderr) {
done();
});
});
});
})
#!/bin/sh
cd $1
git filter-branch --env-filter '
OLD_EMAIL="???"
CORRECT_NAME="llafuente"
CORRECT_EMAIL="???"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_COMMITTER_NAME="$CORRECT_NAME"
export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_AUTHOR_NAME="$CORRECT_NAME"
export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags
git push --force --tags origin 'refs/heads/*'
# alternative
git filter-branch --commit-filter '
if [ "$GIT_AUTHOR_EMAIL" = "vagrant@localhost.localdomain" ];
then
GIT_AUTHOR_NAME="<name>";
GIT_AUTHOR_EMAIL="<email>";
git commit-tree "$@";
else
git commit-tree "$@";
fi' HEAD
git update-ref -d refs/original/refs/heads/master

Log / show

Display all changes from file/folder

git log -p <file-or-folder>

Change file modes to git ones.

git diff --summary | grep --color 'mode change 100755 => 100644' | cut -d' ' -f7- | xargs -d'\n' chmod +x
git diff --summary | grep --color 'mode change 100644 => 100755' | cut -d' ' -f7- | xargs -d'\n' chmod -x

BRANCHING

Create branches

git br <name>
git co <name>

Sometime branch do not track exacly the origin/branch, use tracking.

git br -t develop origin/develop

Delete branch

#local
git br -d <branch>
#remote
git push --delete origin <branch>

clear if invalid

git fetch --prune origin

reallocate/move branch pointer to different commit

git update-ref -m "<branch> to <commit>" refs/heads/<branch> <commit>
# or
git branch -f <branch> <commit>

BISECT

git bisect start

git bisect good <revision>
git bisect bad <revision>

#mark the current revision as good/bad until the end


#finish the bisect
git bisect end

Stash

git stash # create stash
git stash save "message" # create stash with a message

git stash list # list stash

git show stash@{0} # show changes from a stash

git stash pop # pop the stash

PATCHES

Create a single commit patch

git format-patch -1 <sha>

Create a range of commit patch

# X is a number!
git format-patch -X <sha> --stdout > X-last-commits.patch

Create a patch from two revisions

git diff <sha1>..<sha2> > diff.patch

Apply a patch

patch -p1 < file.patch

After apply remember to commit :)

TAGS

Remove tag

TAG=xxxx
git tag -d ${TAG}
git push origin :refs/tags/${TAG}

Move tag

TAG=xxxx
git tag -f -a ${TAG}
git push -f --tags

History operations

Fix last commit, adding new changes / change message

git commit --amend

Reset/revert last commit and put it in the working copy

git reset --soft HEAD~1

rewrite author & email

Rebase and conflicts

Put branch1 above branch2

git co branch1
git rebase branch2

Resolve collisions

git co --theirs <file> # branch1/<file> is the good one
git co --ours <file> # branch2/<file> is the good one

When status is clean: git rebase --continue If the current commit is not needed: git rebase --skip Call Houston otherwise: git rebase --abort

Resolve all conflicts at once

for FILE in `git status | grep "both modified" | awk '{print $4;}'`
do
    echo $FILE
    git co --theirs $FILE # or --ours
    git add $FILE
done

Push time.

git push

# it may happen that...
# ! [rejected]        XXX -> XXX (non-fast-forward)
# do
git push --delete origin branch1
git push -u origin branch1

Sync Fork

When you see in github This branch is x commits behind xx:xx

git remote add upstream https://github.com/xxxx
git fetch upstream
git checkout <branch>
git merge upstream/<branch>

Display Merge diff

git show sha1

commit 0e1329e551a5700614a2a34d8101e92fd9f2cad6 (HEAD, master)

Merge: fc17405 ee2de56

git diff ee2de56..fc17405
git diff --name-only ee2de56..fc17405

History. Edit an incorrect commit message

git rebase -i HEAD~<number>

Change pick to reword, :wq, edit each message and save.

History. Edit an incorrect commit message with a pattern

Use sed/regex

git filter-branch -f --msg-filter "sed 's/xxx/yyy/'" <commit>..HEAD

git flow init

#maybe needed, depens on how the repo is cloned
git br -t develop origin/develop
git br -t master origin/master
git flow init
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment