Pages

Monday, March 4, 2019

Git - Understanding Branching

Branching is most important concept in the version control system. Branching means we can diverge from the main line of development and continue to work without messing the main line of code.

Lets say that we have developed some code and it is available in our master repository. Now when ever we want to make code changes we make the changes on our local repository and push them to remote or master.

We have your master branch and we don't want to mess anything up on that branch. However, we need to implement a new feature that works with the codebase on the master branch. A branch will allow to make a "copy" while not affecting the original branch

Lets understand more about the branches and how they work. Now with the same example as above, if we run
jagadishsample$Tue Feb 19@ git branch
* master

The “git branch” is the command that lets to create, list, rename and delete branches. To create a branch we can use the “git branch ” command as below,
jagadishsample$Tue Feb 19@ git branch sample
jagadishsample$Tue Feb 19@ git branch --list
* master
 sample

We can see that an asterisk is set before the master indicating that we are currently working with the master branch. The only thing that happens now is that a new pointer is set to the branch with the asterisk. Until we change the pointer to the new branch we cannot work with the new branch that we created.
jagadishsample$Tue Feb 19@ git checkout sample
Switched to branch 'sample'

jagadishsample$Tue Feb 19@ git branch
  master
* sample

Now that we changed our branch from master to sample, lets add some content to the sample
branch.
jagadishsample$Tue Feb 19@ vi two

When we check the status,
jagadishsample$Tue Feb 19@ git status
On branch sample
Untracked files:
 (use "git add ..." to include in what will be committed)

    two

nothing added to commit but untracked files present (use "git add" to

I have a file already available in the same location but i don't see that. This is because the file is already committed. Once we create a branch we will get a copy of the master branch that has file already committed.
Now let's add and commit the file two to the sample branch as,
jagadishsample$Tue Feb 19@ git add .
jagadishsample$Tue Feb 19@ git commit -m "sample branch"
[sample ea4ccb9] sample branch
1 file changed, 2 insertions(+)
create mode 100644 two

We can use the same “git show ” to show the files committed to the sample branch as,

jagadishsample$Tue Feb 19@ git show sample
commit ea4ccb9f6cbf5cb5f4c5c99b4053735aa4d36b9d (HEAD -> sample)
Author: jagadish
Date:   Tue Feb 19 11:19:25 2019 +0530

   sample branch

diff --git a/two b/two
new file mode 100644
index 0000000..da67877
--- /dev/null
+++ b/two
@@ -0,0 +1,2 @@
+this is jagadish in two file
+

We can now confirm that the branch sample has some new code committed and this code is not available with the master branch. We can use the “git show master” to see the files in the master branch.

Now since the new code in our sample branch is not available , lets merge the new branch code to our master branch as,
jagadishsample$Tue Feb 19@ git checkout master
Switched to branch 'master'

Change to the master branch and confirm as,
jagadishsample$Tue Feb 19@ git branch
* master
  sample

Once we see the asterisk before the master we can then merge our sample branch to our master branch as,

jagadishsample$Tue Feb 19@ git merge sample
Updating 0bfba39..ea4ccb9
Fast-forward
two | 2 ++
1 file changed, 2 insertions(+)
create mode 100644 two

Lets check the master branch now as,
jagadishsample$Tue Feb 19@ git show master
commit ea4ccb9f6cbf5cb5f4c5c99b4053735aa4d36b9d (HEAD -> master, sample)
Author: jagadish
Date:   Tue Feb 19 11:19:25 2019 +0530

   sample branch

diff --git a/two b/two
new file mode 100644
index 0000000..da67877
--- /dev/null
+++ b/two
@@ -0,0 +1,2 @@
+this is jagadish in two file
+

We can see both files are available now which means what ever separate code that we have written as branch is now merged. It is safe to delete a branch now using,
jagadishsample$Tue Feb 19@ git branch -D sample
Deleted branch sample (was ea4ccb9).

1 comment :

  1. You will draw more loyal readers to your site and keep your
    readers coming back because you are sharing the best with them.
    They do their level best to make a site attractive and beautiful.
    By using a service that can provide you will all of the various choices, you will save time and money getting online and noticed.

    ReplyDelete