Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there some kind of 'git rebase --dry-run', which would notify me of conflicts in advance?

I'm trying to script rebasing and my script will take different paths depending on if the rebase results in any conflicts.

Is there a way to determine if a rebase would result in conflicts before executing the rebase?

like image 881
Jonathan.Brink Avatar asked Apr 08 '15 14:04

Jonathan.Brink


People also ask

Does git rebase have conflicts?

When you perform a git rebase operation, you're typically moving commits around. Because of this, you might get into a situation where a merge conflict is introduced. That means that two of your commits modified the same line in the same file, and Git doesn't know which change to apply.

How do you check if there will be merge conflicts?

To see the beginning of the merge conflict in your file, search the file for the conflict marker <<<<<<< . When you open the file in your text editor, you'll see the changes from the HEAD or base branch after the line <<<<<<< HEAD .

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 .


2 Answers

At the time of writing (Git v2.6.1 v2.10.0), the git rebase command offers no --dry-run option. There is no way of knowing, before actually attempting a rebase, whether or not you're going to run into conflicts.

However, if you run git rebase and hit a conflict, the process will stop and exit with a nonzero status. What you could do is check the exit status of the rebase operation, and, if it is nonzero, run git rebase --abort to cancel the rebase:

git rebase ... || git rebase --abort 

And what if the rebase is successful but you realise that you want to undo it, you can run

git reset --hard ORIG_HEAD 
like image 125
jub0bs Avatar answered Sep 24 '22 14:09

jub0bs


If you just want to see if the rebase would be successful but then you want to "roll back," you can alway reposition the branch tip back to the original commit. Just tag or make a note of the original SHA.

Or perhaps easier, create a new temporary branch in which to "stage" the rebase:

git checkout your-branch git checkout -b tmp git rebase other-branch 

If it was successful but you want to "roll back," your-branch is untouched. Just git branch -D tmp and you're back to where you started from.

If there were conflicts and you did some work to resolve them and you now you want to keep the rebase, just reposition your-branch tip to tmp (and then git branch -D tmp).

like image 36
joneit Avatar answered Sep 22 '22 14:09

joneit