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"}'


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: 84e0c3e543eac7d283507d1537d23e70dc1d6335
  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:[5]

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[6] 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[7]. 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[8][9] 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[10] 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[11] 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[12] 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[13]:

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

Repo size without cloning

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

$ 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)[15] 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[16] 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.


Update a local repository:

hg pull
hg update                                             # Or use -u at the pull command

Delete a branch:

$ hg update -C badbranch                              # update & switch to badbranch
$ hg branches
default                   2922:c7fa97debb36
badbranch                 2924:f58b9c023d3b
$ hg commit --user "joe" --close-branch -m "bogus branch"
$ hg update -C default                                # switch back to the default branch again.

View local changes

hg status

Delete everything but .hg* directories, then checkout a clean copy again

ls -A | grep -v .hg | xargs rm -rf
hg checkout -C

There's also the Purge Extension. Although it's distributed along with Mercurial, it must be activated:

$ cat ~/.hgrc

Now hg purge can be used to get rid of untracked objects.


Bazaar seems to be tightly coupled to Launchpad. Checkout out source code requires magical URIs, e.g.:

$ bzr branch lp:gwibber
$ cat gwibber/.bzr/branch/branch.conf 
parent_location =

Clean untracked files can be done with Bzrtools:

$ sudo apt-get install bzrtools
$ touch foo bar
$ bzr clean-tree --dry-run
deleting paths:

$ bzr clean-tree --force


Checkout a module of a remote repository:

cvs -d co openssl

Update the repository:

cvs update

List all tags in the repository:

cvs -q log -h | awk -F"[.:]" '/^\t/ && $(NF-1)!=0 {print $1}' | sort -u

List all branches in the repository:

cvs -q log -h | awk -F"[.:]" '/^\t/ && $(NF-1)==0 {print $1}' | sort -u

Setting up a CVS server


 virtuser:pwcrypt:unixuser unixuser:pwcrypt anonymous::unixuser (no password)
 xinetd user = cvs passenv =
 PAM, if compiled with --enable-pam /etc/pam.d/cvs @include common-auth @include common-account
 no $CVSROOT/CVRSROOT/passwd !
 xinetd user = root passenv =
 client: CVSROOT=:pserver:evil:password@sheep:/var/lib/cvs cvs login cvs co . cvs ls -Rl ./


branches & tags

List all branches of a repository by listing the /branches URL. This is often below the same level as /trunk


To change the repository URL, use "switch"[18]:


Adding an external repository to an SVN project-directory. For example, adding the WP-reCAPTCHA repo to the Wordpress source tree:

$ cd wp-content/plugins
$ svn propedit svn:externals .
  > wp-recaptcha
  > :x
$ svn update
Fetching external item into 'wp-recaptcha'
External at revision 255348.

To add the externals to the SVN root, use the relative path instead:

$ cd wordpress-svn
$ svn propedit svn:externals .
  > wp-content/plugins/wp-recaptcha
  > :x
$ svn update