Skip to content. | Skip to navigation

Sections
Personal tools
What is this?
Hi, my name is Tom Lazar and I'm a Plone and Zope developer based in Berlin, Germany and this is my personal and professional (no big difference, really...) website.
 

git

Apr 23, 2010

Push a local git branch to remote repository

Filed Under:

Push a local branch to the remote repository and automatically make it track the new remote branch - in one step

So you create a local working branch and eventually decide you want to push it to the remote branch (either to share it with colleagues or with another machine, or simply to have a convenient remote backup).

That, of course is easy. First create a new branch:

# git checkout -b new_branch
Switched to a new branch 'new_branch'

Then perform some work:

# echo "foo" >> foo.txt
# git add foo.txt
# git commit foo.txt -m "more foo"
[new_branch 5ba552f] more foo
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 foo.txt

and when you're ready:

# git push
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 270 bytes, done.
Total 3 (delta 1), reused 0 (delta 0)
To git@github.com:tomster/git-svn-helpers.git
 * [new branch]      HEAD -> new_branch

So far, so good. However, if you then want to pull in remote changes you get the following error:

# git pull
You asked me to pull without telling me which branch you
want to merge with, and 'branch.new_branch.merge' in
your configuration file does not tell me either.      Please
specify which branch you want to merge on the command line and
try again (e.g. 'git pull <repository> <refspec>').
See git-pull(1) for details.

If you often merge with the same branch, you may want to
configure the following variables in your configuration
file:

    branch.new_branch.remote = <nickname>
    branch.new_branch.merge = <remote-ref>
    remote.<nickname>.url = <url>
    remote.<nickname>.fetch = <refspec>

See git-config(1) for details.

You can either enter the git-config commands, edit .git/config manually or simply use the following shell script:

#!/usr/bin/env bash
function parse_git_branch {
  ref=$(git-symbolic-ref HEAD 2> /dev/null) || return
  echo ${ref#refs/heads/}
}
BRANCHNAME=`parse_git_branch`
git push
git config branch.$BRANCHNAME.remote origin
git config branch.$BRANCHNAME.merge refs/heads/$BRANCHNAME

Save it as git-publish anywhere on your $PATH (don't forget to make it executable) and then all you need to do, whenever you want to push a local git branch for the first time is simply this:

# git publish
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 270 bytes, done.
Total 3 (delta 1), reused 0 (delta 0)
To git@github.com:tomster/git-svn-helpers.git
 * [new branch]      HEAD -> new_branch
# git pull
Already up-to-date.

Update: changed the shebang to #!/usr/bin/env bash since it really is a bash script, thanks to @HansHuebner

Update (2): As of git 1.7 you can use git push --set-upstream to achieve the same effect.

Sep 06, 2009

gitosis and virtualenv

Filed Under:

Notes on setting up gitosis (on FreeBSD) using virtualenv.

Basically, the following is a condensed and simpler version of the excellent tutorial over at scie.nti.st, except that we don't use the system-wide site-packages. The crucial simplifications are:

  • we don't muck about with $PYTHONPATH or /etc/environment because that's evil :)
  • instead we use virtualenv

First, install git:

cd /usr/ports/devel/git
sudo make install

Install python and friends:

cd /usr/ports/lang/python25
sudo make install
...
cd /usr/ports/devel/py-setuptools
sudo make install
...
sudo easy_install virtualenv

Add a git user:

sudo pw adduser -n git -m

Copy the public key:

cp ~/.ssh/authorized_keys /tmp/gitosis.pub

Become the git user and install gitosis using virtualenv:

sudo su - git
virtualenv . --no-site-packages
mkdir src
cd src/
git clone git://eagain.net/gitosis.git
cd gitosis
../../bin/python setup.py install

The only thing missing now is to use absolut paths in the hook script (to make it work with virtualenv). IOW, /home/git/repositories/gitosis-admin.git/hooks/post-update should look like this:

#!/bin/sh
set -e
/home/git/bin/gitosis-run-hook post-update
/usr/local/libexec/git-core/git-update-server-info

Make sure, the hook's executable bit is set:

chmod a+x /home/git/repositories/gitosis-admin.git/hooks/post-update

Voila! You can clone the gitosis-admin repository and start working with it.

Jan 12, 2009

Git command aliases

Filed Under:

Not everything in svn is worse than in git!

One of the nice features of subversion are its built-in command aliases, i.e. instead of having to type svn commit you can just use svn ci, instead of propertyedit just type pe.

git doesn't have those, which some find annoying. However, you can easily remedy this by adding an [alias] section to your ~/.gitconfig file. Upon request and because it's really a nice feature here's an example for that from my own configuration. I've copied it myself mostly from Andi, who is as much more prolific than I as he is lazier to blog ;-)

Anyway, here it goes:

[core]
    excludesfile = /Users/tomster/.gitignore
    pager = "/opt/local/bin/less -RciqMSj5"
    editor = mate -w

[color]
    diff = auto
    branch = auto
    status = auto

[diff]
    renames = true

[alias]
    st = status
    d = diff
    ci = commit -v
    cia = commit -v -a
    co = checkout
    cp = cherry-pick
    l = log
    ll = log -p
    lt = log trunk..
    llt = log -p trunk..
    lm = log master..
    llm = log -p master..
    b = branch

The .gitignore file referenced above looks like this (and should look rather familiar to svn users):

.svn
*.egg-info
*.pyc
.DS_Store
tmtags

Update: @philikon found the FM :-)