Wednesday, August 20, 2014

Git feature branch

More and more we are using "feature branch" rather than committing directly to master.

First, let's verify on which branch we are:
git checkout
Your branch is up-to-date with 'origin/master'.

and which branches are available in the current repo:
git branch
* master

it doesn't hurt to explicitly make sure we are on master:
git checkout master
Already on 'master'
Your branch is up-to-date with 'origin/master'.

and if needed, let's get the latest from the central repo:

git pull --rebase

we can now create a branch:

git checkout -b feature/pippo

and make sure the branch is actually created:

git branch
* feature/pippo

since I am paranoid, I again switch to this new branch:

git checkout feature/pippo
Already on 'feature/pippo'

I create a file:

echo ciao > ciao.txt


git add ciao.txt

and commit

git commit -m "added ciao"

if you switch to "master", ciao.txt disappears, because it's only in the "feature/pippo" branch:

git checkout master

ciao.txt is gone!

git checkout feature/pippo

ciao.txt is there again!

Now we need to push the branch to the central repo:

git push
fatal: The current branch feature/pippo has no upstream branch.
To push the current branch and set the remote as upstream, use

git push --set-upstream origin feature/pippo

you can simply do:
git push -u origin HEAD
Counting objects: 6, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 273 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To ssh://
 * [new branch]      HEAD -> feature/pippo
Branch feature/pippo set up to track remote branch feature/pippo from origin.

How to promote to master the changes in "feature/pippo" ? With a pull request, followed by a code review and a test! In github or stash you can create the pull request using the UI. This is a very cool and organized way to include changes to the master branch in a controlled, "atomic" way - a whole branch is a unit of changes that can be excluded as a whole if needed.

No comments: