Feature branches are used to introduce new features, remove existing features and fixing bugs for next release. Since they get merged back into develop branch, master branch will always remain behind develop branch in both local and remote repositories.



When working with feature branches, you should open up a "pull request" in GitHub so that your team members can "peer review" your work. This is considered as the best practise! Also pay attention to "Summary of actions" notes in code.


Preparation


These steps are compulsory before start working on a new feature branch because our local branch might be behind remote copy.


Step 1


Make sure we're on develop branch.


$ git branch
* develop
master

Step 2


Fetch all remote updates.


$ git remote update
Fetching origin

Step 3


Update local develop branch so it is up-to-date with remote copy.


$ git pull origin develop
From github.com:inanzzz/gitflow
* branch develop -> FETCH_HEAD
Already up-to-date.

Create feature branch


Step 1


Create it so that it tracks local develop branch.


$ git flow feature start hello-world
Switched to a new branch 'feature/hello-world'

Summary of actions:
- A new branch 'feature/hello-world' was created, based on 'develop'
- You are now on branch 'feature/hello-world'

Now, start committing on your feature. When done, use:

git flow feature finish hello-world

$ git branch
develop
master
* feature/hello-world

Step 2


Assume that you did some work in your project so now commit what you've done.


$ echo 'feature/hello-world' > one.txt
$ git add --all
$ git commit -m 'Feature hello-world commit'
[feature/hello-world eeaecc7] Feature hello-world commit
1 file changed, 1 insertion(+)
create mode 100644 one.txt


Step 3


Publish it to open a "pull request" in remote repository.


$ git flow feature publish hello-world
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 314 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:inanzzz/gitflow.git
* [new branch] feature/hello-world -> feature/hello-world
Already on 'feature/hello-world'
Your branch is up-to-date with 'origin/feature/hello-world'.

Summary of actions:
- A new remote branch 'feature/hello-world' was created
- The local branch 'feature/hello-world' was configured to track the remote branch
- You are now on branch 'feature/hello-world'

Step 4


If you go to GitHub, there will be a notification bar that will ask you to open a new "pull request" for the feature branch you've just published. Open it by comparing it to develop branch, writing a subject and a description for it.




Step 5


At this point "peer review" takes place against the "pull request" in GitHub. If everyone in your team is happy with the work you've done in feature branch, you can move on to finish your feature branch in next step. If your code needs refactoring then carry on working until everyone is happy.


$ git branch -avv
develop c9b8a72 [origin/develop] Very first commit
* feature/hello-world 24683c1 [origin/feature/hello-world] Feature hello-world commit
master c9b8a72 [origin/master] Very first commit
remotes/origin/develop c9b8a72 Very first commit
remotes/origin/feature/hello-world 24683c1 Feature hello-world commit
remotes/origin/master c9b8a72 Very first commit

Step 6


Finish feature branch.


$ git flow feature finish hello-world
Switched to branch 'develop'
Your branch is up-to-date with 'origin/develop'.
Updating c9b8a72..24683c1
Fast-forward
one.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 one.txt
To git@github.com:inanzzz/gitflow.git
- [deleted] feature/hello-world
Deleted branch feature/hello-world (was 24683c1).

Summary of actions:
- The feature branch 'feature/hello-world' was merged into 'develop'
- Feature branch 'feature/hello-world' has been locally deleted; it has been remotely deleted from 'origin'
- You are now on branch 'develop'

Check git status. As you can see, local develop branch is ahead of remote develop branch because feature branch has been merged into it. Also local and remote feature branches have been deleted.


$ git branch -avv
* develop 24683c1 [origin/develop: ahead 1] Feature hello-world commit
master c9b8a72 [origin/master] Very first commit
remotes/origin/develop c9b8a72 Very first commit
remotes/origin/master c9b8a72 Very first commit


Step 7


Push local develop branch to remote repository.


$ git push origin develop
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 315 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:inanzzz/gitflow.git
c9b8a72..24683c1 develop -> develop

Just to confirm that your local develop branch is now up-to-date with the remote develop branch.


$ git branch -avv
* develop 24683c1 [origin/develop] Feature hello-world commit
master c9b8a72 [origin/master] Very first commit
remotes/origin/develop 24683c1 Feature hello-world commit
remotes/origin/master c9b8a72 Very first commit


Overview


In short terms, this is what we did above.


# 1. Checked out into develop branch
git checkout develop

# 2. Fetched all remote updates
git remote update

# 3. Update local develop branch with remote copy
git pull origin develop

# 4. Created a feature branch that tracks develop
git flow feature start hello-world

# 5. Did some dummy work
echo 'feature/hello-world' > one.txt
git add --all
git commit -m 'Feature hello-world commit'

# 6. Publish feature branch to remote repository
git flow feature publish hello-world

# 7. Opened a "pull request" in GitHub for "peer review"

# 8. Finish feature branch
git flow feature finish hello-world

# 9. Push develop branch
git push origin develop