How do I make Git honor a canceled merge that was not asked for?

I ran into this situation about 3 or 4 times. Sometimes Git wants to do a merge. This occurs in response to git commit <filename> -m <message>

followed by git push

.

I know a merge will happen because Git will launch the editor for the post. So I execute CTRL+ Cin an attempt to stop it.

I don't care why or what brought me to the state. When things need to be combined, I just want to stop. I don't want this to happen because it pollutes accounts, pollutes history, and sometimes adds things that aren't ready.

However, I can see that Git is still doing the merge. Confer: Merge the "master" branches https://github.com/weidai11/cryptopp .

Once I CTRL+ C, the merger should not continue. How do I make Git honor a canceled merge that was not asked for?

-4


source to share


1 answer


git push

usually doesn't work if there are any new commits on the remote that you don't already have on your local branch.

To push, you cannot miss any commits that are on the server.

In the example above, you seem to have made the commits d091b80

, and 217cb1f

on two separate machines, or two different local copies.

When you tried to push 217cb1f

, the server already had it d091b80

, and you didn't have it on your local branch.

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

This is usually what you do manually. For some reason, it seems that yours git push

is calling somehow git pull

.

While I couldn't find a specific way to enable this behavior (and would not suggest doing it), I see that some people will want git to behave this way.
If you are using other people's config files for git or for your shell, I would see if there is anything that might be causing this strange behavior.

So how does the merge work at all? Ditch the strange behavior of the push command, and assume you manually pulled:

Git pull causes an unexpected merge

Git pull does two things:

  • It retrieves the remote
  • It merges the remote branch into a local branch

If you don't have new commits on your local machine, you have a linear history and changes from the server will be triggered by a quick redirect. This means that your local branch will just be set to the same revision as the remote branch (render, just need to walk a little down the same road).

Imagine you made a commit at your local branch, and a colleague (or yourself on another machine) pushed another change to a remote branch on the server: The
two branches now wandered differently (rendered one road splitting in two). When git pull tries to merge them together, the merge commit needs to be done (render two roads merging into one).

Alternatively, you can make git pull --rebase

or set an appropriate option in the git config. This will tell git to apply the local changes to the "end of path" of the server.
Note, however, that reloading makes conflict resolution much more difficult and generally more difficult than merges.




Pushing as soon as possible will minimize branch rejection. If you haven't made any new local pull

commits yet, it's a good idea to also change from the server before committing. This way you prevent your branches from being rejected.

A good way to do it:

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 the merge and already edit the commit message

  • Clear commit message in the editor buffer, save and exit.
    Any comments (prefixed with #) can remain as they are removed with git before evaluation (although this behavior can be changed in configuration).
    If you are a vim user I find this dG:x

    is the fastest way to do this.
  • Run git merge --abort


    This will force git to try to restore the pre-merge state of your working copy.
    This command is available in git 1.7.4 or higher. Check out the man entry for git-merge for more information .



Why Ctrl + C Doesn't Do What You Expect

Simply pressing Ctrl+ Cwill (in most, if not all cases) not do what you expect. When git starts a command line editor (like vim, emacs, or nano), that editor becomes that shell's active / current process.

Press Ctrl+ Cto send the current signal SIGINT

. Meaning, submitting SIGINT

to your editor, not git.

If you must use an editor, it will fail (exits and return an exit code other than 0) on receiving this signal, which might (although I haven't tested it) abort the commit. Like an editor that saves an empty buffer and exits.
If you configure git to start a graphical editor, it will remain the front-most process and SIGINT

still have to abort the commit.

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




For me personally (and for many broad git users) this behavior is what I would like to get from git. If you see it differently, you can file a bug report or feature request here .

+3


source







All Articles