Android App

TechLogic

A branch is a parallel version of a repository. As official doc says, a branch in Git is simply a lightweight movable pointer to one of these commits. We generally use a branch to isolate development work without affecting other branches in the repository. The default branch name in Git is master.

When you want to add a new feature or fix a bug, it is not recommended to do changes directly into master branch to avoid the chances for unstable code to get merged into the main code base. So, its recommended to create a new (feature / development / bugfix) branch to encapsulate your changes.



How to create a new branch in git?

Create a new feature / development / bugfix branch in the repository

$ git branch <dev_branch>

Switch to the dev branch to work on it.

$ git checkout <dev_branch>


Alternately, we can create a branch and checkout to that branch by using this command:

$ git checkout -b <dev_branch>




How to check a branch list in git?


From your terminal window, list the branches on your repository.

$ git branch

* master
  dev
  
This output indicates there are two branches, the master and the asterisk indicates it is currently active.




How to commit changes in git?


Commit the change to the feature / development / bugfix branch:

`git add .` command is used to stash all changes or files

$ git add .
$ git commit -m "adding a change from the develope branch"





How to push a branch to repo in git?



Push the develope branch in git:

$ git push origin <dev_branch>

This will push all changes along with commit message to git.





How to rename the currently checked out branch?



$ git branch -m <renamed_dev_branch>




How to delete a branch in git?



$ git branch -d <dev_branch>

We can also use the `-D` flag  instead of `-d` which is synonymous with `--delete --force`. This will delete the branch regardless of its merge status.


The '-d' option stands for '--delete', which would delete the local branch, only if you have already pushed and merged it with your remote branches.

The '-D' option stands for '--delete --force', which deletes the branch regardless of its push and merge status, so be careful using this one!




How to check if a branch is merged into master in git?


$ git branch --merged master : This prints the branches merged into master

$ git branch --merged lists : This prints the branches merged into HEAD (i.e. tip of current branch)

$ git branch --no-merged : This prints the branches that have not been merged








Post a Comment