tags:

views:

44

answers:

2

Ok, so I had been working on something, and decided it was completely screwed -- after having committed some of it. So I tried the following sequence:

git reset --hard
git rebase origin
git fetch
git pull
git checkout

At which point I got the message

Your branch is ahead of 'origin/master' by 2 commits.

I want to get rid of my local commits, without having to wipe out my local directory and redownload everything. How can I accomplish that?

+6  A: 
$ git reset --hard origin/master

will remove all commits not in origin/master.

mipadi
I thought the syntax "origin/master", with a slash, referred to a local repo?
Daniel
mipadi: More correctly put, it will reset the current branch to point to the same commit as origin/master.
Christoffer Hammarström
It refers to a branch. `origin/master` is a branch that tracks the `master` branch of the `origin` remote repo.
mipadi
+1  A: 

As an aside, apart from the answer by mipadi (which should work by the way), you should know that doing:

git branch -D master
git checkout master

also does exactly what you want without having to redownload everything (your quote paraphrased). That is because your local repo contains a copy of the remote repo (and that copy is not the same as your local directory, it is not even the same as your checked out branch).

Wiping out a branch is perfectly safe and reconstructing that branch is very fast and involves no network traffic. Remember, git is primarily a local repo by design. Even remote branches have a copy on the local. There's only a bit of metadata that tells git that a specific local copy is actually a remote branch. In git, all files are on your hard disk all the time.

slebetman
But how does that tells apart commits made locally from commits made at origin? In fact, it tells me that `Cannot delete the branch 'master' which you are currently on.`
Daniel
1. Basically all commits are the same, regardless if they are made locally or at origin. What's important is that the histories are synchronized correctly. Your local commit will only exist at origin after you pushed them and by default git will refuse the push if the history at origin may end up in a state that doesn't make sense.
slebetman
2. Of course you cannot delete the branch that is currently checked out. To delete master, check out another branch first. Of if there is no other branch simply create a temporary one: `git checkout -b temp;git branch -D master;git checkout master;git branch -D temp`
slebetman
Also note about what I said about your local copy of the remote repo being different: git will not allow you to edit or even view files in your copy of the remote branch. It will only allow you to create another branch from the remote branch which you can then view and edit. By convention, this local branch have the same name as the remote branch. You are partially right in that `origin/master` is on your local machine. That's your local (full) copy of the remote branch. The real remote branch is `origin master`.
slebetman