假设我们在Git中有以下情况:
创建的存储库:
mkdir GitTest2
cd GitTest2
git init
在master中发生并提交一些修改:
echo“On Master”>文件
初始提交
Feature1分支了master并完成了一些工作:
Git分支feature1
Git签出特性1
echo "Feature1" > featureFile
git commit -a -m commit for feature1
同时,在主代码中发现了一个bug,并建立了一个热修复分支:
Git checkout master
Git分支hotfix1
Git签出hotfix1
该错误在热修复分支中修复,并合并回主程序(可能在拉取请求/代码审查之后):
echo "Bugfix" > bugfixFile
git commit -a -m " bug修正提交"
Git checkout master
Git merge -no-ff hotfix1
feature1的开发仍在继续:
Git签出特性1
假设我需要在我的功能分支中进行热修复,可能因为bug也发生在那里。我怎样才能在不复制提交到我的特性分支的情况下实现这一点呢?
我想防止在我的特性分支上获得两个与特性实现无关的新提交。如果我使用拉请求,这对我来说尤其重要:所有这些提交也将包含在拉请求中,并且必须进行审查,尽管这已经完成了(因为热修复已经在master中)。
我不能做一个git合并主-ff-only: "fatal: not possible to fast-forward, aborting.",但我不确定这是否帮助了我。
In Eclipse -
1)结账主分支
Git Repositories ->Click on your repository -> click on Local ->double click master branch
->Click on yes for check out
2)拉主分支
Right click on project ->click on Team -> Click on Pull
3)检查你的功能分支(遵循第一点提到的相同步骤)
4)将master合并为feature
Git Repositories ->Click on your repository -> click on Local ->Right Click on your selected feature branch ->Click on merge ->Click on Local ->Click on Master ->Click on Merge.
5)现在你会在feature branch中看到Master branch的所有变化。消除冲突。
For conflict if any exists ,follow this -
Changes mentioned as Head(<<<<<< HEAD) is your change, Changes mentioned in branch(>>>>>>> branch) is other person change, you can update file accordingly.
注意-您需要为冲突文件添加索引
6)提交并在功能分支中推送你的更改。
Right click on project ->click on Team -> Click on commit -> Commit and Push.
OR
Git Repositories ->Click on your repository -> click on Local ->Right Click on your selected feature branch ->Click on Push Branch ->Preview ->Push
In Eclipse -
1)结账主分支
Git Repositories ->Click on your repository -> click on Local ->double click master branch
->Click on yes for check out
2)拉主分支
Right click on project ->click on Team -> Click on Pull
3)检查你的功能分支(遵循第一点提到的相同步骤)
4)将master合并为feature
Git Repositories ->Click on your repository -> click on Local ->Right Click on your selected feature branch ->Click on merge ->Click on Local ->Click on Master ->Click on Merge.
5)现在你会在feature branch中看到Master branch的所有变化。消除冲突。
For conflict if any exists ,follow this -
Changes mentioned as Head(<<<<<< HEAD) is your change, Changes mentioned in branch(>>>>>>> branch) is other person change, you can update file accordingly.
注意-您需要为冲突文件添加索引
6)提交并在功能分支中推送你的更改。
Right click on project ->click on Team -> Click on commit -> Commit and Push.
OR
Git Repositories ->Click on your repository -> click on Local ->Right Click on your selected feature branch ->Click on Push Branch ->Preview ->Push
git 合并
您可以按照以下步骤进行操作
1. 合并源/主分支到特征分支
# step1: change branch to master, and pull to update all commits
$ git checkout master
$ git pull
# step2: change branch to target, and pull to update commits
$ git checkout feature
$ git pull
# step3: merge master to feature(⚠️ current is feature branch)
$ git merge master
2. 将特征分支合并到原点/主分支
Origin /master是远程主分支,而master是本地主分支
$ git checkout master
$ git pull origin/master
$ git merge feature
$ git push origin/master
下面是一个脚本,您可以使用它将主分支合并到当前分支中。
该脚本的功能如下:
切换到主分支
拉动主分支
切换回当前分支
将主分支合并到当前分支中
将此代码保存为批处理文件(.bat),并将脚本放置在存储库中的任何位置。然后点击它运行它,你就设置好了。
:: This batch file pulls current master and merges into current branch
@echo off
:: Option to use the batch file outside the repo and pass the repo path as an arg
set repoPath=%1
cd %repoPath%
FOR /F "tokens=*" %%g IN ('git rev-parse --abbrev-ref HEAD') do (SET currentBranch=%%g)
echo current branch is %currentBranch%
echo switching to master
git checkout master
echo.
echo pulling origin master
git pull origin master
echo.
echo switching back to %currentBranch%
git checkout %currentBranch%
echo.
echo attemting merge master into %currentBranch%
git merge master
echo.
echo script finished successfully
PAUSE