git pull --rebase的正确使用

在多人使用同一个远程分支合做开发的时候,极可能出现 push 代码的时候出现如下问题:git

$ git push origin master

# 结果以下
To github.com:hello/demo.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'git@github.com:hello/demo.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.
复制代码

很明显此时远程分支有新的 commit 未同步到本地,没法推送。正常状况下咱们会执行如下操做:github

$ git pull origin master

# 结果以下
remote: Counting objects: 2, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 2 (delta 1), reused 0 (delta 0)
Unpacking objects: 100% (2/2), done.
From github.com:hello/demo
   3824da0..f318a05  master     -> origin/master
Merge made by the 'recursive' strategy.
 one.md | 2 ++
 1 file changed, 2 insertions(+)
 
$ git push origin master

# 结果以下
Enumerating objects: 10, done.
Counting objects: 100% (8/8), done.
Delta compression using up to 8 threads
Compressing objects: 100% (8/8), done.
Writing objects: 100% (8/8), 1.24 KiB | 1.24 MiB/s, done.
Total 8 (delta 5), reused 0 (delta 0)
To github.com:hello/demo.git
   f318a05..1aefef1  master -> master
复制代码

确实 push 成功了,可是此时用 git log 查看如下提交记录:gitlab

$ git log

# 结果以下
commit 1aefef1a2bedbd3ebd82db8dcf802011a35a9888 (HEAD -> master, origin/master, origin/HEAD)
Merge: 24cfa5c f318a05
Author: hello <hello@qq.com>
Date:   Tue Jul 23 09:53:47 2019 +0800

    Merge branch 'master' of github.com:hello/demo

commit 24cfa5c3ad271e85ff0e64793bf2bcc9d700c233
Author: hello <hello@qq.com>
Date:   Tue Jul 23 09:50:06 2019 +0800

    feat: 新功能提交

commit f318a05b1a4cbc0a6cf8d7dc7d3fb99cbafb0363
Author: world <world@qq.com>
Date:   Tue Jul 23 09:48:20 2019 +0800

    feat: 其余功能提交

...
复制代码

你会发现多出了一条 merge commit,这个 commit 就是在执行 git pull origin master 的时候自动生成的。若是多人屡次如此操做,那么提交记录就会出现不少条这种自从生成的 merge commit,很是难看。fetch

要解决以上问题,再也不出现自动生成的 merge commit,那么只要在执行 git pull origin master 的时候带上 --rebase 便可:ui

$ git pull --rebase origin master

$ git push origin master
复制代码

项目示例

如今经过一个示例项目来示范以上命令的用法。项目(demo)的结构以下:this

# 在 demo 目录下执行如下命令

$ ls

# 结果以下
one.md  two.md

$ cat one.md

# 结果以下
hello one

$ cat two.md

#结果以下
hello two
复制代码

A、B两位开发同窗使用一个远程分支(master)合做开发。A同窗修改了 one.md 文件,提交了一个 commit 而且已经推送到远程分支 master:spa

$ cat one.md

# 结果以下
hello one

new feature for one

$ git push origin master

# 结果以下
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 8 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 1.24 KiB | 1.24 MiB/s, done.
Total 4 (delta 3), reused 0 (delta 0)
To github.com:hello/demo.git
   f318a05..1aefef1  master -> master
复制代码

此时B同窗不知情,也并未将远程分支的更新拉到本地。B同窗修改了 two.md 文件,也提交了一个 commit 并尝试推送到远程分支 master:版本控制

$ cat two.md

# 结果以下
hello two

new feature for two

$ git push origin master

# 结果以下
To github.com:hello/demo.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'git@github.com:hello/demo.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.
复制代码

发现报错,没法推送。此时执行如下命令:code

$ git pull --rebase

$ git push origin master
复制代码

便可成功推送。three

注意事项

  • 执行 git pull --rebase 的时候必须保持本地目录干净。即:不能存在状态为 modified 的文件。(存在Untracked files是不要紧的)
  • 若是出现冲突,能够选择手动解决冲突后继续 rebase,也能够放弃本次 rebase

执行 git pull --rebase 的时候必须保持本地目录干净

  1. modified 状态的文件

本地有受版本控制的文件改动的时候,执行以上命令:

$ git pull --rebase

# 结果以下
error: cannot pull with rebase: You have unstaged changes.
error: please commit or stash them.
复制代码

会出现以上报错,没法操做。

此时查看如下文件状态:

$ git status

# 结果以下
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	modified:   two.md

no changes added to commit (use "git add" and/or "git commit -a")
复制代码

就是由于本地有文件改动还没有提交形成的。此时有两种作法:

  • 若是本次修改已经完成,则能够先提交(commit)一下
  • 若是本次修改还没有完成,则能够先贮藏:git stash

作了以上一种操做以后再尝试 git pull --rebase,不会再报错。

若是作了 git stash 操做,此时能够经过 git stash pop 回到以前的工做状态。

  1. Untracked files

查看本地文件状态:

$ git status

# 结果以下
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	three.md

no changes added to commit (use "git add" and/or "git commit -a")
复制代码

若是是以上这种状况,直接执行 git pull --rebase 是不会有问题的。

若是出现冲突,能够选择手动解决冲突后继续 rebase,也能够放弃本次 rebase

在上面的示例项目中,若是 A、B 同窗修改了同一个文件。那么颇有可能会出现冲突,当 B 同窗来执行命令的时候会出现以下情况:

$ git pull --rebase

# 结果以下
remote: Counting objects: 6, done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 6 (delta 1), reused 0 (delta 0)
Unpacking objects: 100% (6/6), done.
From gitlab.lrts.me:fed/gitlab-merge
   93a1a93..960b5fc  master     -> origin/master
First, rewinding head to replay your work on top of it...
Applying: feat: 其余功能提交
Using index info to reconstruct a base tree...
M	one.md
Falling back to patching base and 3-way merge...
Auto-merging one.md
CONFLICT (content): Merge conflict in one.md
error: Failed to merge in the changes.
Patch failed at 0001 feat:其余功能提交
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".
复制代码

这种状况下,能够手动打开 one.md 文件解决冲突,而后再执行:

$ git add one.md

$ git rebase --continue
复制代码

解决问题。

也能够用 git rebase --abort 放弃本次 rebase 操做。

总结

多人基于同一个远程分支开发的时候,若是想要顺利 push 又不自动生成 merge commit,建议在每次提交都按照以下顺序操做:

$ git stash

$ git pull --rebase

$ git push

$ git stash pop
复制代码
相关文章
相关标签/搜索