stackoverflow上的一个帖子,楼主和后面的回答都详细到位,很好的解释了这个问题
总结git rebase:这是一个 更改历史 的操作,所以不要指望你改了历史然后就顺利把你篡改够的历史提交上去。
PS:pro git 上的一句话:
“永远不要衍合那些已经推送到公共仓库的更新。
如果你遵循这条金科玉律,就不会出差错。否则,人民群众会仇恨你,你的朋友和家人也
会嘲笑你,唾弃你。” 看到这句话你大概就知道该怎么用了。至少不会用坏。
http://stackoverflow.com/questions/8939977/git-push-rejected-after-feature-branch-rebase
1楼
OK, I thought this was a simple git scenario, what am I missing?
I have a master
branch and a feature
branch. I do some work on master
, some on feature
, and then some more on master
. I end up with something like this (lexicographic order implies order of commits):
A--B--C------F--G
\
D--E
I have no problem to git push origin master
to keep the remote master
updated, nor with git push origin feature
(when on feature
) in order to maintain a remote backup for my feature
work. Up until now, we're good.
But now I wanna rebase feature
on top of the F--G
commits on master, so I git co feature
and git rebase master
. Still good. Now we have:
A--B--C------F--G
\
D--E
(yeah, I know, it's actually D'
and E'
.)
Problem: the moment I want to backup the new rebased feature
branched with git push origin feature
, the push is rejected since the tree has changed due to the rebasing. This can only be solved with git push --force origin feature
.
I hate using --force
without being sure I need it. So, do I need it? Does the rebasing necessarilyimply that the next push
should be --force
ful?
This feature branch is not shared with any other devs, so I have no problem de facto with the forced push, I'm not gonna lose any data, the question is more conceptual.
2楼
The problem is that git push
assumes that remote branch can be fast-forwarded to your local branch, that is that all the difference between local and remote branches is in local having some new commits at the end like that:
Z--X--R <- origin/some-branch (can be fast-forwarded to Y commit)
\
T--Y <- some-branch
When you perform git rebase
commits D and E are applied to new base and new commits are created. That means after rebase you have smth like that:
A--B--C------F--G--D'--E' <- feature-branch
\
D--E <- origin/feature-branch
In that situation remote branch can't be fast-forwarded to local. Though, theoretically local branch can be merged into remote (obviously you don't need it in that case), but as git push
performs only fast-forward merges it throws and error.
And what --force
option does is just ignoring state of remote branch and setting it to the commit you're pushing into it. So git push --force origin feature-branch
simply overridesorigin/feature-branch
with local feature-branch
.
In my opinion, rebasing feature branches on master
and force-pushing them back to remote repository is ok as long as you're the only one who works on that branch.
3楼
One solution to this is to do what msysGit's rebasing merge script does - after the rebase, merge in the old head of feature
with -s ours
. You end up with the commit graph:
A--B--C------F--G (master)
\ \
\ D'--E' (feature)
\ /
\ --
\ /
D--E (old-feature)
... and your push of feature
will be a fast-forward.
In other words, you can do:
git checkout feature
git branch old-feature
git rebase master
git merge -s ours old-feature
git push origin feature
(Not tested, but I think that's right...)
4楼
What is wrong with a git merge master
on the feature
branch? This will preserve the work you had, while keeping it separate from the mainline branch.
A--B--C------F--G
\ \
D--E------H
Edit: Ah sorry did not read your problem statement. You will need force as you performed a rebase
. All commands that modify the history will need the --force
argument. This is a failsafe to prevent you from losing work (the old D
and E
would be lost).
So you performed a git rebase
which made the tree look like (although partially hidden as D
andE
are no longer in a named branch):
A--B--C------F--G
\ \
D--E D'--E'
So, when trying to push your new feature
branch (with D'
and E'
in it), you would lose D
and E
.
5楼
Other's have answered your question. If you rebase a branch you will need to force to push that branch.
Rebase and a shared repository generally do not get along. This is rewriting history. If others are using that branch or have branched from that branch then rebase will be quite unpleasant.
In general, rebase works well for local branch management. Remote branch management works best with explicit merges (--no-ff).
We also avoid merging master into a feature branch. Instead we rebase to master but with a new branch name (e.g adding a version suffix). This avoids the problem of rebasing in the shared repository.
相关推荐
Git实战IDEA操作详解 Git是一款分布式版本控制系统,它在软件开发中扮演着至关重要的角色,帮助团队协作和代码管理。IntelliJ IDEA(简称IDEA)是广受欢迎的Java集成开发环境,同样也提供了强大的Git集成功能。本文...
- git push:将本地的提交推送到远程仓库。这通常用于将本地开发的成果共享到团队协作的中心仓库。 - git pull:从远程仓库获取最新的提交并合并到当前分支。这个命令是获取他人更新的快捷方式。 3. 分支(Branch...
- **示例**: 如果重构过程中出现问题,可以使用`git rebase --abort`来取消操作。 ##### 继续重构 **命令**: `git rebase --continue` - **用途**: 解决冲突后继续重构过程。 - **参数**: 无。 - **示例**: 解决完...
- 使用`git merge`和`git rebase`进行分支合并。 - **Git的数据管理机制**: - 基于SHA1哈希算法对文件进行标识。 - 使用对象数据库存储数据。 - **Git的工作流**: - 特征分支工作流。 - Gitflow工作流。 - ...
### Git用户手册知识点详解 #### 一、仓库与分支(Repositories and Branches) **1. 如何获取一个Git仓库** - **初始化本地仓库**:如果你正在启动一个新的项目,可以通过`git init`命令来创建一个新的Git仓库。...
Git详解系列旨在深入探讨Git这一版本控制系统,帮助用户掌握其核心概念、常用命令以及最佳实践。以下是关于Git的详尽解析: 1. **Git起步**: Git是Linux之父Linus Torvalds为管理Linux内核开发而创建的分布式版本...
#### 二、Git命令详解 ##### 2.1 分支管理 - **创建分支**: - `git branch <branch-name>`: 创建一个新的分支,但不会立即切换到该分支。 - `git checkout -b <branch-name>` 或 `git switch -c <branch-name>` ...
3. 自动添加所有修改后直接提交:`git commit -a` 4. 提交时显示所有差异:`git commit -v` 5. 修改上一次提交的信息:`git commit --amend -m [message]` 6. 重做上一次提交并包含新变化:`git commit --amend ...
### 字节跳动《Git学习手册》核心知识点详解 #### 一、Git基础概念与原理 **Git** 是一种分布式版本控制系统,它可以帮助开发者高效地管理项目代码的变更历史。相较于传统的集中式版本控制系统(如SVN),Git具有...
### Git Cheat Sheet详解 #### 一、概述 `git-cheatsheet` 是一份关于 Git 的操作指南,旨在帮助用户快速掌握 Git 的基本命令与用法。Git 作为一款分布式版本控制系统,广泛应用于软件开发过程中,用于跟踪代码...
### Git命令大全知识点详解 #### 一、概述 在软件开发过程中,版本控制工具是必不可少的一部分,而Git作为其中的佼佼者,被广泛应用于个人项目到大型企业级项目的版本管理之中。本篇将根据提供的内容,深入解析Git...
### Git Cheat Sheet详解 #### 创建操作 ##### 从现有仓库创建 - **命令**: `git clone <repository_url> <destination_directory>` - **说明**: 如果你需要从一个已经存在的远程仓库克隆到本地,可以使用`git ...
在熟练掌握基础操作后,你将需要了解更高级的技巧,如撤销更改(`git reset`、`git checkout`)、合并分支(`git merge`)、解决冲突(手动编辑冲突文件或使用`git merge --abort`、`git mergetool`)以及创建和...
Git 是一个分布式版本控制系统,由Linux之父Linus Torvalds于2005年创建,主要用于管理...通过阅读《Git 原理详解及实用指南》,你将能够系统地学习Git,提升你的版本控制能力,从而更高效地进行团队协作和项目管理。
3. **衍合(Rebase)**:`git rebase`将你的分支上的提交移动到另一个分支的历史之上,使分支历史更加简洁。 4. ** Cherry-pick**:选择并应用另一个分支的特定提交到当前分支。 以上只是Git庞大功能集的一部分,...
### Git的使用与创建知识点详解 #### Git简介与特点 - **定义**:Git是一种分布式版本控制系统,由Linus Torvalds开发,主要用于管理和跟踪计算机软件的开发过程中的变更。 - **分布式特性**:Git的工作目录是完全...
### Git常用命令详解 #### 一、初始化与克隆 - **克隆现有仓库** - 命令: `git clone ssh://user@domain.com/repo.git` - 描述: 克隆一个位于远程服务器上的现有仓库到本地。 - 注意事项: 确保SSH连接设置正确。...
安装Git后,你将学习如何初始化一个新的Git仓库(`git init`),克隆远程仓库(`git clone`),添加文件到暂存区(`git add`),以及提交更改(`git commit`)。同时,理解分支(`git branch`)和合并(`git merge`...