Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git rebase removing more recent changes?

Tags:

git

I have a master branch and two working branches, a and b. Branch b was created from the same point as a but touches different files (only one shared, d.py). Branch b was just pulled into master.

                (a)   F---H-J----L---
                     /
(master)A--B--C--D--E--------------M
                     \            /
                 (b)  G----I----K

In my pull request for branch a Github now says

This branch has conflicts that must be resolved

Use the web editor or the commandline to resolve conflicts.

Conflicting files

clippy/d.py

I thought I could do the obvious, git pull --rebase master and end up with a history that looks like this:

                             F---H-J----L--
                            /            
A--B--C--D--E--------------M
             \            /
              G----I----K

And then merge into master and everything would be great

                             F---H-J----L
                            /            \
A--B--C--D--E--------------M--------------N
             \            /
              G----I----K

When I attempted the merge this is what I got

You are currently rebasing branch 'bug/1500/Things' on '1234567'.
  (fix conflicts and then run "git rebase --continue")
  (use "git rebase --skip" to skip this patch)
  (use "git rebase --abort" to check out the original branch)

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   apps/a.py
    modified:   apps/b.py
    modified:   apps/c.py

Unmerged paths:
  (use "git reset HEAD <file>..." to unstage)
  (use "git add <file>..." to mark resolution)

    both modified:   apps/d.py

And I assumed that everything would be great. But then I took a look at a.py and realized all my changes from commits F, H, G and L had disappeared. Same for b.py, c.py etc. As far as I can tell, the only changes that stuck in the rebase were the ones in d.py which was the file shared between the two branches. I aborted the rebase and tried again with just git rebase master but the same thing happened.

Why is git doing this? And how can I successfully rebase my commit to merge it?

like image 481
Indigo Avatar asked Apr 20 '18 15:04

Indigo


People also ask

Does git rebase remove history?

The history can be messed up using rebase, but normally remote repo will not accept the change that modifies the history (unless you use git push --force), but even more, the developer with push permission can delete the branch completely (git push origin :branch-name).

Why you should avoid git rebase?

Case 1: We should not do Rebase on branch that is public, i.e. if you are not alone working on that branch and branch exists locally as well as remotely rebasing is not a good choice on such branches and it can cause bubble commits.

Does rebase rewrite history?

Changing older or multiple commits. To modify older or multiple commits, you can use git rebase to combine a sequence of commits into a new base commit. In standard mode, git rebase allows you to literally rewrite history — automatically applying commits in your current working branch to the passed branch head.

How do I stop rebase abortion?

You can run git rebase --abort to completely undo the rebase. Git will return you to your branch's state as it was before git rebase was called. You can run git rebase --skip to completely skip the commit.


2 Answers

If there are conflicts between master and your a branch, the rebase will stop at the point where the conflict exists and expects you to fix it.

First off, to get to the original state of your branch, you can do git rebase --abort. This will undo the attempted rebase.

If you are still in the middle of the rebase, you can do git diff to see where the conflicts are. You'll see something like this in the conflict files

<<<<<<< HEAD
...
=======
....
>>>>>>> branch-a

You can edit the file to fix the conflicts by choosing the code that you want to keep and removing the <<<< ===== >>>>>>> lines.

Once you've finished editing the files and conflicts, you can run git add . and then git rebase --continue. The rebase will continue moving your commits up to HEAD. At any point, you may or may not run into more conflicts. If you don't, you'll see that the rebase worked successfully and then you'll be able to push your changes to your branch. Since you've rebased, however, you'll only be able to do perform a forced push since your commit refs will have changed from the ones on the remote.

like image 77
Leo Correa Avatar answered Oct 01 '22 06:10

Leo Correa


Same thing happened to me. What's happening is you are still in the middle of a rebase and it's not done so your latest up to date modifications should still be ok if it was committed.

The rebase is going back from the beginning of all the commits in the past that you might have to catch up on (for instance, if your other developer made 10 commits from a few days ago that you need to integrate in your app). Or simply don't accept it or whatever fits best for your app.

Then hit the "+" to add it/include it onto your local branch. (No need to commit/push since you're just bringing it onto your local branch to work off of that).

Then:

git rebase --continue

Then go one by one through all the old commits that have conflicts with your newest commit until you get to your latest which should be reassuring once you see those recent modifications that look normal.

like image 31
p_c Avatar answered Oct 01 '22 07:10

p_c