how to work with git local branches

Compared to SVN and CVS, I think the best thing about Git is Github. It’s awesome how you can see the history of your source code.

So here are the steps to work with local git branches.

1. Commit and push the latest code from your local environment

1
2
[etagwerker@gesell prode]$ git commit -m "My latest stable release"
[etagwerker@gesell prode]$ git push

2. Pull the latest code from the server

1
2
[etagwerker@gesell prode]$ git pull --rebase
Current branch master is up to date.

3. Create a new local branch

1
2
[etagwerker@gesell prode]$ git checkout -b second_round
Switched to a new branch 'second_round'

4. Modify the code, add a feature, set of features or correct bugs, or whatever you want to do. Finally, commit all the code to your local branch

5. Switch to the master branch

1
2
[etagwerker@gesell prode]$ git checkout master
Switched to branch 'master'

6. Pull the latest code from the master branch

1
2
3
4
[etagwerker@gesell prode]$ git pull origin master
From github.com:/etagwerker/prode
* branch            master     -> FETCH_HEAD
Already up-to-date.

7. Merge the local branch with master

1
2
3
4
5
6
7
8
9
10
11
[etagwerker@gesell prode]$ git merge second_round
Updating 3043225..7ca2025

Fast forward

app/controllers/application_controller.rb          |    5 +++
app/models/forecast.rb                             |    4 ++
config/initializers/constants.rb                   |    4 ++-
.../20100625123027_add_round_number_to_games.rb    |   16 ++++++++
10 files changed, 94 insertions(+), 13 deletions(-)
create mode 100644 db/migrate/20100625123027_add_round_number_to_games.rb

8. Push the code to your remote branch

1
2
3
4
5
6
7
8
[etagwerker@gesell prode]$ git push
Counting objects: 44, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (25/25), done.
Writing objects: 100% (25/25), 3.43 KiB, done.
Total 25 (delta 18), reused 0 (delta 0)
To git@github.com:/etagwerker/prode.git
3043225..7ca2025  master -> master

9. And you are done!

Now you can work on local branches and merge with master whenever you want.

This entry was posted in developer tools, linux/unix world, programming and tagged , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

3 Comments

  1. Posted July 9, 2010 at 10:56 am | Permalink

    Step 8a should be:

    Run rake cucumber:all & rake spec.

    If tests pass, you’re good to go!

  2. Aaron Campos
    Posted August 20, 2010 at 6:30 pm | Permalink

    This is very close to my workflow as well, but one thing I find helpful prior to the merge shown on Step 7 above, is to rebase your local branch to the fetched master, so that the merge command in Step 7 will always be a fast-forward, so you won’t have merge commits, and fewer chances for conflicts.

    So, after Step 6 you would do:

    1
    2
    3
    % git checkout second_round
    % git rebase master
    % git checkout master

    Then you would continue with merging your local branch into the master. One other thing that can be very helpful is using the -i option on the rebase so you can, among other things, combine commits in your local branch, prior to merging to master. This can clean up the commit history, and turn a bunch of incremental commits into one big logical commit that encapsulates the whole feature.

    Interactive rebase is described here:
    http://www.gitready.com/advanced/2009/02/10/squashing-commits-with-rebase.html

    Cheers!

  3. Posted August 26, 2010 at 11:36 am | Permalink

    Cool, thanks! I will try this next time.

    I am still learning something new about Git every day (and I am finally seeing the advantages over SVN)

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Spam protection by WP Captcha-Free