Git – Configuration and Basic Commands – Part 3

In Part 1  we looked at Git configuration and in Part 2 – creating and cloning repository, commit and push files. In this part lets discuss about “branches”..

As soon as we create a new repository either in GitHub or locally or when we clone existing repository – “master” branch will be created by default.

Git_Branch.JPG

Ideally we do not want to make changes to “master” branch as in most of the organizations I’ve worked “master” branch is “production” and for the same reason we also do not want to make multiple commits to “master” branch. I’ve also seen cases where developers do not have permission to merge changes directly to “master” branch but instead have their own working branch(es) – do development, testing and once happy raise “merge” request and the administrator can commit to master branch.

# git branch – list all branches

#git branch <branch_name> – creates branch

#git checkout <branch_name> – go to the branch

#git checkout -b <branch_name> – it creates branch and does checkout

#git merge <branch_name> – merges the branch_name to the branch we are in. For example if you want to merge branch A into B ( git checkout B && git merge A ).

#git branch -d <branch_name> – deletes branch

Branches well explained in atlassian document : https://www.atlassian.com/git/tutorials/using-branches

Lets create a “test1” branch..

Git_Branch2.JPG

You can see a * infront of the branch which indicates the branch we are in. “git status” also shows the branch we are in. Lets check files in both branches..

Git_Branch3.JPG

Now create a file in “test1” branch and commit..

Git_Branch4.JPG

Compare with “master” branch.. As you can see below “test1branch.txt” file present in “test1” branch but not in master.

Git_Branch5.JPG

Now lets merge the “test1” with “master”.. You need to be in “master” branch and run the merge.. Merging the branch won’t delete the “test1” branch. If there are any conflicts git highlights and ask the user to resolve them.

Git_Branch6.JPG

So far our changes are local and no updates done to the remote – GitHub repository. From the below screenshot we can see the hint from git – local master ahead of remote by 1 commit.

Git_Branch7.JPG

Git_Push

Git_Push2.JPG

Merge explained in the document: https://www.atlassian.com/git/tutorials/git-merge

Advertisements

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