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
Advertisements

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 Post-update.zip and unzip it. Now copy this file to your .git/hooks/ folder and make it executable.

wget http://wiki.mattrude.com/images/d/de/Post-update.zip
unzip Post-update.zip
rm -rf Post-update.zip
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://hjemli.net/pub/git/cgit

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

make

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/code.mattrude.com/">
      AllowOverride None
      Options ExecCGI
      Order allow,deny
      Allow from all
</Directory>

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.

EXAMPLE CGITRC FILE
-------------------

# Enable caching of up to 1000 output entriess
cache-size=1000

# Specify some default clone prefixes
clone-prefix=git://foobar.com ssh://foobar.com/pub/git http://foobar.com/git

# Specify the css url
css=/css/cgit.css

# Show extra links for each repository on the index page
enable-index-links=1

# Show number of affected files per commit on the log pages
enable-log-filecount=1

# Show number of added/removed lines per commit on the log pages
enable-log-linecount=1

# Add a cgit favicon
favicon=/favicon.ico

# Use a custom logo
logo=/img/mylogo.png

# Enable statistics per week, month and quarter
max-stats=quarter

# Set the title and heading of the repository index page
root-title=foobar.com git repositories

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

# Include some more info about foobar.com on the index page
root-readme=/var/www/htdocs/about.html

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

##
## List of common mimetypes
##

mimetype.git=image/git
mimetype.html=text/html
mimetype.jpg=image/jpeg
mimetype.jpeg=image/jpeg
mimetype.pdf=application/pdf
mimetype.png=image/png
mimetype.svg=image/svg+xml

##
## 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.url=foo
repo.path=/pub/git/foo.git
repo.desc=the master foo repository
repo.owner=fooman@foobar.com
repo.readme=info/web/about.html

repo.url=bar
repo.path=/pub/git/bar.git
repo.desc=the bars for your foo
repo.owner=barman@foobar.com
repo.readme=info/web/about.html

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

repo.url=baz
repo.path=/pub/git/baz.git
repo.desc=a set of extensions for bar users

repo.url=wiz
repo.path=/pub/git/wiz.git
repo.desc=the wizard of foo

# Add some mirrored repositories
section=mirrors

repo.url=git
repo.path=/pub/git/git.git
repo.desc=the dscm

repo.url=linux
repo.path=/pub/git/linux.git
repo.desc=the kernel

# Disable adhoc downloads of this repo
repo.snapshots=0

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

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

Aside

Git Global Setup

 git config --global user.name "Your Name Here"
 git config --global user.email you@example.com

Git Commands

Common Git Commands

To download a new git repository

(Public access)

git clone http://git.example.com/folder/project.git

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

Commit

Modify the last commit

git commit --amend

Patches

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

Branching

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

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

Submodules

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://example.com/program.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 https://svn.foo.com/svn/proj
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.