变基与合并git rebase git merge

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了变基与合并git rebase git merge相关的知识,希望对你有一定的参考价值。

参考技术A git rebase -i a4ade219a399b70c936e2449450a03043ab1ae1f

https://git-scm.com/book/zh/v2/Git-%E5%88%86%E6%94%AF-%E5%8F%98%E5%9F%BA

https://git-scm.com/book/zh/v1/Git-%E5%B7%A5%E5%85%B7-%E9%87%8D%E5%86%99%E5%8E%86%E5%8F%B2

http://weizhifeng.net/git-rebase.html

参考下面的url:已经总结的很详细了

http://www.cnblogs.com/sjjg/p/4979417.html

总的原则:

只对尚未推送或分享给别人的本地修改执行变基操作清理历史,从不对已推送至别处的提交执行变基操作,这样,你才能享受到两种方式带来的便利。

markdown git rebase变基合并

## 变基合并(仅在你修改过后)
#### clone 自己的项目
`git clone --branch master https://github.com/maboloshi/hexo-theme-next.git`
> Cloning into 'hexo-theme-next'...
remote: Counting objects: 7563, done.
remote: Total 7563 (delta 0), reused 0 (delta 0), pack-reused 7563Receiving obje
cts 100% (7563/7563), 11.23 MiB | 910.00 KiB/s
Receiving objects: 100% (7563/7563), 11.33 MiB | 913.00 KiB/s, done.
Resolving deltas: 100% (4150/4150), done.
Checking out files: 100% (270/270), done.

#### 查看项目当前remote
`cd hexo-theme-next`
`git remote -v`
> origin  https://github.com/maboloshi/hexo-theme-next.git (fetch)
origin  https://github.com/maboloshi/hexo-theme-next.git (push)

#### 添加 new remote, "upstream" (上游):
`git remote add upstream https://github.com/iissnan/hexo-theme-next.git`
`git remote -v`
> origin  https://github.com/maboloshi/hexo-theme-next.git (fetch)
origin  https://github.com/maboloshi/hexo-theme-next.git (push)
upstream        https://github.com/iissnan/hexo-theme-next.git (fetch)
upstream        https://github.com/iissnan/hexo-theme-next.git (push)

#### 将该远程的所有分支获取到远程跟踪分支中,例如`upstream/master`:
`git fetch upstream`
> remote: Counting objects: 4469, done.
remote: Compressing objects: 100% (5/5), done.
Receiving objects:  99% (4425/4469remote: Total 4469 (delta 914), reused 916 (de

> Receiving objects: 100% (4469/4469), 1.67 MiB | 725.00 KiB/s, done.
Resolving deltas: 100% (2861/2861), completed with 184 local objects.
From https://github.com/iissnan/hexo-theme-next
> * [new branch]      dev        -> upstream/dev
>  * [new branch]      master     -> upstream/master
>   * [new branch]      servant    -> upstream/servant
>   * [new branch]      testing    -> upstream/testing
>   * [new tag]         v5.1.1     -> v5.1.1
>   * [new tag]         v5.1.2     -> v5.1.2
>   * [new tag]         v5.1.3     -> v5.1.3
>   * [new tag]         v5.1.4     -> v5.1.4

#### 确保你在你的`master`分支上
`git checkout master`
> Already on 'master'
Your branch is up to date with 'origin/master'.

#### 重写你的master分支,以便任何尚未处于`upstream/master`的提交都在该分支之上重播:
`git rebase upstream/master`
> First, rewinding head to replay your work on top of it...
Applying: 添加 disqus 点击载入功能.
Using index info to reconstruct a base tree...
M       _config.yml
M       layout/_partials/comments.swig
A       layout/_scripts/third-party/comments/disqus.swig
Falling back to patching base and 3-way merge...
CONFLICT (modify/delete): layout/_scripts/third-party/comments/disqus.swig delet
ed in HEAD and modified in 添加 disqus 点击载入功能.. Version 添加 disqus 点击载
入功能. of layout/_scripts/third-party/comments/disqus.swig left in tree.
Auto-merging layout/_partials/comments.swig
CONFLICT (content): Merge conflict in layout/_partials/comments.swig
Auto-merging _config.yml
CONFLICT (content): Merge conflict in _config.yml
error: Failed to merge in the changes.
Patch failed at 0001 添加 disqus 点击载入功能.
The copy of the patch that failed is found in: .git/rebase-apply/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".

冲突解决方式
1. 手动解决后继续合并
`git add  --all .`
`git rebase --continue`

2. 跳过冲突
`git rebase --skip`
3. 取消并恢复到合并之前
`git rebase --abort`

#### 如果你不想重写主分支的历史记录(例如,因为其他人可能已经克隆了它),那么你应该用 `git merge upstream / master`替换上一个命令。 但是,为了进一步提出尽可能干净的请求,最好重新绑定。

#### 如果您已将分支重定位到上游/主数据库,则可能需要强制推送以将其推送到GitHub上自己分叉的存储库。 你可以这样做:
合并后push到远程端
`git push -f origin master`
#### 
您只需在重新组装之后首次使用-f。
![snip20180315_6](https://user-images.githubusercontent.com/7850715/37467825-37248c6e-289c-11e8-89db-1e661f059526.png)

git push origin master

Tips:
    git协议由https变为ssh
    `git remote set-url origin git@github.com:someaccount/someproject.git`

修改最后一次comment
`git add/rm`
`git commit --amend`

以上是关于变基与合并git rebase git merge的主要内容,如果未能解决你的问题,请参考以下文章

Git~rebase

git rebase

对于git中变基操作的粗略认识

markdown git rebase变基合并

git变基 rebase最佳实践

git变基 rebase最佳实践