Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
581 views
in Technique[技术] by (71.8m points)

git - How can I do a bugfix on master and integrate it into my less stable branch(es)?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 which 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

  2. 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...