Archive

Archive for the ‘git’ Category

Change git origin

January 7, 2012 Leave a comment

Scenario:
you checkout a remote github repo as read-only:

git clone git://github.com/yourusername/oneofyourrepo

After you work on the local working directory for a while you decide that you would want to be able to push the changes back to github.

The easiest way is to either:
1) run the follow command on the root of the working directory:

git config remote.origin.url git@github.com:yourusername/oneofyourrepo

2) edit .git/config, look for section [remote “origin”] and change the url to
git@github.com:yourusername/oneofyourrepo

If you have already added the ssh public key of the host that you are working on to github.com, you should now be able to push the changes.

Credit goes to: http://stackoverflow.com/questions/3011402/leaving-github-how-to-change-the-origin-of-a-git-repo

Categories: git

git branching by example

December 11, 2011 3 comments

In this post I am going to demonstrate how to use git to apply a bug fix from the master branch to a feature branch that has already committed a new feature.

Example: fork a branch, add some new features to it. In the meanwhile add a bug fix to the master branch. We want to apply the bug fix to the feature branch while working on it but we decide it’s not time yet to add the new feature to the master branch. The simplest way I found (so far) is to create a temporary branch (and switch to it) on top of the master branch with the bug-fix commit, merge from feature branch, then switch to feature branch, merge changes from combined.

mkdir demo_proj
cd demo_proj
vi func.php

Put the following code into func.php and save.

<?php
    function do_something() {
        echo "hello\n";
        return "wrong value";
    }
git add .
git commit -a -m "added function do_something in func.php"
git checkout -b feature
vi func.php

Add a new function new_feature() to func.php

<?php
    function do_something() {
        echo "hello\n";
        return "wrong value";
    }


    function new_feature() {
        return 999;
    }
git checkout master
vi func.php

Change word “wrong” to “right”:

<?php
	function do_something() {
		echo "hello\n";
		return "right value";
	}
git commit -a -m "fixed a bug in do_something()"
git checkout -b combined
git merge feature
git checkout feature
git merge combined
git branch -d combined

After “git merge combined” the feature branch has the bug-fix from master branch while maintaining its own new_feature() function. Branch combined has accomplished its goal hence it’s OK to remove it.

Categories: git