HowiedeiMac:ganlin howie$ git push origin master
To https:///greenhn/ganlin.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https:///greenhn/ganlin.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
HowiedeiMac:hello howie$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
直接合并feature分支
git merge feature
HowiedeiMac:hello howie$ git merge feature
Auto-merging newFunc.go
CONFLICT (content): Merge conflict in newFunc.go
Automatic merge failed; fix conflicts and then commit the result.
竟然失败了,说明我两个分支之前的版本已经不同步了,需要手动合并冲突,再提交:
先查看冲突文件:git status
HowiedeiMac:hello howie$ git status
On branch master
Your branch is ahead of 'origin/master' by 7 commits.
(use "git push" to publish your local commits)
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: newFunc.go
打开文件,进行修改
原文件:
func NewFunc() {
<<<<<<< HEAD
=======
fmt.Println("add new func")
>>>>>>> feature
}
修改后:
func NewFunc() {
fmt.Println("add new func")
}
现在通过add添加,然后commit提交
HowiedeiMac:hello howie$ git add newFunc.go
HowiedeiMac:hello howie$ git commit -m 'merge master and feature'
[master 562ec58] merge master and feature
现在在查看一下分支提交历史:
HowiedeiMac:hello howie$ git log --oneline --graph
* 562ec58 (HEAD -> master) merge master and feature
|\
| * 4f58ab8 (feature) add new func
* | 0e80f97 do something
|/
* 94c134b init base
虽然合并成功,但是Master已经保存了合并历史,出现开叉了!对于强迫症患者来说肯定是不能接受的。
通过rebase合并分支:
现在将版本退回到合并前,也就是回退一个版本
git reset --hard head^
HowiedeiMac:hello howie$ git reset --hard head^
HEAD is now at 0e80f97 do something
HowiedeiMac:hello howie$ git log --oneline --graph
* 0e80f97 (HEAD -> master) do something
* 94c134b init base
退回去了,现在是位于master分支的init base提交这里。
先切换回feature分支:
HowiedeiMac:hello howie$ git checkout feature
Switched to branch 'feature'
HowiedeiMac:hello howie$ git rebase master
First, rewinding head to replay your work on top of it...
Applying: add new func
Using index info to reconstruct a base tree...
M newFunc.go
Falling back to patching base and 3-way merge...
Auto-merging newFunc.go
CONFLICT (content): Merge conflict in newFunc.go
error: Failed to merge in the changes.
Patch failed at 0001 add new func
hint: Use 'git am --show-current-patch' to see the failed patch
Resolve all conflicts manually, mark them as resolved with
"git add/rm <conflicted_files>", then run "git rebase --continue".
You can instead skip this commit: run "git rebase --skip".
To abort and get back to the state before "git rebase", run "git rebase --abort".
失败了,原因很简单,两个分支修改个同一个文件,产生了冲突。所以先需要解决冲突:
打开冲突的文件,解决冲突
原文件:
func NewFunc() {
<<<<<<< HEAD
=======
fmt.Println("add new func")
>>>>>>> add new func
}
修改后:
func NewFunc() {
fmt.Println("add new func")
}
现在通过add添加
HowiedeiMac:hello howie$ git add newFunc.go
现在是重点,之前的rebase其实只是完成了一半,由于出现冲突而终止,现在冲突解决,可以通过git rebase —continue继续完成之前的rebase操作。
HowiedeiMac:hello howie$ git rebase --continue
Applying: add new func
rebase完成,再查看一下提交历史:
HowiedeiMac:hello howie$ git log --oneline --graph
* b2593e6 (HEAD -> feature) add new func
* 0e80f97 (master) do something
* 94c134b init base
提交记录已经是一条完美的直线。现在切换到主分支master,将feather分支上的提交合并过来。
git checkout master
git merge feature
HowiedeiMac:hello howie$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 7 commits.
(use "git push" to publish your local commits)
HowiedeiMac:hello howie$ git merge feature
Updating 0e80f97..b2593e6
Fast-forward
newFunc.go | 1
1 file changed, 1 insertion( )
再次查看一下提交历史:
HowiedeiMac:hello howie$ git log --oneline --graph
* b2593e6 (HEAD -> master, feature) add new func
* 0e80f97 do something
* 94c134b init base