1

this is probably a very easy question but I am completely new to Git so...

  1. I have done a pull on my master branch.
  2. Another person has done a lot of changes to the master branch.
  3. I wanted to discard all my changes so I deleted the project locally (just deleted from windows explorer) and then run a git pull myremote master.

I do now see why this does not work. It ended up in that only the changed files from the master branch were pulled. And if i now run git status I will see all the other files as deleted files. Not good!!

So, what I want to do is to get a clean copy or checkout (or what it is called) of the master branch as if it was the first time I pulled the project from the master branch. Reset the local Git repository or something?

How do I proceed?

user1140596
  • 121
  • 1
  • 1
  • 10

3 Answers3

1

Try doing this to disreguard your changes:

git checkout .

and/or:

git reset HEAD --hard

then pull from master

edit:

Also if you do a git status and see that files are deleted, you can do a git add -u to stage those deletions and commit them to confirm.

okcoker
  • 1,329
  • 9
  • 16
  • Thank you, that worked perfectly. I just did git checkout . and then pulled from master. Now I have all files checked out and the git status does not report any deleted files. It do say I am 181 commits ahead of origin/master though. Don´t understand what this means but hopefully nothing bad. – user1140596 Sep 13 '12 at 07:48
  • You have extra files unsynced with master that aren't pushed yet – okcoker Sep 13 '12 at 07:52
  • Ok, how do I get rid of those extra files? Does this mean that if I push now, a lot of changes will be done to the master branch? I do not want any of my previous changes to be commited. – user1140596 Sep 13 '12 at 07:59
  • Hmm, I guess this does not solve my problem completely after all since I now seem to have a lot of files in my commit that aren´t pushed yet. I tried git reset HEAD --hard as well but git status still reports 181 commits ahead of origin/master. I want the pull to result in a complete copy of what´s in the server as if it was the very first time I checked it out. – user1140596 Sep 13 '12 at 11:11
  • Take a look here and let me know if that helps http://stackoverflow.com/questions/2342618/git-your-branch-is-ahead-of-origin-master-by-3-commits :) – okcoker Sep 13 '12 at 17:04
1

To reset your local tree (the files you can actually edit) to the exact state of the master on your server (remote) you could do (assuming the remote name to your server is origin) :

git reset origin/master --hard 
git clean -fd

The first command will reset the HEAD of the currently checkedout local branch (first line of git status is : On branch $current_branch) to the state of the server's master (meaning git log for $current_branch and for origin/master will be the exact same). The second command will remove extraneous files potentially left over.

I would recommand to use

git checkout -b wip

before to create a branch named wip in case you want to preserve some local unpushed commits done directly on the local master. you can then either merge wip or cherry-pick some commits as needed.

The suggested command

git reset HEAD --hard

Will reset your local tree to the state recorded by the head commit of your local branch, but the history of the local branch will still potentially be different from the history of the remote branch. It will cleanup any modifications you did locally and won't loose any commits you haven't pushed yet.

Jean
  • 21,329
  • 5
  • 46
  • 64
  • Thanks Jean for your good explanation. Unfortunately I did not see your comment before I completely deleted my whole local directory where I had my git checkout and then created a brand new one, see my answer below. Therefore I can not verify if your answer would work for me. At least it made me brighter :-), one vote up. – user1140596 Sep 13 '12 at 12:51
  • you can easily experiment with this by recreating the state you were in : on master create a local dummy commit, then muck some files and try out the various commands :) – Jean Sep 13 '12 at 13:06
0

I ended up in creating a new folder, browsing to this folder and run 'git init' in this folder. Then I created a new remote in this folder pointing to my project and then I pulled the project with 'git pull myremote master'. Then I deleted my previous folder that caused my problem. This was very simple :-).

user1140596
  • 121
  • 1
  • 1
  • 10