13

Trying to use/learn git with a personal project. There's only me and a remote git repo, a few commits, and I'm stuck in a failed merge. A lot of my files have Git merge conflict markup now too.

How do I tell git to just throw everything out, just use mine?

A specific example of how I got into the state I'm in:

echo A new file > myFile.txt             # example file
git add myFile.txt                       # add file
git commit                               # commit changes
git push                                 # push changes to remote repo
echo A conflicting edit > myFile.txt     # oh, no, forgot some changes
git add myFile.txt                       # add again
git commit --amend                       # amend previous commit
git push                                 # fails. Git suggests to do a pull first
git pull origin HEAD                     # "Automatic merge failed" Now what?
                                         # Just use what I have locally!
Kache
  • 11,723
  • 10
  • 47
  • 71
  • Yeah. Didn't find a simple "use my local everything" command. – Kache Sep 28 '12 at 02:19
  • Git merge conflicts are solved in this mega thread: http://stackoverflow.com/questions/161813/how-do-i-fix-merge-conflicts-in-git – Jessedc Sep 28 '12 at 02:19
  • 2
    Yeah, found that thread. It didn't help me. They talked about using tools I couldn't use (`git mergetool`), looking at verbose documentation, general advice about how to avoid merges, etc. When I Googled for "Git use local", I kept finding articles about how to use git on my computer without a remote origin. It's infuriating. Are you saying there isn't a one-line command that just tells git to "use my local"? – Kache Sep 28 '12 at 02:25
  • 1
    This is nowhere close to an "Exact Duplicate" of the "git merge megathread". This a specific merge scenario, and as such, better to have it here than lost in the noise of a mega thread with 10 responses and a 100 comments. – Mark E. Haase Oct 17 '12 at 15:02
  • @mehaase Thanks! This was exactly what I was hoping for – Kache Oct 18 '12 at 13:58

2 Answers2

17
git checkout --ours . # checkout our local version of all files
git add -u            # mark all conflicted files as merged/resolved
git commit            # commit the merge

There is a messy alternative that can break the repo for everyone else using the same remote origin. Only consider it if you're the only one using it:

git reset --hard HEAD # undo that failed merge
git push --force      # replace everything remote with local

Explanation (now that I understand git better)
The reason this happened is because amending commits changes 'history'. Doing this locally is safe because it doesn't affect anyone else. However, amending commits that have already been pushed does affect other repos, and is not safe.

Kache
  • 11,723
  • 10
  • 47
  • 71
  • `man git-merge` provides an alternative in the section "How to resolve conflicts". `git merge --abort` – ignis Nov 12 '12 at 21:44
6

Your GUI is probably just setting --strategy=ours (git merge -s ours <branch>). This will perform the merge, citing both commits as parents, but keep your entire directory state.

Your other option is to use git merge -s recursive -X ours <branch>, which will try to bring in files from both branches but will prefer your version whenever there is a conflict.

Docs

You can see the two different styles at work using the following demonstration shell script:

#!/bin/sh

mkdir gittest
cd gittest
git init

git checkout master
echo "Line one" > bar
git add bar
git commit -m "Original commit"

git checkout -b fork1
echo "Line one and something" > bar
echo "Line two" > bam
git add bar bam
git commit -m "Fork1 commit."

git checkout master
git checkout -b fork2
echo "Line one and other stuff" > bar
echo "Line three" > baz
git add bar baz
git commit -m "Fork2 commit."

git checkout fork1
if [ "$1" = "ours" ]; then
  # `ls gittest` => bam bar
  # `cat gittest/bar` => Line one and something
  git merge -s ours fork2
else
  # `ls gittest` => bam bar baz
  # `cat gittest/bar` => Line one and something
  git merge -X ours fork2
fi
Jeff Bowman
  • 74,544
  • 12
  • 183
  • 213
  • 2
    +1 for merge strategy. More good info on merge strategies http://stackoverflow.com/questions/366860/when-would-you-use-the-different-git-merge-strategies – Jessedc Sep 28 '12 at 02:46
  • 3
    Side note: Also try `git merge -s recursive -X ours` to accept auto-merged changes but always use your local copy if there are conflicts. – Jeff Bowman Sep 28 '12 at 17:39
  • Didn't work. I get: error: 'merge' is not possible because you have unmerged files. hint: Fix them up in the work tree, hint: and then use 'git add/rm ' as hint: appropriate to mark resolution and make a commit, hint: or use 'git commit -a'. fatal: Exiting because of an unresolved conflict. – Kache Oct 11 '12 at 14:05
  • @Kache, that looks like you're still in the middle of a merge, at which point you can't proceed without resolving all of your files. To cancel the merge, run `git reset --hard HEAD`, then try my steps. You can also try a demo script showing the two different "ours" strategies at http://pastebin.com/ScLzuHf7 . – Jeff Bowman Oct 11 '12 at 17:33
  • Resetting like that would just put me back where I was, i.e. my local repo thinks everything is fine and updated. Anyway, I found the answer I needed and edited it into my question. I wanted to formally submit an answer, but they closed the question on me. =\ – Kache Oct 11 '12 at 17:47
  • Ah, I understand how your method works now. It executes a merge from that `(working directory clean)` state. However, that means to do what I want, you'd have to `branch` or `stash` the current copy somewhere, so that you can cleanly `git merge` two clean branches. – Kache Oct 11 '12 at 18:34