Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does `git rebase` apply all of the commits rather than just applying the last one?

Tags:

git

git-rebase

I'm aware of how git rebase works, and I've been using it in production for a while. It replays all of the commits of a branch onto another branch. It rewrites history of your current branch.

I've recently started playing around with git rebase --skip, although I was initially terrified of it. I realize it just skips that commit as if it never happened.

It seems to me that the resulting working directory would be the same in these two cases:

1) all of the commits are skipped except the most recent

2) all of the commits are replayed, such that the commits are resolved in favor of their branch.

Reminder: their is the branch that you are currently on when you rebase.
Other reminder: I'm specifically interested in the working directory. Is this the same?

Is it true that the working directory is the same result?

like image 957
makansij Avatar asked Sep 09 '15 19:09

makansij


People also ask

Why does git rebase create new commits?

A rebase will sequentially take all the commit from the branch you're in, and reapply them to the destination. This behavior has 2 main implications: By reapplying commits git creates new ones. Those new commits, even if they bring the same set of change will be treated as completely different and independent by git.

What is the main issue with git rebase?

The golden rule of git rebase is to never use it on public branches. The rebase moves all of the commits in main onto the tip of feature . The problem is that this only happened in your repository. All of the other developers are still working with the original main .

What happens if you skip a commit during rebase?

You can run git rebase --skip to completely skip the commit. That means that none of the changes introduced by the problematic commit will be included. It is very rare that you would choose this option. You can fix the conflict.

Does git rebase change both branches?

Unlike a merge, which merges two branches in one go, rebasing applies the changes from one branch one by one.


1 Answers

In general, no. A replay of a commit is very similar to the cherry-pick operation. Not the state of the repository is applied, only the patch (=diff, changeset, ...) corresponding to that commit. If you skip commits, you omit the changeset, which may or may not result in conflicts. Either way, only skipping empty commits yields the same results in general.

However, if you perform an interactive rebase and squash all commits into one, the resulting work tree should be the same. Be sure to know about Undoing a git rebase if necessary. (Or better, perform all rebasing operations in an one-off branch, branched off the original "their" branch. Then, you can always double-check if the results have changed by comparing that rebased one-off branch with the "their" branch, and update the "their" branch only after everything is good.)

like image 96
krlmlr Avatar answered Oct 23 '22 02:10

krlmlr