24

I've been working on a local copy of a remote git repo. I created a branch on my local copy, let's call it 'my_branch'. I've committed a few times on my_branch.

I recently pushed 'my_branch' to remote. However I didn't know that someone else added a version to the remote master. So, I fetched it to my local master.

So...long story short, my local repo looks like this (I'm trying to use the diagraming convention here) .

 --C0--------------C7--  (local master)
      \
       --C1-C2-C3--     (local my_branch)
             \
              --C4-C5-C6--     (local sandbox_branch)

I want it to look like:

 --C0--------------C7--  (local master)
                      \
                       --C1'-C2'-C3'--     (local my_branch)
                             \
                              --C4'-C5'-C6'--     (local sandbox_branch)

I tried to rebase my_branch ONTO local master but I got this error message (I'm using a visual tool for git called GitX):

Rebase Failed!
There was an error rebasing HEAD with branch 'master'.

command: git rebase refs/heads/master

It seems that I cannot create a rebase-apply directory, and
I wonder if you are in the middle of patch application or another
rebase.  If that is not the case, please
    rm -fr /my_project_directory/.git/rebase-apply
and run me again.  I am stopping in case you still have something
valuable there.

What am I doing wrong? How should I handle this? If I were to do this on the command line what is the command to get me to the state in the diagram above?

UPDATE 1

BTW, I'm not in the middle of an application patch or another rebase...at least not intentional. After I found out that remote was updated AFTER I pushed, I did a fetch. Could that have done anything to make GitX think that I'm in the middle of an application patch or another rebase?

I've also updated the diagram to be more accurate. There is a branch off of my_branch. I didn't include it in the original question b/c I didn't think that it would matter. I'm including just in case...

UPDATE 2

FYI...The master tree for 'local' and for 'remote' looks like the diagram that I drew, except it doesn't have the sandbox_branch.

milesmeow
  • 3,688
  • 5
  • 35
  • 58
  • 1
    Have you tried `git rebase --abort`? – Fred Foo Jan 08 '12 at 18:56
  • "It seems that I cannot create a rebase-apply directory, and I wonder if you are in the middle of patch application or another rebase" <-- is this the case? – fge Jan 08 '12 at 18:56
  • OK, so is there actually a directory named like what git says there is? What does `git status` say? – fge Jan 08 '12 at 19:20
  • @larsmans : running `git rebase --abort` gives me `HEAD is now at 445ef4b REMOVED "ADD AN ALBUM" BUTTON` When I look at the branch diagrams in GitX...nothing seemed to have changed. – milesmeow Jan 08 '12 at 19:30
  • @fge : `git status` gives me `# On branch lite_main nothing to commit (working directory clean)`. Note: This is after the `git rebase --abort`. I've attempted to rebase again using GitX. I think it ran a `git rebase master lite_main`. Now I'm getting merger conflicts...which I think is what I'm expecting. :) Does that sound right? I'm still in my lite_main branch. So I should resolve the conflicts while working on that branch, yes? And then run `git rebase --continue` right? – milesmeow Jan 08 '12 at 19:48

2 Answers2

40

git rebase has found a .git/rebase-apply directory and so presumes that you might be in the middle of a rebase. This would have happened if there was a conflict during a previous rebase and the rebase was not finished; i.e. you did not run one of git rebase --abort, git rebase --skip or git rebase --continue (the last one after resolving the conflict).

Anyway, it does not matter how you ended up in this state if you don't think you ran git rebase at all. Simply rm -fr /my_project_directory/.git/rebase-apply as the help suggests and you should be able to do the rebase now.

But wait. Since you say that you have already published your branch to the remote repository, you should not try to rebase master onto it. In fact, if your remote is set to deny non-fast-forward commits (which seems to be a generally recommended best practice), you'll not even be able to push the rebase'd changes to your remote. In general, it is a bad practice to try to modify a commit (which is what git rebase does) after you have published it to a remote.

Siddhartha Reddy
  • 6,130
  • 1
  • 33
  • 20
  • Uh oh...So what should I do? I'm a pretty new to git. `git rebase --abort` first? FYI...The master tree for 'local' and for 'remote' looks like the diagram that I drew, except it doesn't have the sandbox_branch. – milesmeow Jan 08 '12 at 20:06
  • I guess I could potentially merge my changes onto master locally...and then push to remote...is that the way to go? – milesmeow Jan 08 '12 at 20:33
  • Do you want to publish these changes to the remote master? If yes, you should merge the branch into master and push to remote. If you just want to ensure that the latest changes from master are also part of your branch, merge master into your local branch and push that to your remote. – Siddhartha Reddy Jan 09 '12 at 02:30
  • I tried to `git rebase` and resolved conflicts (didn't see your comment until it was too late) and also `git reset --hard` to try to fix my local repo. I think I've basically screwed up my local repo...So, I've basically freshly cloned the remote repo again. Checked out my_branch as a tracking branch. Now how do I 'merge the branch into master and push to remote'? – milesmeow Jan 09 '12 at 08:06
  • Ensure you have master checked out. Then: `git merge origin/` and `git push origin master`. I'm assuming your remote is called origin. – Siddhartha Reddy Jan 09 '12 at 14:20
2

Go to your project directory. Find rebase-apply folder under .git and delete the folder.

Note : By default .git folder will be hidden.

Sai Prasad
  • 131
  • 9