130

I am trying to remove the file from my working directory but after using the following command

git checkout file_Name.txt

I got the following error message

error: path 'first_Name.txt' is unmerged

What is that and how to resolve it?

Following is my git status

$ git status
On branch master
You are currently reverting commit f200bf5.
  (fix conflicts and run "git revert --continue")
  (use "git revert --abort" to cancel the revert operation)

Unmerged paths:
  (use "git reset HEAD <file>..." to unstage)
  (use "git add <file>..." to mark resolution)

        both modified:      first_file.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        explore_california/

no changes added to commit (use "git add" and/or "git commit -a")
dan1st
  • 12,568
  • 8
  • 34
  • 67
Naseer
  • 4,041
  • 9
  • 36
  • 72
  • That moved to the staging index but i am not able to clean the folder explore_california/ – Naseer Mar 13 '14 at 17:57
  • 1
    If you want to delete `explore_california` then just do a `rm -r explore_california` since it is not being tracked by git. – brokenfoot Mar 13 '14 at 17:59
  • What that did is asking me to remove more than 50 files individually and i m still pressing y? – Naseer Mar 13 '14 at 18:02
  • But It worked Finally.Thanx – Naseer Mar 13 '14 at 18:04
  • @brokenfoot add it as an answer. –  Mar 13 '14 at 19:06
  • @khan: See this link on why it asks for your permission to delete files: http://superuser.com/questions/345923/remove-file-without-asking – brokenfoot Mar 13 '14 at 19:17
  • 2
    Possible duplicate of [Git: can't undo local changes (error: path ... is unmerged)](http://stackoverflow.com/questions/3021161/git-cant-undo-local-changes-error-path-is-unmerged) – Stewart Apr 04 '17 at 10:09
  • The reason is somehow you moved this file to staging (like running `git add first_Name.txt`) it needs to be unstaged before checked out. So easily run `git reset first_Name.txt` then you run `git checkout first_Name.txt` Expect the changes are reverted. – Halil Kayer Oct 28 '21 at 08:25

9 Answers9

198

If you want to discard modifications you made to the file, you can do:

git reset first_Name.txt
git checkout first_Name.txt
cristianoms
  • 3,456
  • 3
  • 26
  • 28
  • 2
    Needs the double hyphen arg? git reset -- first_name.txt and git checkout -- first_name.txt – demented hedgehog Jun 03 '18 at 23:56
  • 3
    You only use the `--` to separate the tree you want to check out from the files you want to check out. To a more in depth explanation, take a loot here: https://stackoverflow.com/questions/13321458/meaning-of-git-checkout-double-dashes – cristianoms Sep 17 '18 at 20:12
  • 7
    I think it's safer to use the double-hyphen `git reset -- first_Name.txt` and `git checkout -- first_Name.txt` just in case the filename is the same as one of your branches/tags/commits. – joeytwiddle Dec 09 '19 at 07:08
36

To remove tracked files (first_file.txt) from git:

git rm first_file.txt

And to remove untracked files, use:

rm -r explore_california
brokenfoot
  • 11,083
  • 10
  • 59
  • 80
31

Following worked for me

git reset HEAD

I was getting the bellow error

git stash
src/config.php: needs merge
src/config.php: needs merge
src/config.php: unmerge(230a02b5bf1c6eab8adce2cec8d573822d21241d)
src/config.php: unmerged (f5cc88c0fda69bf72107bcc5c2860c3e5eb978fa)

Then i ran

git reset HEAD

it worked

Mahesh Hegde
  • 1,131
  • 10
  • 12
20

status tell you what to do.

Unmerged paths:
  (use "git reset HEAD <file>..." to unstage)
  (use "git add <file>..." to mark resolution)

you probably applied a stash or something else that cause a conflict.

either add, reset, or rm.

gcb
  • 13,901
  • 7
  • 67
  • 92
  • 3
    Exactly I applied a stash. Now the question is when to use `add`, when `reset` and when do I use `rm`? For example, I don't want to keep the stashed version but the version from upstream instead? – Pro Backup Aug 28 '18 at 15:16
4

I don't think execute

 git rm first_file.txt

is a good idea.

  1. when git notice your files is unmerged, you should ensure you had committed it.

  2. And then open the conflict file:

    cat first_file.txt

  3. fix the conflict

4.

git add file

git commit -m "fix conflict"

5. git push

it should works for you.

steven
  • 529
  • 6
  • 15
1

Step 1:

git stash -u

Step 2:

git stash drop stash@{1}

Step 3:

git checkout .
Liam_1998
  • 1,157
  • 3
  • 12
  • 27
0

In my case, I found that I need the -f option. Such as the following:

git rm -f first_file.txt

to get rid of the "needs merge" error.

alant
  • 82
  • 7
0

i resolved by doing below 2 easy steps :

step 1: git reset Head step 2: git add .

kallayya Hiremath
  • 3,560
  • 1
  • 13
  • 14
0

It works for me.

It will recover file from curreny HEAD

    git restore -S -W file_Name.txt