Thursday, January 26, 2012

Branching and Merging for Git

Branching is a technique wherein you can create an active line of development for your source code. Through this, you will be able to create a separate copy of your source code  that you can edit and merge to your original or other copies (branches).

In order to create a branch named “testing” on an existing git repository:

git branch testing

This will create a branch named testing.

In order to check your local branch:

        git branch -l

To check for your remote branch

        git branch -r

To check all available branch for your repo:

        git branch -a

Example you inputted git branch -l, this will list all the branch on your local repo



The asterisk marks the branch you are currently on. For you to transfer to testing just:

        git checkout testing

For you to transfer to master again, commit your changes first for testing branch then checkout master:

        git checkout master

Therefore, the change you made on the testing branch is not visible on master.You can change then commit it.

If you are on master and you want to merge the changes of master and testing, you can do so by running:

        git merge testing

If you’re on testing:

        git merge master

Sometimes, there are conflicts on merging the two branch (conflict means you edit the same file on the same line). You can see all the file that has conflict by the use of:

        git diff

If you can’t fixed the conflict on files and decided to give up you can:

        git reset --hard HEAD

or if the change is committed use:

        git reset --hard ORIG_HEAD

If you want to delete testing branch, just execute:

        git branch -d testing


No comments:

Post a Comment