2017年8月30日 星期三

[ Git 常見問題 ] Git merge master into feature branch

Source From Here 
Question 
Lets say we have the following situation in git: 
1. A created repository: 
# mkdir GitTest2
# cd GitTest2
# git init

2. Some modifications in the master take place and get committed. 
# echo "On Master" > file
# git commit -a -m "Initial commit"

3. Feature1 branched off master and some work is done: 
# git branch feature1
# git checkout feature1
# echo "Feature1" > featureFile
# git commit -a -m "Commit for feature1"

4. Meanwhile, a bug is discovered in the master-code and a hotfix-branch is established 
# git checkout master
# git branch hotfix1
# git checkout hotfix1

5. The bug is fixed in the hotfix branch and merged back into the master (perhaps after a pull request/code review): 
# echo "Bugfix" > bugfixFile
# git commit -a -m "Bugfix Commit"
# git checkout master
# git merge --no-ff hotfix1

6. Development on feature1 continues: 
# git checkout feature1

Now my question: Say I need the hotfix in my feature branch, maybe because the bug also occurs there. How can I achieve this without duplicating the commits into my feature branch? I want to prevent to get two new commits on my feature branch which have no relation to the feature implementation. This especially seems important for me if I use Pull Requests: All these commits will also be included in the Pull Request and have to be reviewed although this has already been done (as the hotfix is already in the master). 

I can not do a git merge master --ff-only: "fatal: Not possible to fast-forward, aborting.", but I am not sure if this helped me. 

How-To 
You should be able to rebase your branch on master
# git checkout feature1
# git rebase master

Manage all conflicts that arise. When you get to the commits with the bugfixes (already in master), git will say that there were no changes and that maybe they were already applied. You then continue the rebase (while skipping the commits already in master) with: 
# git rebase --skip

If you perform a git log on your feature branch, you'll see the bugfix commit appear only once, and in the master portion.

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...