Entries in git (3)

Monday
Nov052012

Removing a Shared Branch No Longer In Use

When I first wrote my post on sharing branches, there was something I did not talk about. Namely, how to remove a branch once you're done sharing. (I'll confess I didn't write about it, because at the moment I had not found a way of doing it properly)

But now, thanks to this nice stackflow post, I now know how to do it.

First of all, there are two different scenarios. The first case is when the collaborator you were sharing with, no longer needs their copy of your branch. They might want to remove the branch to tidy up their local repo. This can be done with the following command:

git branch -d <branch>

or to follow in the steps of my old example:

git branch -d superCoolFeature

The lower case -d option will only remove the branch if the branch has not been merged or somehow saved. You can force the removal with the upper case -D option if you're feeling more forceful.

The second case is when you are done using and sharing your branch. You probably have concluded whatever you were trying to do with this branch and merged the result back into master. At this point you might want to remove the temporary branch not only locally, but remotely as well. That can be done with:

git push origin --delete <branchName>

or again, in the case of my example:

git push origin --delete superCoolFeature

And the branch should now be gone from the remote repo. To remove it from the local repo, do the same "branch -d" step as your collaborator did in the first case. Needless to say, please make sure you only erase a branch you were done with.

Sunday
May012011

Sharing Git Branches Remotely

I usually have two branches when working on a project with a team. The master branch tracks origin/master, and I do most of my work on my local development branch.

Every now and then, I work an experimental feature. When I start on it, I'm not sure if it will work out and wether or not it should make it into the master branch someday. In those cases, I will make a new local branch to play with the idea. Lets say I called it superCoolFeature. At some point of the development of this feature, I might need to share this branch with one of my team members. How do do I do that?

Well... I just figured that out recently and I thought I'd post it here in order to remember it the next time I need it.

It turns out to be a two step process. First you need to push your local branch onto your remote (usually your remote is origin, but you can type git remote to see a list of remotes.) After that, you will need to make your local branch track your remote branch. Here are the commands that do just that:

Note: You need to be on a different branch before you execute the second command, so you might want to do a git checkout master first.

git push origin <branch>
git branch -f <branch> origin/<branch>

or to be more specific to my example:

git push origin superCoolFeature
git branch -f superCoolFeature origin/superCoolFeature

Once you've done that, you should have a new branch on your remote and your local branch should track it. Now you need to tell your teammate to create his own local branch that tracks the remote branch. They can do that with the following:

git branch -t <branch> origin/<branch>

or again, more specifically:

git branch -t superCoolFeature origin/superCoolFeature

As always, you should proceed with caution. Have backups of your repo before performing a new command for the first time (In general backups are a good idea, regardless of what you're up to.)

Also for future reference, here is the list of blog post on the subject I found while investigating this:

Thursday
Oct232008

Speeding up a Cocoa Project Repository Setup with Git

I just finished working my way through a chapter of a new iPhone dev book. I had a working copy of the example of the book and felt like mucking around with it to learn more on my own. I still want to keep the original state of the code, but I am too lazy to setup a repository for such "disposable" code.


Years of using Subversion have made me lazy like that. I am really lazy. I always forget what the steps are to create a repo, mostly because it is usually such an infrequent task for me. But now that Git makes it so easy to create a repo right there in the project directory I'm working on without littering the whole thing with junk... hmmm... Maybe I should be using it even for "disposable" code.


So to facilitate this new behavior I wrote a little script. It expects the root of the project to be the current working directory, so make sure you "cd" into the right place before invoking it. Here is what it does:



  1. Creates a new new git repository at the CWD

  2. Creates a new .gitignore file to ignore:
    • the build directory

    • user.pbxuser file inside the project

    • user.perspectivev3 file inside the project

  3. Adds all the files in the CWD to the index, staging them for inclusion in the next commit

  4. finally commit everything with the default message "- initial commit to Repository"


I think this will be useful to me, and so I post it here in case it is useful to others. It is really very simple. Please let me know if you have any suggestions to improve it.



#!/bin/bash

git init

cat > .gitignore <<EOF
build
.xcodeproj/.pbxuser
.xcodeproj/.perspectivev3
EOF

git add .

git commit -a -m "- initial commit to Repository"