111

I have a directory on my machine where I store all projects from GitHub. I opened one of them and made changes locally on my machine. Project got messed up and now I want to discard all the changes I made and pull the latest version from the repository. I am relatively new to GitHub, using Git Shell.

Would git pull command be sufficient? Do I need to do anything extra to discard the changes made locally? Can anyone help?

Coding Duchess
  • 6,445
  • 20
  • 113
  • 209

8 Answers8

151

git reset is what you want, but I'm going to add a couple extra things you might find useful that the other answers didn't mention.

git reset --hard HEAD resets your changes back to the last commit that your local repo has tracked. If you made a commit, did not push it to GitHub, and want to throw that away too, see @absiddiqueLive's answer.

git clean -df will discard any new files or directories that you may have added, in case you want to throw those away. If you haven't added any, you don't have to run this.

git pull (or if you are using git shell with the GitHub client) git sync will get the new changes from GitHub.

Edit from way in the future: I updated my git shell the other week and noticed that the git sync command is no longer defined by default. For the record, typing git sync was equivalent to git pull && git push in bash. I find it still helpful so it is in my bashrc.

Cody
  • 2,643
  • 2
  • 10
  • 24
  • None of this works for me, I get: Please move or remove them before you can merge – Stepan Yakovenko Dec 07 '18 at 11:29
  • @StepanYakovenko sounds like your repository is in the middle of attempting a merge, which should be indicated if you type `git status`. Check out this question: https://stackoverflow.com/questions/36039687/git-pull-please-move-or-remove-them-before-you-can-merge – Cody Dec 12 '18 at 18:36
  • 1
    You need to do `git clean -fd` if there are directories to be blown away as well. – Josiah Yoder Dec 13 '18 at 21:02
44

Run the below commands

git log

From this you will get your last push commit hash key

git reset --hard <your commit hash key>
abSiddique
  • 11,647
  • 3
  • 23
  • 30
18

If you already committed the changes than you would have to revert changes.

If you didn't commit yet, just do a clean checkout git checkout .

General_Twyckenham
  • 2,161
  • 2
  • 21
  • 36
  • 9
    Be careful with revert. It's often better to simply reset. Revert creates a new commit which removes or adds back what was in a previous commit. These removals and "adding back", however, can cause problems when you later attempt a merge with another branch, since they count as changes. For example, if you accidentally make a change in branch A, revert the change then make the same change in branch B, the push branch B followed by branch A, branch A may end up removing the changes you made in branch B. – JDB Aug 05 '16 at 02:02
  • @JDB Ahh yes thanks - I use reset myself in the rare cases I've needed to, just confused it with revert when I wrote the answer – General_Twyckenham Aug 05 '16 at 14:24
6

In addition to the above answers, there is always the scorched earth method.

rm -R <folder>

in Windows shell the command is:

rd /s <folder>

Then you can just checkout the project again:

git clone -v <repository URL> 

This will definitely remove any local changes and pull the latest from the remote repository. Be careful with rm -R as it will delete your good data if you put the wrong path. For instance, definitely do not do:

rm -R /

edit: To fix spelling and add emphasis.

ScottM
  • 620
  • 7
  • 20
  • 2
    Frankly I'm embarrassed how often I need to fall back on this – Reticulated Spline Jun 02 '22 at 22:50
  • 1
    This actually feels more peaceful and convincing than the other methods mentioned. I understand that for a repo with 100s or 1000s of files it can be a bit painful, but it's not a daily task, hopefully. – Sam Sirry Dec 22 '22 at 11:57
5

I messed up my git local master branch where local master was diverted from remote local.

To make git local and master in sync, followed below step.

  • git fetch --all

  • git reset --hard origin/master

If you are on any other branch you can use the branch name like below

  • git reset --hard origin/<branch_name>

How it works?

  1. git fetch downloads the latest from remote without trying to merge or rebase anything.

  2. git reset resets the master branch to what you just fetched

  3. --hard option changes all the files in your working tree to match the files in origin/master

In Case you want to keep your local changes before syncing with remote branch.

Create a backup branch before reset

  • git checkout master (or local branch name)
  • git branch new-branch-name
  • Follow above step to reset head to remote branch head.
  • After running the command in the answer, `git pull` still brings in files from the remote. How do I run this reset for ALL branches on the remote? – Dan Dascalescu May 17 '23 at 18:24
3

Just adding to what other people said I would do the following. If you already staged changes, meaning you already did "git add" at some point, I would do:

git reset HEAD <file_name>

If you haven't stated files for changes, as someone mentioned before just do:

git checkout . 
arturfil
  • 380
  • 5
  • 13
0

I usually do this with:

git fetch "${remote-origin}" &&
git reset --hard @{u}

The fetch gets the latest from the remote (you can probably just do git fetch or git fetch origin), while the reset sets the local branch to reference the same commit as the upstream branch. @{u} is a convenient shortcut for "upstream".

William Pursell
  • 204,365
  • 48
  • 270
  • 300
-10

To push over old repo. git push -u origin master --force

I think the --force would work for a pull as well.

Ronnie Royston
  • 16,778
  • 6
  • 77
  • 91
  • 3
    This is exactly the opposite of what the OP wants to do. – JDB Aug 05 '16 at 02:00
  • "I think the --force would work for a pull as well" ...would that not work? – Ronnie Royston Aug 05 '16 at 02:46
  • 1
    [`git pull`](https://git-scm.com/docs/git-pull) is shorthand for `git fetch` followed by `git merge FETCH_HEAD`. `fetch` has a `--force` option, but it doesn't really have anything to do with overwriting changes in the current branch. So while technically `git pull --force` is legal, it doesn't do what you think it does. – JDB Aug 05 '16 at 02:54