Git - Can't change branch
I'm a git newbie, and just started using it the other day. The idea seems good, but I've already run into a problem that makes git seem like much more of a hassle than it's worth.
What I'm doing:开发者_运维技巧
I started by adding it to one of my projects, and I created 3 branches:
- Master
- Stable
- Alpha
All seemed fine. As of today, Master and Stable were pretty much the same code. I was working on Alpha, which contained the beginnings of some new features.
What went wrong:
I wanted to finish bug testing the stable branch, so I can release an update to an iOS app. I changed to the master branch accidentally (this is the branch that I plan to use for released versions) and then wanted to change to the stable branch. However, on attempting to change I receive the error:
"you need to resolve your current index first"
And the branch refuses to change. Apparently, it's something to do with a bad merge... although I can't even remember merging anything, except for maybe a few lines of code when I was setting up git. Here's the output of 'git status':
# On branch master
# Unmerged paths:
# (use "git reset HEAD <file>..." to unstage)
# (use "git add/rm <file>..." as appropriate to mark resolution)
#
# both modified: Schedule.xcodeproj/project.xcworkspace/xcuserdata/Jordan.xcuserdatad/UserInterfaceState.xcuserstate
#
I'm using Xcode 4, and using the built in repository manager to switch branches/manage git etc.
So, what went wrong? Why is git giving me errors, even though I never really merged much (or anything?) in the first place - and how can I fix it, and avoid it happening in the future?
Fixed with:
git reset HEAD <file name of the file stopping merge>
Stopped from happening again by adding ignores and attributes to some of the .xcodeproj package files:
.gitignore
# xcode noise
build/*
*.pbxuser
*.mode1v3
*.xcuserstate
# old skool
.svn
# osx noise
.DS_Store
profile
.gitattributes
*.pbxproj -crlf -diff -merge
The simple approach is to run git stash
first, if you're not concerned with Schedule.xcodeproj/project.xcworkspace/xcuserdata/Jordan.xcuserdatad/UserInterfaceState.xcuserstate, which I probably wouldn't be.
As Stephen (below) points out, that's not an ideal approach, however. Another approach (which probably won't be liked by some) is to delete the file from the repository and commit that change:
git rm -f --cached Schedule.xcodeproj/project.xcworkspace/xcuserdata/Jordan.xcuserdatad/UserInterfaceState.xcuser
git commit
The --cached
flag keeps it from getting deleted off your hard drive, and the -f
flag is required because it's been "modified" (even though you think it hasn't).
To keep similar problems happening in the future, I'd add this file, as well as all other user specific files, to the .gitignore file.
It is because you have modified files locally. At this stage, you have to commit these files or you have to reset them. If you want don't want to commit them then :
$ git reset HEAD file path
After resetting the files you can simply checkout them as :
$ git checkout file path
CAUTION: By doing this your local changes will be lost.
精彩评论