Question

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!
Was it helpful?

Solution

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.

OTHER TIPS

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top