From Segfault
Jump to navigation Jump to search


Global configuration:[1]

git config --global    "John Doe"
git config --global
git config --global color.pager  false
git config --global pack.threads 0

To send email via git:

git config --global sendemail.smtpserver
git config --global sendemail.smtpserverport 587

Remote repositories

Let's assume we have an existing Github repository:

git clone

After committing some changes, let's publish them too:

git remote set-url origin
git push

To verify:

$ git config --local -l

Or maybe we don't have a repository yet:

mkdir foobar && cd foobar
echo "Hello, world!" >
git add
git commit -m "bla"

git remote add origin

But before we can push to the repository, we need to create it. After generating a Personal Access Token we can use this to create the repository:

curl -u dummy:${TOKEN} -d '{"name":"foobar"}'

Now the repository should be visible on and we can push out our local copy:

git push -u origin master

If the generated token has sufficient rights, we can delete the repository again:[2]

curl -u dummy:${TOKEN} -X DELETE

With the now deprecated [3] password authentication API[4], creating the repository would look like this:

curl -u 'dummy:Passw0rd' --header 'x-github-otp: 123456' -d '{"name":"foobar"}'


gh appears to be the more actively maintained command line interface to Github.[5], the other one being hub.

We need a personal access token, configured with appropriate permissions. The token will be stored in ~/.config/gh/hosts.yml:

$ cat ~/.config/gh/hosts.yml
   oauth_token: '386b076a09e5747f6dcd7cffafe40c88d349a020'
   user: dummy
   git_protocol: ssh


gh repo clone dummy/foobar foobar-git
cd foobar-git

Commit to a new branch:

git checkout -b bugfix-2                                        # Short for: git branch bugfix-2 && git switch bugfix-2
echo "foobar" > 
git add .
git commit -m "another edit"

git push origin -u bugfix-2
gh pr create -t "new PR" -b "This will fix everything"

Merge PR into our master branch:

$ gh pr list
#4  new PR  bugfix-2  about 1 minute ago
$ gh pr merge -d -m 4
✓ Merged pull request #4 (new PR)
✓ Deleted branch bugfix-2 and switched to branch master

Let's try the same for foreign remote repositories, i.e. repositories where we do not have commit access:

gh repo clone someuser/test-repo
cd test-repo
gh repo fork --remote
[edit, commit]

Create a pull request to the original repository:

gh pr create -t "new PR" -b "This will fix everything"

Once the pull request has been opened, we can delete our own fork if we no longer needed it:[6]

$ gh repo delete dummy/test-repo
? Type dummy/test-repo to confirm deletion: dummy/test-repo


With hub installed, we can even create pull requests:

git checkout -b feature-2                                        # Short for: git branch feature-2 && git switch feature-2
echo "foobar" >> 
git add .
git commit -m "another edit"

git push origin -u feature-2
hub pull-request

Note: for this to work we need a personal access token, configured with appropriate permissions. The token will be stored in ~/.config/hub:

$ cat ~/.config/hub
- user: dummy
  oauth_token: 14bf0d4cb5471417ec56ac099b52d18a421ac2e1
  protocol: https

Merge PR into our master branch:

git checkout master 
hub merge
git push

Let's try the same for foreign remote repositories, i.e. repositories where we do not have commit access:

git checkout -b test
[edit, commit]

Fork into a new repository of ours and push out our changes to that new repo:

hub fork --remote-name origin
git push origin test

Create a pull request to the original repository:

hub pull-request

Once the pull request has been opened, we can delete our own fork if we no longer needed it:[7]

hub delete dummy/forked_repo

Calculate checksum of remote repository

$ git ls-remote origin HEAD
a4c6476651ab11eea605fd09e63acd84bed80284        HEAD
a4c6476651ab11eea605fd09e63acd84bed80284        refs/heads/master

Calculate checksum of the local repository:

$ git rev-parse HEAD

Undo a Merge

This is described in detail in the Pro Git book:

git checkout master
git fetch origin                                         # May be needed if there are foreign commits in our tree.
git reset --hard commit-before-the-merge                 # Add origin/master to reset to, well, origin/master

dangling tree

git fsck would complain about a dangling tree:

$ git fsck --full --strict
Checking object directories: 100% (256/256), done.
Checking objects: 100% (400/400), done.
dangling tree 85200871abce3a8aef5136f1221ff0267ecca339
dangling tree 5050f6d17066212c805458709f18e099b921fd59

The tree was working perfectly and there's only one tree in this repo anyway. After git gc only one "dangling tree" was left. Another git prune did the trick, no more dangling trees.


After cloning a local repository, its origin points to the local resource, instead of the remote resource:

$ git clone foo@alice:/usr/local/src/e2fsprogs-git
$ cd e2fsprogs-git
$ git remote -v show
origin  foo@alice:/usr/local/src/e2fsprogs-git (fetch)
origin  foo@alice:/usr/local/src/e2fsprogs-git (push)

Let's see what the original URL was:

alice$ $ git remote -v
origin  git:// (fetch)
origin  git:// (push)

So, let's change our newly created clone to point to the upstream repository:

$ git remote set-url origin git://

Rewrite history

Rewriting history[8] can be done via git amend. Changing the last commit is easy:

git commit --amend

Changing a commit that is farther back is tricky. Let's rewrite something within the last 3 commits:

git rebase -i HEAD~3
> pick f396a45 abc
> pick 8b29ab1 xxx
> pick cd27e14 yyy

Note: the order here is different from e.g. "git log" and f396a45 is the oldest commit!

In order to change the oldest commit, replace the "pick" to "edit" next to f396a45 and save-exit the editor. Now we can rewrite this commit with:

git commit --amend

With that, rebase can continue:

git rebase --continue

Altering commit data

Altering various commit entities can be accomplished with git filter-branch[9]. Let's alter the commit date as an example:

$ git log -n 1  | head -3
commit 27dba1e90110fd1e7b2429c8b17021ba537c471b
Author: John Doe <>
Date:   Sun Feb 22 14:51:54 2015 -0800

But the file we just commited was from a while back:

$ ls -lgo file.c
-rwx------ 1 2767 Feb  5 01:14 file.c

So we want to alter the commit history to match that file date:

D=2015-02-05T01:14:00                                            # See git-commit(1): DATE FORMATS

Let's get the correct date format too:

D=$(date -r file.c +%Y-%m-%dT%H:%M:%S)

Now we can alter the commit:

$ git filter-branch --env-filter "if [ \$GIT_COMMIT = $H ]; then \
    export GIT_AUTHOR_DATE="$D" export GIT_COMMITTER_DATE="$D"; fi"

The commit date has now been changed (and the commit hash too):

$ git log -n 1  | head -3
commit c510521d1668ff8464e91de96d20fc3b6da70c31
Author: John Doe <>
Date:   Thu Feb 5 01:14:00 2015 -0800

The commiter or author can also be changed[10][11] for a single commit:

AN="Me Surname"

git filter-branch --env-filter "if [ \$GIT_COMMIT = $H ]; then \
    export GIT_AUTHOR_NAME="$AN" export GIT_AUTHOR_EMAIL="$AE"; fi"

To rewrite every commit[12] on a branch:

git filter-branch --env-filter "export GIT_AUTHOR_EMAIL=$AE GIT_COMMITTER_EMAIL=$CE" master



Merge commits into one

Multiple commits can be merged[13] like this:

$ git log --pretty=oneline
9ef870fca68ecc80a2f3145ed99ffa3baa5521f1 c
69d2072da98250c5ee9f97359ad582b1e8dceb31 b
5ed10e1c6fc507fcd36955e35d650ee7944700da a

We want to combine commits b & c (the last two commits) into one single commit:

git rebase --interactive HEAD~2
> pick 69d2072 b
> pick 9ef870f c

Change the "pick" to "squash" for the "c" commit, so that it get's combined with its previous commit, b:

> pick 69d2072 b
> squash 9ef870f c

Save & exit from the editor and another editor pops up:

# This is a combination of 2 commits.
# The first commit's message is:
# This is the 2nd commit message:

Edit as needed and save & quit again. Now the history should look like this:

$ git log --pretty=oneline
98f1c6415f98ff9a12a871bc752b678b41c40e13 b and c
5ed10e1c6fc507fcd36955e35d650ee7944700da a

Convert tarballs into git repository

This can be done[14] as such:

git init
tar -xf ~/foo.tar
git add .
git commit -a -s
rm -rf *

Now the next tarball can be extracted and we continue with "git add" again:

git add .
git commit -a -s
rm -rf *

Continue until all tarballs are integrated into the repository.


Which tag is a certain commit part of?

git describe --contains c022a0acad534fd5f5d5f17280f6d4d135e74e81

So, commit c022a0acad534fd5f5d5f17280f6d4d135e74e81 was introduced in v2.6.36-rc1~300^2~1.


To find the commit that introduced a string[15]:

git log -S foo --source path/to/file                      # Use --all to search the whole repository

Search across all branches:[16]

git rev-list --all | xargs git grep "foo"

Repo size without cloning

For Github repos, their API can be used[17]:

$ curl -s | awk '/size/ {print $1, $2, "?B"}'
"size": 481892, ?B

...but we don't know the unit size of this value :-\

CVS to Git

While cvs2git may (or may not)[18] be able to do the job, using git-cvsimport was easier to use and actually worked:

$ cat ~/authorconv
foobar=Foo Bar <>

$ git cvsimport -v -R -d -A ~/authorconv module

This takes quite some time to complete, but in the end:

$ ls -go .git/cvs-*
-rw-r--r--. 1    94 Apr  8 19:55 .git/cvs-authors
-rw-r--r--. 1 68611 Apr  8 20:12 .git/cvs-revisions

$ git log | grep -c ^commit

SVN to Git

Migrating a Subversion repository to Git[19] should be quite straighforward.

Generate the list of authers who commited to the SVN repository:

$ cd ~/project-svn
$ svn log --xml | grep \<author\> | perl -pe 's/.*>(.*?)<.*/$1 = /' | sort -u | tee ~/users.txt
alice = 
bob = 

We have to define mappings for these authors to git-svn can use it:

$ cat ~/users.txt
alice = Alice <>
bob = Bob <>

With ~/users.txt in place, we can import the repository:

mkdir ~/project-git && cd ~/project-git
git svn clone svn:// --authors-file=$HOME/users.txt --no-metadata foobar-git

Now the Git repository should be in place:

$ cd ~/foobar-git
$ git log --max-count=1
commit 8b5d45c20bc9cf490e368bc82f058fb4d9739b2c
Author: Alice <>
Date:   Sat Jan 21 19:24:16 2012 +0000

   fixed something

HTTPS checkout

When checking out over HTTPS:

$ git clone
Cloning into 'tlsdate'...
error: Problem with the SSL CA cert (path? access rights?) while accessing
fatal: HTTP request failed

We may have to install the CA certificates first:

apt-get install ca-certificates

Track remote branches

In an already cloned repository, do:

git remote add foobar

Fetch objects from the new branch, including tags:

git fetch --tags foobar

List all remote branches:

$ git remote 

Update remote branch, locally:

git remote update foobar

Update all remote branches, locally:

git checkout master
git remote update

Show untracked files

git ls-files . --exclude-standard --others

List ignored files, as per .gitignore:

git ls-files . --ignored --exclude-standard --others

Credential store

To save the login credentials of a password protected repository:

$ git remote -v
origin (fetch)
origin (push)

$ git config credential.helper store

Git will ask one more time for credentials, and then store them on the disk:

$ git pull
Username for '': bob
Password for '': ***********
Already up to date.

$ git pull
Already up to date.

$ cat ~/.git-credentials

To prevent saving credentials to disk, we can store them in memory for some time:

$ rm ~/.git-credentials
$ git config credential.helper 'cache --timeout=36000'

Git will ask one more time for credentials, and then store them in memory for 10 hours:

$ git pull
Username for '': bob
Password for '': ***********
Already up to date.

$ git pull
Already up to date.


Remove stale branches[20]:

git fetch --all --prune

Remove local branches no longer present on the remote:

$ ls -1d .git/refs/{heads,remotes}/*

$ git branch -vv | awk '/\[origin.*: gone\]/ {print $1}' | xargs git branch -d

Or, in a more manual fashion:

for x in $(git branch --format='%(refname:short)' | grep -v master); do
   if ! git branch -r | grep -vE 'HEAD|master' | grep -q "origin/${x}"; then
        git branch -D "${x}"