Branching in Git

I switched companies 6 months ago. What was more interesting was, I came in that recherché moment when the entire team was switching repositories. We were moving to git. So, it was fun, going through all those self learning and stumble upon stumble over one’s mistakes. But it was all worth it, git is great. It is easy and it is fast. It is worthwhile to read about the history and the basic concepts. Enough said, onto git branching.

Well, you could get this wonderful tutorial on branching, or learn one command a day. But, let us see something simple. My task was simple, create a local branch and push it to remote.

So, if i want to create a new branch, ‘stopgap01’, key in the following command in the git bash
$ git branch stopgap01

This creates a branch called ‘stopgap01’ . Both master and stopgap01 point to the same commit.

Note we are still in the ‘master’ branch, to move to the newly created branch, use the checkout command,
$ git checkout stopgap01
Switched to branch 'stopgap01'

So, let us make a few changes and commit,
$ git commit -m 'fixing a typo error'
[stopgap01 dd8cf4c] fixing a typo error
1 file changed, 1 insertion(+), 1 deletion(-)

But note that, it is still in your local machine. In real life, all teams work with a remote repository. You should be able to push the locally created branch to the remote repository so that your teammates should be able to work on it. Read more about working with remotes here.

To push to the remote, use the command,
$ git push origin stopgap01
Total 0 (delta 0), reused 0 (delta 0)
To nba_gitlab@sl123456.dcm.allstar:roster/list.git
* [new branch] stopgap01-> stopgap01

Here ‘orgin’ is the alias of the remote repository.

Deleting a branch

So, if you made your changes and merged in the changes to the parent branch, it is recommended you delete the branch.

Deleting from local is easy, just add a -d parameter to the branch command
$ git branch -d stopgap01
Deleted branch stopgap01 (was dd8cf4c).

To be noted that if there are unmerged changes, you will have to merge or stash them. Or force delete by using -D instead of -d.

Now, this deletes the branch from your local machine, but it is still available in your remote. The command for removing from remote is very queer,
$ git push origin :stopgap01
To nba_gitlab@sl123456.dcm.allstar:roster/list.git
- [deleted] stopgap01

We use the same push command, but with a ‘:’ before the branch name.

So, your branch is fully deleted.