You are currently viewing How To Work With Local & Remote Branches In GIT

How To Work With Local & Remote Branches In GIT

Introduction

Let’s assume the initial version of your application is released and you start receiving complaints about bugs and areas of improvement from the users. In this case, you will need an isolated space where you can copy the existing production code make some changes, do some testing and then merge the code with your main code for the next releases. 

Branching gives you an isolated workspace where you can do changes without affecting the mainline code. When you create a branch in git, it simply creates a reference point to your snapshots. In any way if you messed up your code or a particular feature is failing you can simply clean it up without impacting the main code.

Default Git Branch

When you run the “git init” command to initialize a git repository it automatically creates a branch called “master” and all the commits you initially make will be under the master branch. The default branch name doesn’t have to be master, you can set any default branch name but master, main, or trunk are the standard conventions.

I created a new project and added two commits to the project. Take a look at the below image, HEAD is now pointing to the master branch. The “HEAD” is just a pointer that points to the latest commit in the current branch.

Create A New Git Branch

When you create a branch, git creates a reference point to the latest commit in your current branch. The following command will create a branch called feature/JIRA1234. 

# Syntax
$ git branch <branch-name>
$ git branch feature/JIRA1234

Switch Between Different Git Branches

You can switch between branches using the “git checkout” and “git switch” commands. 

$ git switch <branchname>
$ git switch feature/JIRA1234

$ git checkout <branchname>
$ git checkout feature/JIRA1234

The git checkout command does a lot more things than checking out branches. As an alternative to the checkout command, the switch command is created and its purpose is to only switch branches.

Both checkout and switch commands can also create and switch branches automatically. This way you can avoid running the git branch command first to create a branch and then the git switch/checkout command to switch to the branch.

$ git checkout -b <branchname>
$ git switch -c <branchname> 

Let’s say you have created a new feature and want to push it to the remote repository. The following command pushes the branch feature/JIRA1234 to the remote repository and updates the upstream.

# syntax
$ git push -u origin <branch>

$ git push -u origin feature/JIRA1234

List Of Git Branches

1. The first line of the “git status” command shows which branch you are currently in.

$ git status
On branch feature/JIRA1234
Your branch is up to date with 'origin/feature/JIRA1234'.
nothing to commit, working tree clean

2. The “git branch” command shows you the list of local branches. The current branch will be prefixed by the “*” symbol.

$ git branch 
* feature/JIRA1234
  master

If you add the -a flag to the branch command it will show the local and remote branches list.

$ git branch -a 
* feature/JIRA1234
  master
  remotes/origin/feature/JIRA1234
  remotes/origin/master

3. The “git log” command shows which branch the HEAD is pointing at. The HEAD is just a reference pointer that points to the last commit from the current branch.

* 77e03c2 (HEAD -> feature/JIRA1234, origin/feature/JIRA1234) feature2 added

4. You can also check the HEAD file under the .git directory which shows the current branch HEAD is referring.

$ cat .git/HEAD
     1  ref: refs/heads/nixzie

Rename Local & Remote Branch in Git

Renaming a branch locally is a simple process that can be done by passing the -m flag to the branch command. Here I renamed my branch “feature/JIRA1234” to “feature/JIRA9876”.

#Syntax
$ git branch -m <old-branch> <new-branch>

$ git branch -m feature/JIRA1234 feature/JIRA9876

Pushing the change to the remote repo is a two-step process.

1. The branch from the remote repository should be deleted.

2. The renamed branch should be pushed to the remote repository and the upstream branch should be updated to point to this renamed branch.

You can run the following command which will delete the old branch, push the renamed branch and update the upstream.

-u -> Updates the upstream.

:feature/JIRA1234 -> Branch to be deleted in remote repo

feature/JIRA9876 -> Branch to be pushed to the remote repo

$ git push -u origin :feature/JIRA1234 feature/JIRA9876

Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
To github.com:branchtest.git
 - [deleted]         feature/JIRA1234
 * [new branch]      feature/JIRA9876 -> feature/JIRA9876
Branch 'feature/JIRA9876' set up to track remote branch 'feature/JIRA9876' from 'origin'.

Delete Local & Remote Branch In Git

You can delete a git branch by passing the -d flag to the git branch command. An important point you have to understand is, that git will not allow you to delete the branch if it is not fully merged.

$ git branch -d feature/JIRA9876
error: The branch 'feature/JIRA9876' is not fully merged.
If you are sure you want to delete it, run 'git branch -D feature/JIRA9876'.

To force delete you can pass the -D flag

$ git branch -D feature/JIRA9876
Deleted branch feature/JIRA9876 (was 77e03c2).

To delete the remote repository run the following command.

$ git push origin --delete feature/JIRA9876
To github.com:XXX/branchtest.git
- [deleted]         feature/JIRA9876

Alternatively, you can also run the following command which is the same command we ran in the rename section. Instead of –delete flag “:” is used.

$ git push origin :feature/JIRA9876

Wrap-Up

Creating and working with branches is a pretty common and easy task. The difficult part is how these changes are merged into the main branch which will be deployed as part of your product. In the next article, we will focus on how to merge branches and resolve merge conflicts.

Leave a Reply

14 − ten =