Home > How To > GIT Create a Branch and Merging

GIT Create a Branch and Merging

My previous post was about basic Creating GIT repos server / client

Before we start you can also get more information about Branching here 
Now lets create Branches and Merge new branch to “master” branch.
Later we will push this update to the remote GIT SERVER repos.
[To setup remote server see my previous post]


I will keep it simple for now, later on as I learn complex GIT stuff, will post it 🙂


Commands we will be using.
$ git branch – lists all the branches in the current repos
$ git branch – creates a new branch. 
$ git checkout   – moves control the branch.
$ git merge  – merges current branch with .
$ git push origin –all – pushes all data to the remote server repos.

Create a Branch
  • Lets create branch and lets call it “experiment”
ahmed@ahmed-work-horse:~/ahmed/test.git/test$ git branch 
* master
ahmed@ahmed-work-horse:~/ahmed/test.git/test$ git branch experiment
ahmed@ahmed-work-horse:~/ahmed/test.git/test$ git branch 
  experiment
* master
  • Now we have the branch created
  • (*) represents that we are currently in the “master” Branch
  • Lets move to branch “experiment”
ahmed@ahmed-work-horse:~/ahmed/test.git/test$ git checkout experiment 
Switched to branch ‘experiment’
  • Now we are in the branch “experiment”
  • Lets change something here.
  • Lets change the readme.txt file and then merge it to “master”.
ahmed@ahmed-work-horse:~/ahmed/test.git/test$ ls
readme.txt
ahmed@ahmed-work-horse:~/ahmed/test.git/test$ cat readme.txt 
Bismilla – Now we have updated the File.
Here is the old line.
Hello People !!! 
  • Above here is the original file information.
ahmed@ahmed-work-horse:~/ahmed/test.git/test$ vim readme.txt 
ahmed@ahmed-work-horse:~/ahmed/test.git/test$ cat readme.txt 
Bismilla – Now we have updated the File.
Here is the old line.
This is in the Branch – This was added in the Experimental Branch
Hello  People !!! 
-Ahmed
  • After changing.
  • Now lets check-in this file to branch.
ahmed@ahmed-work-horse:~/ahmed/test.git/test$ git status 
# On branch experiment
# Changes not staged for commit:
#   (use “git add …” to update what will be committed)
#   (use “git checkout — …” to discard changes in working directory)
#
# modified:   readme.txt
#
no changes added to commit (use “git add” and/or “git commit -a”)
ahmed@ahmed-work-horse:~/ahmed/test.git/test$ git commit readme.txt
[experiment ea810ac] Updated in Branch
 1 files changed, 5 insertions(+), 2 deletions(-)



Branch Merge to Master Repos

  • Now the file is committed to branch “experiment”
  • Lets move to branch “master”
ahmed@ahmed-work-horse:~/ahmed/test.git/test$ git checkout master 
Switched to branch ‘master’
Your branch is ahead of ‘origin/master’ by 1 commit.
ahmed@ahmed-work-horse:~/ahmed/test.git/test$ cat readme.txt 
Bismilla – Now we have updated the File.
Here is the old line.
Hello People !!! 
  • In “master” we dont see the changes we made in branch “experiment”.
  • So now we will merge the two branch.
ahmed@ahmed-work-horse:~/ahmed/test.git/test$ git diff readme.txt
ahmed@ahmed-work-horse:~/ahmed/test.git/test$ git merge experiment 
Updating 43a10ac..ea810ac
Fast-forward
 readme.txt |    7 +++++–
 1 files changed, 5 insertions(+), 2 deletions(-)
  • In branch “master” – we will merge branch “experiment”
ahmed@ahmed-work-horse:~/ahmed/test.git/test$ git diff readme.txt
ahmed@ahmed-work-horse:~/ahmed/test.git/test$ cat readme.txt 
Bismilla – Now we have updated the File.
Here is the old line.
This is in the Branch – This was added in the Experimental Branch
Hello  People !!! 
-Ahmed
  • Now we see, branch “experiment” changes in the “master” branch.
  • When you try to commit you will see that we are 2 commits behind the origin/master.
  • origin/master refers to the remote SERVER repository.
  • Lets do few more changes to the file and push it to the remote server.
ahmed@ahmed-work-horse:~/ahmed/test.git/test$ git commit readme.txt
# On branch master
# Your branch is ahead of ‘origin/master’ by 2 commits.
#
nothing to commit (working directory clean)
ahmed@ahmed-work-horse:~/ahmed/test.git/test$ git checkout experiment 
Switched to branch ‘experiment’
ahmed@ahmed-work-horse:~/ahmed/test.git/test$ git status 
# On branch experiment
nothing to commit (working directory clean)
ahmed@ahmed-work-horse:~/ahmed/test.git/test$ git checkout master 
Switched to branch ‘master’
Your branch is ahead of ‘origin/master’ by 2 commits.
ahmed@ahmed-work-horse:~/ahmed/test.git/test$ git status 
# On branch master
# Your branch is ahead of ‘origin/master’ by 2 commits.
#
nothing to commit (working directory clean)
ahmed@ahmed-work-horse:~/ahmed/test.git/test$ vim readme.txt 
ahmed@ahmed-work-horse:~/ahmed/test.git/test$ git status 
# On branch master
# Your branch is ahead of ‘origin/master’ by 2 commits.
#
# Changes not staged for commit:
#   (use “git add …” to update what will be committed)
#   (use “git checkout — …” to discard changes in working directory)
#
# modified:   readme.txt
#
no changes added to commit (use “git add” and/or “git commit -a”)
ahmed@ahmed-work-horse:~/ahmed/test.git/test$ git commit readme.txt
[master 6a06d23] Updated in MASTER
 1 files changed, 1 insertions(+), 1 deletions(-)
ahmed@ahmed-work-horse:~/ahmed/test.git/test$ git status 
# On branch master
# Your branch is ahead of ‘origin/master’ by 3 commits.
#
nothing to commit (working directory clean)




Push Data to Remote SERVER Repos.

  • Now lets push all the updated information to SERVER repos.
ahmed@ahmed-work-horse:~/ahmed/test.git/test$ git pu
pull   push   
ahmed@ahmed-work-horse:~/ahmed/test.git/test$ git push origin –all
ahmed@172.16.2.15‘s password: 
Counting objects: 11, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (9/9), done.
Writing objects: 100% (9/9), 959 bytes, done.
Total 9 (delta 2), reused 0 (delta 0)
   acb77b7..6a06d23  master -> master
 * [new branch]      experiment -> experiment
ahmed@ahmed-work-horse:~/ahmed/test.git/test
  • Now you can see that the branch “experiment” and “master” is synced with the remote SERVER repos.
  • Other users who dont have this update can “pull” from the remote server.
Thats all for today. 
Advertisements
Categories: How To
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: