Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly use git and branches

I'm kind of new to version control with GIT. I read this Guide and am following the basic approach that is shown in the diagram HERE. Still, I have some doubts about how to use git branches to separate the development of new features from existing code.

Here is an example. Suppose that at the start, my repository contains the following two main branches:

  • Master branch (containing the release version)
  • Develop Branch (containing new fixes or features to separate them from existing project features)

When I need to develop new features or modules, I create branches from Develop and start the new code projects there. For example, I make three new branches to add features related to Sun, Star, and SuperNova. Now, my repository contains five branches:

  • Master branch: Release 1.0.0
  • Develop branch: Modification after release 1.0.0
  • NewModule_Sun branch: add Sun to project (create from Develop branch)
  • NewModule_Star branch: add Star to project (create from Develop branch)
  • NewModule_SuperNova branch: add SuperNova to Project (create from Develop branch)

For Release 1.0.1, I want to include the the Sun and Star modules, but not SuperNova. So, I merge them with Develop and then merge Develop with the Release:

  1. Merge NewModule_Sun into Develop
  2. Merge NewModule_Star into Develop
  3. Merge Develop into Master (release 1.0.1)

The Develop branch needs to be kept permanently, but the Sun and Star branches are no longer needed. They get deleted:

  1. Delete the NewModule_Sun branch
  2. Delete the NewModule_Star branch

After these changes my repository contains the following three branches:

  • Master Branch: Release 1.0.1
  • Develop Branch: Modification after release 1.0.1
  • NewModule_SuperNova branch: Modification after release 1.0.0 (created from Develop when it was not merged with the Star/Sun branches)

==

Firstly, am I using git branches correctly?

Secondly, I reviewed the history of the final Develop branch, and it seems that I have lost some information on the NewModules. Is that normal? And, is it possible to transfer all the history information to the Develop branch?

Thank you!!

like image 671
Marcx Avatar asked Apr 04 '12 10:04

Marcx


People also ask

Should you use branches in git?

In Git, branches are a part of your everyday development process. Git branches are effectively a pointer to a snapshot of your changes. When you want to add a new feature or fix a bug—no matter how big or how small—you spawn a new branch to encapsulate your changes.

What is the best practice for git branching strategy?

Build your strategy from these three concepts: Use feature branches for all new features and bug fixes. Merge feature branches into the main branch using pull requests. Keep a high quality, up-to-date main branch.

Can git work with two branches at once?

You can have many branches in your repository, but only one of these will be "checked out" as the working-tree so that you can work on it and make changes. git worktree adds the concept of additional working trees. This means you can have two (or more) branches checked-out at once.


2 Answers

Am I doing a propery use of git?

Yes the workflow that you describe is pretty much standard workflow. You create some branch, you work on it and when you're done you merge it and remove the not needed branch (unless you are going to continue developing on that branch).

After removing a branch, viewing the history it seems to me that I have lost every information about the branch itself... is that normal?

Yes this is normal.

is it possible to remove a branch but leaving the history information unbroken?

Not sure what you mean here. As long as you have merged the branch before deleting it, the history is still there. You just merged it into another branch and the history can be seen on that branch. There is no way to know when a branch was deleted if that's what you are asking for.

like image 53
ralphtheninja Avatar answered Oct 07 '22 14:10

ralphtheninja


I suggest you read http://nvie.com/posts/a-successful-git-branching-model/ which defines a good pattern for git branching.

I have found that I keep development branches for a period, until time renders the change history you made in those revisions not worth keeping (about 6 months), and then delete them.

like image 30
Simon Featherstone Avatar answered Oct 07 '22 15:10

Simon Featherstone