JBoy
JBoy

Reputation: 5735

Undo a git rebase

Using intelliJ i am working on a feature branch, i wanted to integrate the changes my team did on master into my branch, but i messed it up.
I checked out master, from Git pop up i selected the feature branch and selected 'rebase current onto selected' thinking that this would have added the changes from master on top of feature branch, unfortunately it happened the opposite. So what i would like to do now is to undo this rebase on master, i would like to bring master back to the latest head commit.
Any advice?
I read that rebasing creates a new commit but i think that that did not go through because i have conflicts, when from git command line i run git status from master i see:

You are currently rebasing branch 'master' on 'FEATURE/....'.
  (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)

no changes added to commit (use "git add" and/or "git commit -a")

Upvotes: 6

Views: 23982

Answers (3)

Caleb
Caleb

Reputation: 124997

So what i would like to do now is to undo this rebase on master, i would like to bring master back to the latest head commit.

Presumably, you're working in your own local copy of a repository that's hosted on a server somewhere. In that case, there's a perfectly good version of the master branch on that server, and you just need to tell git to replace your local master with the one on the server. Let's say the shared repo is called upstream. You can do the following:

git checkout master

to make sure you're really on the master branch as you expect. You're about to erase all your local changes to the master branch, so it pays to make sure you're in the right place. Then:

git fetch upstream master
git reset --hard upstream/master

After this, your master should agree with the one on the server. For more on this topic, take a look at the related question, How do I force “git pull” to overwrite local files?.

Upvotes: 0

Dmitrii Smirnov
Dmitrii Smirnov

Reputation: 7528

If rebase has not finished, just abort it. If rebase gets stopped due to conflicts, a prompt to resolve them appear. If you cancel the merge prompt without solving the conflict, a notification should appear with options to Resolve conflicts, Continue, and Abort Rebase notification

In addition, with ongoing rebase, the action to Abort rebase is available at the top of the Branches popup

Branches popup

It is also available in VCS - Git menu. You could always use Find action (Ctrl+Shift+A) or Search everywhere (Double shift) to find it. Or use git rebase --abort in the command line, e.g. via builtin Terminal (Alt+F12 to open it)

If rebase has already finished, you should reset the current branch (which is still master) to the commit it used to be on. You could reset it to the same commit where origin/master sits unless there were unpushed commits.

If there were unpushed commits, you have to use git reflog on the command line to find out the hash of the commit, and then reset the current branch there.

thinking that this would have added the changes from master on top of feature branch, unfortunately it happened the opposite

BTW, IntelliJ shows hints clarifying the actions. When you select an action in the branches popup, the status bar shows the description of the action with exact branches names.

Upvotes: 15

Marcelo Idemax
Marcelo Idemax

Reputation: 2810

Any advice?

Use Git from command line first and just move to GUIs after get used to the daily commands.

I would suggest you to read a bit about git rebase in order to have a bit of knowledge to work with.

git-rebase - Reapply commits on top of another base tip

(from: https://git-scm.com/docs/git-rebase)

Answering...

You're in the middle of a rebase process and based on my XP on GUIs most of them doesn't handle rebase process properly.

Follow these steps:

  1. Open the repository folder in your terminal (linux, osx) or in the Git Bash (windows).
  2. Let's abort and start again, execute in the terminal: "git rebase --abort". This command will revert your master to the HEAD state before you start the rebase.

From now on let's make the rebase via terminal, but keep in mind that like merge, rebase also must to be started from the branch you want to update:

  1. git checkout master or the branch you want to get the new commits from your feature branch.
  2. git rebase my_feature_branch: please read the terminal output! You will have all instructions enough to accomplish this rebase.

PS: You might face many steps and many git rebase --continue but don't worry it's a normal Git behavior. Once you get a conflict, fix it as a ordinary merge, commit if needed and move on.

Upvotes: 2

Related Questions