Git Tutorial - Git Branching Complete Tutorial
Git Tutorial - Git Branching Complete Tutorial

Git Tutorial – Git Branching Complete Tutorial


Git Tutorial — If you use git as version tool you may come across some common things like creating a branch, merging branch or deleting a branch. Here We will provide a quick glance of git branching scenarios and commands which can be useful for a developer in the daily development cycle.

Creating a new branch –

In recent versions of Git (1.7.0 and newer), you can create and checkout a new branch :

git checkout -b <branch>

flag -b causes a new branch to be created and then checked out.

Another way to create a new branch is but this command will not check out the newly created branch.

git branch <branch>

Setting remote tracking for branch –

Edit files, add and commit. Then push with the -u option:

git push -u origin <branch>

Git will set up the tracking information during the push. Now branch created locally will also be tracked remotely (a branch will be created at your main repository e.g hosted on github.com or bitbucket or gitlab)

Switching to a branch –

git checkout <branch>

Stashing a branch –

Sometimes you want to switch to another branch due to the urgent issue while you are in the midst of some development in the current branch. You can not commit changes in the current branch because it can break some functionality and if you do checkout another branch you will lose local changes.

In this situation stashing comes to help. It is the place where you can stash your changes, switch branch and then come back and start from where you left. Let’s run these commands in sequence

git status

It shows that some files have been modified.

git stash

All uncommitted changes moved into the stash.

git status

Working directory is clean.

Applying changes form stash –

To see what stashes have been stored

git stash list

Apply changes from a stash

git stash apply

Merging branch –

1. Switch to a branch where changes need to be merged

git checkout master

2. To see available branches

git branch

3. Merge a selected branch into master

Merging Branches Without A Conflict –

If the files from the different branches you’re merging don’t have any conflicts with your current branch, then the merge will be done smoothly without any issues.

git merge

Merging Branches With a Conflict –

In case of conflict first list out which files need merging via

git ls-files -u

It will show you the files where conflicts need to be resolved. If git has multiple version of a file then it will be listed multiple times.

If you want to see each branch file individually you can run

git show <branch>:<filename>

or consolidated file with conflict

git diff <filename>

You will see + and – in diff output. These have following meanings

  • “+” (a “plus sign”) means the line was missing from the previous version.
  • “-“ (a “minus sign”) means the line was added to the previous version.
  • ” ” (a space) means the line was not changed.

Resolving conflict manually –

Edit the conflicted file in editor and remove or add content to make it conflict free. Then add file and commit it.

git add <filename>

Check again for any conflicting state


git status
git commit

Resolving conflict with GUI –

Sometimes merging manually can be error prone. Git has a large number of GUI tools which can be used for merging e.g Beyond Compare, Kdiff3, meld.

I prefer to use Kdiff3, love its interface and 3-way merge. I have set it as my default merge tool in global config.

git config --global merge.tool kdiff3

Merging with Kdiff3 is quite simple. Just type the following command and it will open conflicted file one by one so that you can resolve it. Saving the file after resolving the issues will make it conflict free.

git mergetool -t kdiff3

Deleting branch after merging –

Sometimes in your git workflow, you need to create a temporary git branch for hot-fix of issues which after its use may be deleted.

Deleting a branch locally –

git branch -d branch_name

Deleting a remote branch –

If you want to delete corresponding remote tracking branch on the server.

git push origin --delete branch_name

Merging specific files from another branch –

Sometimes you will need to merge only a specific file from a certain branch. Let’s say you want to merge the only file abc.txt from hot-fix branch into the master branch.

Make sure that you are on the master branch. Check that via running

git branch
* master
  hotfix

If not switch to master branch


git checkout master
git checkout hot-fix abc.txt

Now abc.txt will be replaced in the master branch with one present in hotfix branch.

This is not actual merging but replacing the file itself. There are other lots of way to do the actual merge for a single file but I like the simplicity of this approach and follow this in my project.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.