Git - Creating a Branch

From EdWiki

GitLab - Creating a Branch

  • Goals
    Learn how to create a local branch in a repository

It’s time to do a major rewrite of the hello world functionality. Since this might take awhile, you’ll want to put these changes into a separate branch to isolate them from changes in master.

  • Create a Branch
    Let’s call our new branch ‘greet’.
git checkout -b greet
git status

NOTE: git checkout -b <branchname> is a shortcut for git branch <branchname> followed by a git checkout <branchname>.

Notice that the git status command reports that you are on the ‘greet’ branch.

  • Changes for Greet: Add a Greeter function.
  • File : greeter.c
#include <stdio.h>
#include <stdlib.h>
void Greeter(char* grt) {
git add src/greeter.c
git commit -m "Added greeter function in greeter.c file"
  • Changes for Greet: Modify the main program
  • File : hello.c
    Author  : J.Shankarappa (
#include <stdio.h>
#include <stdlib.h>
void Greeter(char* grt);
int main(int argc, char* argv[]) {
    if( argc > 1 ) {
        puts(argv[1]);         /* prints argv[1] */
        Greeter("Please WelCome to the Git Workshop");
    } else {
        puts("Hello World!!!"); /* prints !!!Hello World!!! */
    return EXIT_SUCCESS;
git add src/hello.c
git commit -m "Hello uses Greeter function"
  • Changes for Greet: Update the Makefile
    Update the Makefile
  • File : Makefile
   #              Makefile                                  #
   all:	hello.elf
   hello.elf:	hello.o greeter.o
   hello.o:	hello.c greeter.c hello.h
   	-rm -f hello.elf hello.o
   # The following variables and implicit rules are required for the GNU
   # Assembler and the GNU Compiler.
   AS        = as
   CC        = gcc
   LD        = ld
   ASFLAGS   = -g
   CFLAGS    = -O2 -g -Wall
   LDFLAGS   =
   LDLIBS    = -lc
   # Compile C code (.c) to an object file (.o)
   %.o: %.c
   	$(CC) -c $(CFLAGS) $< -o $@
   # Assemble ARM assembly language source (.s) to an object file (.o)
   %.o: %.s
   	$(AS) $(ASFLAGS) $< -o $@
   # Link files into an executable (.exe), using the GNU Compiler.
  	$(CC) $(LDFLAGS) $+ $(LOADLIBES) $(LDLIBS) -o $@
   # Miscellaneous rules
   .PHONY:	all clean
git add Makefile
git commit -m "Updated Makefile"
  • Up Next

We now have a new branch called greet with 2 new commits on it. Next we will learn how to navigate and switch between branches.