1

I've not worked on a project quite some time and have just tried to pull the latest version.

I can't remember the last time I'd worked on the project but I'd obviously had changes made to the Master that I'd not pushed. As I've received some conflict warnings.

Is there anyway to ignore my previous commits and pull down the master to overwrite my files?

What do you recommend I do in this situation?

2 Answers 2

2

I'm assuming that you've resolved the conflicts now, so that the first commit in git log is a merge commit.

Firstly, it's a good idea save where your old master branch was, just in case you want to go back. You can do that with:

git branch old-master master^

Then, if you want to reset your master branch to the version of master that you've just fetched from the origin repository, you can do that with git reset --hard:

# Make sure that you're on the master branch:
git checkout master

# Make sure the remote-tracking branches from origin are up-to-date:
git fetch origin

# Check that there's no output from "git status":
git status

# Now reset master to where origin/master points to:
git reset --hard origin/master

Note that git reset --hard is a dangerous command - it will throw away any uncommitted changes, which is why I suggest making sure that the output of git status is clean before using it.

Sign up to request clarification or add additional context in comments.

2 Comments

That's great thanks - the 'git branch old-master master^ command is a new one to me. Very useful.
To add a little to that, the ^ suffix to master means to take the first parent of master. (You can also do master^^ for the grand parent through the first parents, and so on.) The first parent of a merge commit is always the one that was merged into, i.e. the commit that you were at before the merge. Thus master^ will be your master before the merge. You could also do this with master@{1}, which gives you the previous position of the master branch recorded in the reflog - they should be the same in this case, but some might suggest that the latter is more obvious.
0

I guess an easy way would be to backup your local master branch and recreate it... something like:

git fetch origin
git checkout master
git branch master -m old-master
git checkout -b master origin/master
git branch -D old-master (if you don't care about your commits there)

Hope it helps,

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.