10

I'm using Git for one of my projects at the moment, and I love it.

However, because I'm the only one working on my project, the only commands I've been using are

git status
git add .
git commit -m 'message here'
git push origin master

I have pushed the project to remote a long time ago ago (I Capistrano for deployment), and all is working great.

Now I want to change the design of the site, but keep the logic intact. I'm guessing I need to create a new branch (let's call it newdesign) for that.

What I'm wondering, though, is this: if I'm working on the newdesign branch, and I see a bug in the master branch, how can I fix the bug on master and then integrate that bugfix in the newdesign branch, so that the latter be kept up to date with the actual logic?

jub0bs
  • 60,866
  • 25
  • 183
  • 186
BigJobbies
  • 499
  • 8
  • 20

2 Answers2

42

This is a classic case in which you can take advantage of Git branches. Here is the workflow you should use.

Let's say you've created a develop branch stemming from master and you've made a few commits on that branch:

enter image description here

Suddenly, you realise that there is a bug on master, a bug that you need to fix quickly. Instead of working directly off of master, you should create a short-lived bug-fix branch, to isolate the bug-fixing task from your master branch:

git checkout master
git checkout -b bugfix

enter image description here

After making one commit (or more) on the bugfix branch to fix the problem,

enter image description here

you should make sure that everything works as it should (run tests, etc.). When you're happy with the state of your code on bugfix, merge it into master:

git checkout master
git merge bugfix

enter image description here

At that stage, you can push your (now fixed) master branch to remote, and delete the bugfix branch:

git push origin master
git branch -d bugfix

Now, to integrate the latest changes on master into develop, you basically have two options.

  1. Merge master into develop, by running:

    git checkout develop
    git merge master
    

enter image description here

  1. Alternatively, rebase develop on top of master, by running:

    git checkout develop
    git rebase master
    

enter image description here

In either case, your develop branch will now contain the fix, and you can resume work on develop.

jub0bs
  • 60,866
  • 25
  • 183
  • 186
  • 3
    Owww ... Thank you so much for that ... That has answered my question quite well. – BigJobbies Sep 19 '14 at 13:12
  • The bugfix branch doesn't seem necessary. You can just commit directly to master then `git rebase master develop`. Also, `rebase` only works if develop is not public. – Mateen Ulhaq Jan 05 '20 at 22:54
  • 3
    @MateenUlhaq I prefer using a bugfix branch: if my initial attempt at fixing the bug is going nowhere, I can just delete the bugfix branch and restart from scratch at `master`, rather than having to clean up my botched attempt on `master`. – jub0bs Jan 13 '20 at 09:27
  • 1
    Thank you for the very clear and complete answer! – Fiver Aug 26 '21 at 15:26
  • @Fiver You're welcome. I appreciate it :) – jub0bs Aug 26 '21 at 16:54
  • 1
    @jub0bs I wish I could upvote this more than once. – Carl Boneri Jan 25 '22 at 16:26
1

let's suppose you have you master and dev branches.

You work on dev for your new features and you do your fix on master.

Then you checkout dev and merge master into dev. This way master is fixed and dev can profit from the fix you made AND keeps it's own history.

Alternatively, you can rebase dev on top of branch. This gives you a "cleaner" history in the sense that you don't have merge points.

See the git guide about merging: http://git-scm.com/docs/git-merge

vratojr
  • 818
  • 2
  • 8
  • 25
  • Hey @vratojr ... But if i merged master into dev, wouldnt i lose the work on the new design i was doing in dev? – BigJobbies Sep 19 '14 at 11:53
  • No you won't loose your changes in the dev branch ... what merge does is just copy the contents of one branch to the another branch where the target or another branch is running behind in commits – justrohu Sep 19 '14 at 12:27
  • Ohhh i think i understand @justrohu ... So it will just copy over the changes from where the dev branched off from the master? Anything before that doesnt matter – BigJobbies Sep 19 '14 at 12:50
  • @BigJobbies Yes it kinda copies that but if there are any other changes to Master branch those changes are saved too. – justrohu Sep 19 '14 at 14:11