88

I'm trying to switch branches in git but I'm getting this error message:

error: you need to resolve your current index first

I'm using git under xcode4

git status
# On branch DateCode
# Unmerged paths:
#   (use "git reset HEAD <file>..." to unstage)
#   (use "git add/rm <file>..." as appropriate to mark resolution)
#
#   both modified:      Whereami.xcodeproj/project.xcworkspace/xcuserdatauser.xcuserdatad/UserInterfaceState.xcuserstate
#
no changes added to commit (use "git add" and/or "git commit -a")
Frappuccinos-MacBook-Pro:whereami
lampShade
  • 4,361
  • 8
  • 34
  • 50

8 Answers8

102

Try this if you don't want any of the merges listed in git status:

git reset --merge

This resets the index and updates the files in the working tree that are different between <commit> and HEAD, but keeps those which are different between the index and working tree (i.e. which have changes which have not been added).

If a file that is different between <commit> and the index has unstaged changes -- reset is aborted.

More about this - https://www.techpurohit.in/list-some-useful-git-commands & Doc link - https://git-scm.com/docs/git-reset

jitendrapurohit
  • 9,435
  • 2
  • 28
  • 39
53

You end up with both modified in the output of git status if there were conflicts produced by a merge. git isn't letting you change branch until you've resolved these conflicts. If you edit that file, you should see some conflict markers in it - there's a guide to resolving those conflicts in the git manual. (Since kernel.org is currently down, you can find that guide here instead.)

Alternatively, if you think the merge was a mistake, you could undo it with: git reset --merge

Mark Longair
  • 446,582
  • 72
  • 411
  • 327
25

If you don't care about the changes that git says are outstanding, then you can do a force checkout.

git checkout -f {{insert your branch name here}}

PHP Bugs
  • 1,133
  • 12
  • 23
jyapx
  • 2,009
  • 2
  • 15
  • 18
  • 3
    This was exactly what I needed -- I had a test branch where a merge had failed with lots of conflicts, and I just wanted to throw away the branch. I wasn't able to get back to a good branch (checkout failed, and `git reset --merge` also failed). But `checkout -f` worked great. – Chad Nov 27 '17 at 22:37
6

I got this message when updating new files from remote and index got out of whack. Tried to fix the index, but resolving via Xcode 4.5, GitHub.app (103), and GitX.app (0.7.1) failed. So, I did this:

git commit -a -m "your commit message here"

which worked in bypassing the git index.

Two blog posts that helped me understand about Git and Xcode are:

  • Ray Wenderlich on Xcode 4.5 and
  • Oliver Steele from 2008
  • 6

    you can reset your branch with HEAD

    git reset --hard branch_name
    

    then fetch branches and delete branches which are not on remote from local,

    git fetch -p 
    
    kylieCatt
    • 10,672
    • 5
    • 43
    • 51
    Deepika Patel
    • 2,581
    • 2
    • 19
    • 13
    1

    Since the file is modified by both, Either you need to add it by

    git add Whereami.xcodeproj/project.xcworkspace/xcuserdatauser.xcuserdatad/UserInterfaceState.xcuserstate
    

    Or if you would like to ignore yoyr changes, then do

    git reset HEAD Whereami.xcodeproj/project.xcworkspace/xcuserdatauser.xcuserdatad/UserInterfaceState.xcuserstate
    

    After that just switch your branch.This should do the trick.

    Koshik Raj
    • 108
    • 6
    1

    You need to commit or destroy any unsaved changes before you switch branch.

    Git won't let you switch branch if it means unsaved changes would be removed.

    Andrea
    • 19,134
    • 4
    • 43
    • 65
    • The error and the `git status` output indicate that this isn't just changes in the working tree that would be lost by changing branch -- they indicate that a merge failed and the conflicts must be resolved in the index. – Mark Longair May 14 '11 at 15:49
    0

    Git says you have unresolved merge conflicts in your current branch. Ideally you should resolve the merge conflicts, and then commit the changes.

    BUT, IF you really don't care about the local changes in current branch (maybe you were trying out something locally, but need not persist them), you can ignore and force checkout the other branch

    git checkout -f <other-branch-name>
    

    IF you'd still want the first branch (with merge conflict) but just want the latest from remote, you can do this

    • Force delete that branch
    • Fresh checkout the branch
    # assuming you already switched to another branch
    
    git branch -D <branch-to-reset>
    git fetch
    git checkout <branch-to-reset>
    
    Arghya C
    • 9,805
    • 2
    • 47
    • 66