Git Recipes

A cookbook of ways to do various things in git.

create a repository with gitosis

git clone
edit gitosis.conf
git commit -a -m 'message'
git push

Create a new local branch that you publish publicly.

with a shared publishing server

Create new local branch and check it out:

git checkout -b blue

Push this branch to the server:

git push origin blue:blue

origin = repository
blue:blue = refspec in the form of :

With older versions of git you may need to do git push origin blue:refs/heads/blue

Then, other people can check out your branch like so:

git pull
git branch -r
git checkout --track -b blue origin/blue

The git branch -r is just to check to see if origin/blue was actually added.

To delete your new branch from the server:

git push origin :blue

with a private publishing server

Create a branch called ‘blue’ that other people can pull from using an ssh account on a server with git daemon running.

git remote add published-blue git+ssh://
git push published-blue master
git remote update
git branch blue published-blue/master
git config remote.published-blue.push blue:master

Then, whenever you want other people to be able to pull your changes:

git checkout blue
git push

Other people can pull this like so:

git clone

Add a new remote repo as a local branch

“blue” will be the name of the remote repository. We will add this as “blue-remote” to our repo, and check out a branch called “blue”.

(1) add additional repo as remote

git remote add blue-remote

(2) optional: populate an empty repo.

If the remote repository is totally empty, we might want to populate it with our current master.
To do this, you run this command:

git push blue-remote master:refs/heads/master      

Only one person would do this once when ‘blue’ was initially created.

(3) make everything is in sync.

git remote update

(4) Create a local branch ‘blue’ based on ‘blue-remote’

git branch blue blue-remote/master

(5) Make it so that the push command will do what you expect when in the blue branch

git config blue:master

With this, a push in the ‘blue’ branch will push to the remote repository ‘blue’, which is probably what you want.

If you didn’t do this, you would have to do this to get that behavior:

git push blue-remote blue:master

Altogether now

git remote add blue-remote
git remote update
git branch blue blue-remote/master
git config blue:master

Creating a Remote Branch

(1) Create the remote branch

git push origin origin:refs/heads/new_feature_name

(2) Make sure everything is up-to-date

git fetch origin

(3) Then you can see that the branch is created.

git branch -r

This should show origin/new_feature_name

(4) Start tracking the new branch

git checkout --track -b new_feature_name origin/new_feature_name

This means that when you do pulls that it will get the latest from that branch as well.

(5) Make sure everything is up-to-date

git pull

(6) Cleaning up Mistakes

If you make a mistake you can always delete the remote branch

git push origin :heads/new_feature_name

Pushing a local branch to a remote repo

Lets say you already have a local branch called “pine”. How do you get this to show up on the remote repository?

> git push origin pine
 * [new branch]      pine -> pine

You may also need to edit .git/config, depending on how you created the local branch:

[branch "pine"]
	remote = origin
	merge = refs/heads/pine

Deleting a remote branch, and deleting it from the remote repo

Suppose you have a branch set up to track a remote branch, and you want to delete this branch everywhere (the branch, the remote branch it points to, and the branch on the server).

Suppose the branch was named deadbranch:

git branch -d deadbranch
git branch -rd origin/deadbranch
git push origin :deadbranch

The first line deletes your branch, the second line deletes your remote, and the third line pushes this to the server.

In order for other people to get these changes, they must run:

git remote prune origin

Separating out subdirectories from repositories

Say you have a repository that contains a project that has several nicely related subprojects in directories. You want to split out these subprojects so that you can share them with other projects and repositories, in otherwords they need to live their own lives as their own repositories.

To do this you just need to use the command ‘git-filter-branch’ as follows:

$ cp -a repo repo.old  # just to keep a backup
$ cd repo
$ git filter-branch --subdirectory-filter somedir -- --all

This will rewrite as much refs as possible and leave you sitting in the directory ‘somedir’ with nothing else surrounding it.
Note that ‘refs/original/*’ will still point to the old commits and you will get a message about this, so they won’t be gone totally, you need to now clone this repository somewhere else, once you are sure the filter-branch did what you want:

$ cd ..
$ git clone repo repo2
(check out repo2)
$ rm -rf repo

Now repo2 is a clean repository with only the files and commits related to the directory that you specified with ‘somedir’ above!

Colors make the world prettier

Adding the following to your $HOME/.gitconfig makes things really nice to look at:

        ui = auto
	rlog = log --pretty=format:\"%h %Cblue%cr%Creset %cn %Cgreen%s%Creset\"

Now try doing a ‘git-log -p’ and also a ‘git rlog’. Makes reading diffs much easier, and makes seeing the recent commits easy.