Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1103851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:23:59+00:00 2026-05-17T01:23:59+00:00

I want to enforce (i.e. throw an error and fail) that whenever I do

  • 0

I want to enforce (i.e. throw an error and fail) that whenever I do a git merge I don’t have any unstaged changes, much in the same why a git rebase will not work if unstaged changes exist. Is there a way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T01:24:00+00:00Added an answer on May 17, 2026 at 1:24 am

    It ain’t pretty, but it works. I’d be interested in seeing a better solution.

    Git already rejects the merge if the file to be merged is dirty:

        $ git init
        Initialized empty Git repository in /tmp/mmm/.git/
        $ echo this is file1 > file1
        $ git add file1 
        $ git ci -m'first commit' file1 
        [master (root-commit) efd89a6] first commit
         1 files changed, 1 insertions(+), 0 deletions(-)
         create mode 100644 file1
        $ git co -b branch1
        Switched to a new branch 'branch1'
        $ echo change file1 >> file1 
        $ git ci -m'change on a branch' file1 
        [branch1 031e317] change on a branch
         1 files changed, 1 insertions(+), 0 deletions(-)
        $ git co master
        Switched to branch 'master'
        $ echo change on master >> file1 
        $ git merge branch1
        Updating efd89a6..031e317
        error: Your local changes to 'file1' would be overwritten by merge.  Aborting.
        Please, commit your changes or stash them before you can merge.
    

    Continuing the example:

        $ git ci -m'commit change on master' file1 
        [master 8a2d52c] commit change on master
         1 files changed, 1 insertions(+), 0 deletions(-)
        $ git merge branch1
        Auto-merging file1
        CONFLICT (content): Merge conflict in file1
        Automatic merge failed; fix conflicts and then commit the result.
        $ vi file1 
        $ git add file1 
        $ git ci
        [master ee16606] Merge branch 'branch1'
        $ git log --oneline
        ee16606 Merge branch 'branch1'
        8a2d52c commit change on master
        031e317 change on a branch
        efd89a6 first commit
        $ echo this is file2 > file2
        $ git add file2
        $ git co -b branch2
        A       file2
        Switched to a new branch 'branch2'
        $ echo change on branch2 >> file2 
        $ git ci -m'commit on branch2' file2 
        [branch2 06ff9c3] commit on branch2
         1 files changed, 2 insertions(+), 0 deletions(-)
         create mode 100644 file2
        $ git co master
        Switched to branch 'master'
        $ echo this change logically conflicts with the change to file2 on branch2 >> file1 
    

    It is the next command that you want to force a failure — you want to avoid this merge when your tree is dirty. You can do this in the prepare-commit-msg hook, but only if you do –no-ff merges, and don’t let the merge automatically commit. Here’s an outline of the hook:

    #!/bin/sh
    case "$2,$3" in
      merge,)
        echo "Check here if the working tree is dirty. If it is, fail."
        exit 1
        ;;
      *) ;;
    esac
    

    You have to use –no-ff and –no-commit, on every branch that you’d be merging into and want to protect from merges like this:

        git config branch.master.mergeoptions "--no-commit --no-ff"
    

    And here’s what the session would look like:

        $ git merge branch2
        Automatic merge went well; stopped before committing as requested
        $ git ci
        IN THE PREP HOOK: .git/COMMIT_EDITMSG, merge, 
        Check here if the working tree is dirty. If it is, fail.
        $ git status
        # On branch master
        # Changes to be committed:
        #
        #       modified:   file2
        #
        # Changed but not updated:
        #   (use "git add ..." to update what will be committed)
        #   (use "git checkout -- ..." to discard changes in working directory)
        #
        #       modified:   file1
        #
        bstpierre@bstpierre 1119 /tmp/mmm master
        $ git ci
        IN THE PREP HOOK: .git/COMMIT_EDITMSG, merge, 
        Check here if the working tree is dirty. If it is, fail.
        bstpierre@bstpierre 1120 /tmp/mmm master
        $ echo $?
        1
    

    And undoing back to the pre-merge state:

        $ git reset --merge
        $ git status
        # On branch master
        # Changed but not updated:
        #   (use "git add ..." to update what will be committed)
        #   (use "git checkout -- ..." to discard changes in working directory)
        #
        #       modified:   file1
        #
        no changes added to commit (use "git add" and/or "git commit -a")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to enforce explicit conversion between structs kind of like native types: int
I have a column that should contain one of values of 2 power n:
I have a RESTful URL that requires either the offset or the prefix request
I want to develop a webpart that allows editors to amend its content using
I'm currently looking to convert an existing SVN repository to git, and then to
I'm adding a password reset feature to my Rails application that uses Authlogic. I
I want to build a service which needs to get this data from some
I'm using NUnit 2.5.3, but if a more recent version of NUnit solves my
Suppose: struct P { P(int v); }; int v; P p = 0; //

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.