-4

I've run into this situation about 3 or 4 times now. On occasion, Git will want to perform a merge. It happens in response to a git commit <filename> -m <message> followed by a git push.

I know a merge is about to occur because Git spins up the editor for the message. So I perform a CTRL + C in an attempt to stop it.

I don't really care why or what got me into the state. When things have to be merged, I just want to stop. I do not want it to occur because it pollutes the check-ins, pollutes the history and sometimes adds stuff that's not ready.

However, I see that Git still performs the merge. Confer: Merge branch 'master' of https://github.com/weidai11/cryptopp.

Once I CTRL + C, the merge should not proceed. How to make Git honor a cancelled merge that was not asked for?

jww
  • 97,681
  • 90
  • 411
  • 885
  • What triggers this "unwanted merge" is unclear, but be aware that an empty commit message will cancel the commit. Simply empty your editor's buffer when prompted to enter a commit message, save and quit your editor. – jub0bs Jul 09 '15 at 09:04
  • How do I cancel it without deleting all the lines in the message? That's just extra work. Is there a way to make Git honor CTRL+C like nearly every other piece of software on the planet? – jww Jul 09 '15 at 09:21
  • 1
    I think you're asking the wrong question, here. You should instead find out what is causing this "unwanted merge". Unless you have some evil script running in the background, Git only does what you ask it to do. – jub0bs Jul 09 '15 at 09:26
  • Perhaps, but I'm not a Git expert, and I don't really plan on becoming one. For the non-experts who just want to check-out and check-in, its safest and easiest to cancel the merge. – jww Jul 09 '15 at 09:34
  • 1
    This has nothing to do with being a git expert. Git simply doesn't do these things on its own. Now this is assuming, that you're using a command line editor and not firing up a gui one: Ctrl+c also wouldn't work that way. The current process running in your terminal (which the signal is sent to) would be your editor and not git. What command are you running before this happens? – Tim Bodeit Jul 09 '15 at 10:16
  • Tim and Jubobs - I think what you are doing is called [bike shedding](http://bikeshed.com/). You're telling me what I should be doing in your world. In my world, I want to ***stop*** immediately when something unexpected happens. I don't want to learn why it happened, or how to respond to it. I just don't care why. Once I stop, I can correct it by deleting the current repo and performing a new clone. Its really that easy for me. Anything else is just extra work for me. – jww Jul 09 '15 at 10:23
  • 1
    I interpreted the last line in your question as "What should I do, so that this does not happen again". I'm sorry if I misunderstood that question or offended you in any way. I was merely trying to help the best way I can. I mentioned, why I believe that using ctrl+c wouldn't work in most cases from a technical standpoint. @Jubobs offered the (as far as I know) fastet way to cancel any git commit (*stopping* immediately). Both of us made clear that we believe that git is not the source of your actual problem. And I would not call that bike shedding. – Tim Bodeit Jul 09 '15 at 10:41
  • 2
    @jww No "bike shedding" here at all. You write: *I don't want to learn why it happened, or how to respond to it. I just don't care why.* I don't want to sound sanctimonious, but that's just a terrible attitude. Instead of trying to avoid a consequence of a problem (i.e. the editor prompting you for a commit message when you don't want to create a commit), go to the source of the problem (i.e. what is causing the creation of that "unwanted" commit) and eliminate it. Makes sense to me. – jub0bs Jul 09 '15 at 10:52
  • @Jubobs - *"but that's just a terrible attitude"* - OK, but it is what it is. My job is to produce stable, secure software. Wasting time with Git is just that - a waste of time. I've wasted so much time in the past on Git I refuse to waste anymore time with it. I'm not interested in learning the things you are interested in learning, like how to master Git. Telling me I should take an interest is the very definition of bike shedding. – jww Jul 09 '15 at 11:00
  • 1
    @jww I'm not going to argue with you; what you do is your business, not mine. I suggested one way of cancelling the creation of a commit (the only one I know of); take it or leave it. – jub0bs Jul 09 '15 at 11:08
  • 1
    Also at no point has either of us actually said you should take any further interest in git. We merely mentioned that you likely have another problem on your system, that is not caused by git itself. If you want to stress that you want `ctrl`+`c` to work, a [git bug report](https://git-scm.com/community) is the right medium for that in my opinion. Do note though, that if your editor is the current process it will be "intercepting" that signal. Where possible git actually allows you to use `ctrl`+`c` (e.g. aborting git fetch). – Tim Bodeit Jul 09 '15 at 11:15
  • Another reason to get to the bottom of this problem is that only "true" merges (i.e. non-fastforward ones) cause the editor to pop up, and give you the opportunity to cancel them. `ctrl` + `c` won't help you for cancelling "unwanted" [fast-forward merges](https://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging#Basic-Branching). – jub0bs Jul 09 '15 at 11:33
  • Tim and Jubobs - OK, thanks. I will kill the terminal process to put an end to these problems once and for all. Thanks for your help. (And I don't know the difference between a *"true"* merge and other types of merges. And I am not going to waste a second of my time trying to learn what it is. I refuse to grep man pages anymore for this program. I just want it to stop immediately :) – jww Jul 09 '15 at 11:39
  • @jww Do you use `git pull`, by any change? – jub0bs Jul 09 '15 at 11:58
  • @Jubobs I was wondering the exact same thing. – Tim Bodeit Jul 09 '15 at 12:12
  • (I meant "chance", not "change".) – jub0bs Jul 09 '15 at 12:13
  • @jww Have you checked if there is something in your shell or git config that might cause this? For details have a look at my updated answer. – Tim Bodeit Jul 09 '15 at 15:19

1 Answers1

3

git push should normally fail if there are any new commits on the remote, that you don't have on your local branch yet.

In order to push, you cannot be missing any commits that the server has.

In the example you posted, you seem to have made the commits d091b80 and 217cb1f on two different machines or two different local copies.

When you were trying to push 217cb1f, the server already had d091b80, while you didn't have it in your local branch.

Before git can push to the server, you will need to pull that other commit from the server.

Usually that is something, that you would do manually. For some reason it seems like your git push is somehow invoking a git pull.

While I couldn't find a specific option to enable this sort of behavior (and wouldn't suggest doing so either), I can see that some people would want git to behave this way.
If you are using another persons config files for git or for your shell, I would have a look if there is anything that might cause this weird behavior.

So how does the merge commit generally come about? Set aside the weird behavior of the push command and assume you had manually done a pull:

Git pull causes an unexpected merge commit

Git pull is doing two things:

  • It fetches the remote
  • It merges the remote branch into your local branch

If there you have no new commits on your local machine, then you have a linear history and changes from the server will be included by fast-forwarding. This means that your local branch will simply be set to the same revision as the remote branch (visualise just having to walk down a little further on a single road).

Imagine you have made a commit on your local branch, while a colleague (or yourself on a different machine) has pushed another change to the remote branch on the server:
The two branches have now wandered into separate ways (visualise one road splitting into two here). When git pull tries to merge them together it will need to make a merge commit (visualise two roads merging into one).

Alternatively you could do git pull --rebase or set the corresponding setting in your git config. This will tell git to apply your local changes to the "end of the road" of the server.
Note though, that a rebasing makes conflict resolution much harder and is generally more complicated than merges.


Pushing as early as possible minimises branch diversion. If you haven't made any new local commits yet, it is also a good idea to pull changes from the server before you are making a commit. That way, you are preventing your branches from diverting.

A good way to do this is:

git stash       # This saves your uncommitted changes away as a draft
git pull        # Gets new commits from the remote
git stash pop   # Restores your uncommitted changes

Abort a merge, while already editing the commit message

  1. Empty the commit message in your editors buffer, save and exit.
    Any comments (prefixed with #) can stay as they are removed by git before evaluating (though this behaviour can be changed in the config).
    If you are vim user, I believe dG:x to be the fastest way to do this.
  2. Run git merge --abort
    This will make git try to restore the pre-merge state of your working copy.
    This command is available in git 1.7.4 or higher. For more info have a look at the man entry for git-merge.

Why Ctrl+C does not do what one might expect

Simply pressing Ctrl+C will (in most, if not all cases) not do what you expect. When git launches a command line editor (such as vim, emacs or nano) that editor becomes the active/current process of that shell.

Press Ctrl+C will send a SIGINT signal to the current process. Meaning, sending SIGINT to your editor and not to git.

If you were to use an editor, that fails (quits and returns an exit code other than 0) when receiving that signal, that might (though I have not tested it) abort the commit.
As will an editor, that saves an empty buffer and quits.
If you configure git to launch a GUI editor it will remain the frontmost process and a SIGINT should still abort the commit.

Note, that in all three cases you will likely have the changes from the other branch in your working copy and you will need to clean them up (see step 2).


For me personally (and a lot of extensive git users), this behaviour is what I would want from git. If you see that differently, you could file a bug report or feature request here.

Community
  • 1
  • 1
Tim Bodeit
  • 9,673
  • 3
  • 27
  • 57
  • Thanks Tim. I'm going to accept the answer to put it to bed. The best I can tell, I cannot make Git stop all this extra, unwanted behavior. In the future, I'm just going to kill the process. – jww Jul 09 '15 at 13:41