git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: will git rebase has side effect

From
Kevin Daudt <me@ikke.info>
Date
Oct 1, 2017, 14:17 UTC
Message-ID
<20171001141719.GB20406@alpha.vpn.ikke.info>
In-Reply-To
<20171001134155.GA20406@alpha.vpn.ikke.info>
Forgot to cc the mailing list.
On Sun, Oct 01, 2017 at 09:23:23PM +0800, Yubin Ruan wrote:
Show 13 quoted lines
> Suppose that I have such a history of commit locally:
> 
> A --> B --> C --> D
> 
> If I then add a few more commits locally
> 
> A --> B --> C --> D --> E --> F --> G
> 
> And then I do a rebase and squash F and G into one single commit H.
> What side effect will this rebase have? How will this affect "git push
> origin master"?
> 
> Yubin
Hello Yubin,
So the situation is this:
            [origin/master]
                  |
A --> B --> C --> D --> E --> F --> G
                                    |
                                 [master] 
Then you squash (F' is the result of squashing F and G):
            [origin/master]
                  |
A --> B --> C --> D --> E --> F'
                              |
                           [master] 

When you want to push now, it's just as if you just created just two commits in the first place, and you can just push normally (assuming no one else has pushed in the mean time.

The situation is different when you have pushed already:
                              [origin/master]
                                    |
A --> B --> C --> D --> E --> F --> G
                                    |
                                 [master] 
Then after you squash, the history would look as follows:
                              [origin/master]
                                    |
A --> B --> C --> D --> E --> F --> G
                         \
                          --> F'
                              |
                           [master] 

This sitation would look to git like you created F', and someone else had pushed F and G. It will reject the push, saying you would need to merge these changes first (but you don't want this, because you are merging the same changes together).

To solve this, you can use git push --force-with-lease=master origin, which will force the push, overwriting the history the remote already had.

Hope this helps, Kevin.

nb. --force-with-lease is a safer version of just (-f|--force), because it will prevent you from overwriting history you don't have locally, for example when someone else did push already.

Previous: Yubin RuanNext: Yubin Ruan
Message 2 of 4 in “will git rebase has side effect”
  1. Yubin RuanOct 1, 2017
  2. Kevin DaudtOct 1, 2017
  3. Yubin RuanOct 1, 2017
  4. Kevin DaudtOct 1, 2017

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.