How to discard the changes I don't commit?

I don’t know what happened but I messed up the Git repo again. So all was OK , it was compiling fine. Not compiling now. I had made some changes, not committed any. When I do repo diff I see the changes I made and some weird changes have come into the code. I don’t know! Lot of them. So now I think inorder to fix this and keep the changes I deliberately made and the changes that appear from no where. I will do git add the changes I made and I want to discard all the changes that otherwise appear. How to do this ?

git add filepath

And how to discard what I don’t want to add and commit?

  • How do you remove a branch and all of its history locally and remotely?
  • What is the best practice using git and hudson with one repository including multiple projects
  • amending a single file in a past commit in git
  • Git: I forgot to do an initial commit of generated code; is this a job for rebase?
  • Pushing to Github impossible
  • Importing a Maven project into Eclipse from Git
  • cannot deploy to heroku because the key with fingerprint is not authorised
  • Where have my commits gone?
  • What happens when you rewrite history of a public branch?
  • Sync Github Repo to Dreamhost Site
  • which gitignore rule is ignoring my file
  • Git server under Windows using FreeSSHd
  • 2 Solutions collect form web for “How to discard the changes I don't commit?”

    You can do the following steps:

    1. Use git add to add the changes you want to keep.
    2. git commit those changes to the repository.
    3. git reset --hard HEAD to throw away the rest of the changes.

    Note that step 3 is destructive and you can’t use Git to recover those changes. Make a backup first if you’re unsure.

    If you are using the GitHub GUI (Windows or Mac) and are not worried about losing the local changes on your computer but just need to get the latest version from GitHub:

    Simply use “discard changes” before attempting to sync. This can be done either for a specific file:

    enter image description here

    or for all files:

    enter image description here

    It reverts all selected files to the last version committed to the repo.

    Git Baby is a git and github fan, let's start git clone.