Git: Creating and Importing Patches

Git allows you to create patch files that may be email or transfered in other ways the imported into a repository from outside the git chain.

Creating a patch

To create a patch file of the last commit

git format-patch -1
gzip 0001-name_of_patch_file.patch
Patching a file or directory
gzip -dc 0001-name_of_patch_file.patch.gz |git apply

Git: Allow remote pushes to a checked out repository

In the remote repository you are planing on pushing to, run the following:

git config receive.denycurrentbranch ignore

Then download the and unzip it. Now copy this file to your .git/hooks/ folder and make it executable.

rm -rf
chmod 775 post-update
mv post-update .git/hooks/post-update

You should now be able to remotely push to this repository without errors.

Git: Creating an unattached branch with no history

From inside your git repository, after you have committed all you changes, run:

git symbolic-ref HEAD refs/heads/name-of-new-branch
rm .git/index
git clean -fdx

You will now have an empty directory waiting for your first commit.

So if your creating a new branch for github pages for example, you would run:

git symbolic-ref HEAD refs/heads/gh-pages
rm -f .git/index
git clean -fdx

And your ready to start creating your page.

Installing the git, cgit web interface on a Fedora

cgit is fast web interface for the git. cgit has built a cache and is compiled in c so it’s very quick.

To start out, download the current version of cgit via git

git clone git://

Next you need to setup the git submodule

cd cgit
git submodule init
git submodule update

Once your done, run get-git

make get-git

Then compile the software


And install it

make install

After you have installed cgit, you will need to setup Apache to run cgit. This is pretty easy, just edit your /etc/httpd/conf/httpd.conf file and add the following for the site you wish to run cgit on.

<Directory "/var/www/">
      AllowOverride None
      Options ExecCGI
      Order allow,deny
      Allow from all

Once your done setting up cgit in Apache, you may configure cgit by creating a cgitrc file at /etc/cgitrc. Below is the example config file.


# Enable caching of up to 1000 output entriess

# Specify some default clone prefixes
clone-prefix=git:// ssh://

# Specify the css url

# Show extra links for each repository on the index page

# Show number of affected files per commit on the log pages

# Show number of added/removed lines per commit on the log pages

# Add a cgit favicon

# Use a custom logo

# Enable statistics per week, month and quarter

# Set the title and heading of the repository index page git repositories

# Set a subheading for the repository index page
root-desc=tracking the foobar development

# Include some more info about on the index page

# Allow download of tar.gz, tar.bz2 and zip-files
snapshots=tar.gz tar.bz2 zip

## List of common mimetypes


## List of repositories.
## PS: Any repositories listed when section is unset will not be
##     displayed under a section heading
## PPS: This list could be kept in a different file (e.g. '/etc/cgitrepos')
##      and included like this:
##        include=/etc/cgitrepos

repo.desc=the master foo repository

repo.desc=the bars for your foo

# The next repositories will be displayed under the 'extras' heading

repo.desc=a set of extensions for bar users

repo.desc=the wizard of foo

# Add some mirrored repositories

repo.desc=the dscm

repo.desc=the kernel

# Disable adhoc downloads of this repo

# Disable line-counts for this repo
repo.enable-log-linecount=0# Restrict the max statistics period for this repo

Installing the GIT Daemon for Read Only Access to Repoistory

The idea here is to allow anyone to download your GIT repository using the native git protocol.  This is similar to the post I wrote about Creating a secure Git repository server, but the below method allows full read only public access to the repositories.

Start out by modifying the new file git-daemon.

vim /etc/xinetd.d/git-daemon

Add the below text to the file.

# description: The git daemon offers anonymous access to git repositories
service git
    disable        = no
    type           = UNLISTED
    port           = 9418
    socket_type    = stream
    wait           = no
    user           = git
    server         = /usr/libexec/git-core/git-daemon
    server_args    = --inetd --export-all --base-path=/var/git
    log_on_failure += USERID

You may need to change the location of the git-daemon (the above example is from Fedora) and you will need to update the location of your git repository directory.

The above configuration will share ALL the git repositories in the /var/git directory.

If you do not wish for the all repositories to be public, you may remove the --export-all flag and add a empty file named git-daemon-export-ok to the git repository you wish to still share.

Git Cheat Sheet


Git Global Setup

 git config --global "Your Name Here"
 git config --global

Git Commands

Common Git Commands

To download a new git repository

(Public access)

git clone

Update an exciting repository

(From within the projects folder)

git pull

Comment a change

git comment file.txt

To Revert a comment

git revert ce963c9db00c25b3c1e6add1fe6032aef61a5bed


Modify the last commit

git commit --amend


Creating a patch

git diff file_with_changes > 0001-name_of_patch_file.patch
gzip 0001-name_of_patch_file.patch

Patching a file or directory

gzip -dc 0001-name_of_patch_file.patch.gz |patch -p1


Creating a new branch

git branch <branch>

or to create a branch and switch to it

git checkout -b <branch>

Pulling changes from branches

git checkout <branch_committing_to>

Now that your in the branch you want to add the changes to, run:

git pull . <branch_committing_from>

Changing to a different branch

git checkout <branch>

Deleting a branch

git branch -d <branch>

Deleting a remote branch

git push origin :<branch>

Pushing a branch to github

git push <remote_repository_name> <branch_name>

So that would be…

git push origin <branch>

Display branches on github

git remote show origin

Pulling a branch from github

git checkout --track -b <name_of_local_branch> origin/<name_of_remote_branch>


Tagging a branch

git tag -a -m "tagging version 1.0" 1.0

Pushing the tag to github

git push --tags

Deleting a tag

git tag -d 1.0

Removing a deleted tag from github

git push origin :refs/tags/1.0


To update existing submodules

git pull
git submodule init
git submodule update

To add a new submodule to a project

git submodule add <remote-host>:<project.git> <project.git>

So if you are adding project “program” from the example git server you will run

git submodule add git:// program

Creating an secure Git remote server

On the Server create the git repository

mkdir name-of-git-repo.git
cd name-of-git-repo.git
git init --bare

On the Client System create the git repo to import into the server

git init
touch README
git add README
git commit -m 'first commit'
git remote add origin git@REMOTE_SERVER:name-of-git-repo.git
git push origin master

Importing an SVN repo into git

To import a svn repo create a new git repo and run

git svn clone
git commit

Git Links

Run a Secure git Repository on FreeNAS

Running a secure git repository on FreeNAS is pretty straight forward, once you understand what your trying to do.  If you have looked over my previous post “Creating a secure Git repository server” you understand that all you really need to do is connect to the git repository via ssh/ssl and copy back what you need.  The hardest part of using FreeNAS is creating the keys.

To start out, you need to create a user account on the FreeNAS system.  This will be a generic account that everyone who has write access will use.  You may also create a account for each person, and grant each of them access to the central repository.

After you have your account, follow my post on “Enable SSH Key Authorization on FreeNAS” to copy over the SSL key and setup the account.  Once you are able to log in as your FreeNAS git user, you may follow my previous post “Creating a secure Git repository server” to setup the git repository.