user1100149
user1100149

Reputation: 266

Git - Why are files merging upon checkout?

This is driving me bonkers. I have read several tutorials, even watched videos on YouTube, but it seems I am missing some major fundamentals!

So, very simple workflow. I initialise Git on the directory git init, add the files git add ., and make my initial commit git commit -m 'initial commit'.

Then I create a new branch, ready to work on some new changes:

git branch myChanges
git checkout myChanges
git add .

So, I make my changes. But alas, I don't like them. I want to revert back to where I was before I made them:

git checkout master

But [arggghhhh!] my changes have been copied across! I have contaminated several months of good work with some changes that I don't want! [arggghhhh!]

The reason:

M www/index.php
Switched to branch master

I understand that the M shows that the index.php file has been merged into branch master of the repository?

Now part of my problem is that I have dipped into Git sporadically. And through this I have possibly done things in slightly different ways each time. I must have done, because I have had different results each time! Sometimes I have been able to checkout without merging any files. Sometimes I haven't. I thought I had the problem solved until I started using it again today. And this inconsistency has destroyed my confidence in using it. So I would appreciate it greatly if someone could explain (preferably in simple terms) when Git will, and will not merge files on checkout. So that I can switch between branches with confidence.

And if anyone can tell me the best way to undo the merge that happened when I last checked out to the master branch, that would be a huge help!

Upvotes: 2

Views: 66

Answers (2)

Ben Craig
Ben Craig

Reputation: 318

If you know what your last good commit code in the log was (use git log to find out), you can git reset --hard (insert code here) to revert to that.

I recommend reading this page: http://git-scm.com/docs/git-reset as well as the whole book if you need more git knowledge.

In terms of your files merging, either commit or stash them, and then it wont overwrite when you checkout another branch

Upvotes: 1

Kriateive
Kriateive

Reputation: 108

index.php is modified. Commit it in the branch myChanges before you switch to branch master. This way, the change will stay in myChanges branch.

Upvotes: 3

Related Questions