Vorac
Vorac

Reputation: 9116

How to get remote branch, overwriting local commits?

I have made some commits in my local master branch. Now I want to throw them away and end up with state equal to origin/master.

I wouldn't like to git reset --hard, because those commits are a result of a fast-forward merge of another branch, so my and remote commits are intermixed.

Currently the only solution that I cant think of is

git fetch
git checkout origin/master
git branch -D master
git checkout -b master
git remote origin/master

There surely is a much faster way?

Upvotes: 1

Views: 86

Answers (2)

Chris Maes
Chris Maes

Reputation: 37832

git reset --hard origin/master

does exactly the same as

git checkout origin/master
git branch -D master
git checkout -b master

just keep your git fetch command to make sure you use the latest information about origin/master.

The last command you propose (git remote origin/master) is a non-existing command; and I don't see what you want to do. It isn't necessary anyway.

Upvotes: 0

poke
poke

Reputation: 388313

I wouldn't like to git reset --hard, because those commits are a result of a fast-forward merge of another branch, so my and remote commits are intermixed.

I don’t understand this argumentation.

git reset --hard origin/master will reset your currently checked-out branch so it points at exactly the same commit as origin/master; and then it will overwrite all files in your working directory to reflect that change.

So if you are currently on your local master branch, then this is the command that will reset your local branch and working directory to the state of the remote’s master branch, effectively throwing away whatever you did locally on that branch.

Upvotes: 5

Related Questions