如何丢弃本地更改并从 GitHub 存储库中提取最新版本

我的机器上有一个目录,我在其中存储来自 GitHub 的所有项目。我打开了其中的一个,并在我的机器上进行了本地更改。项目搞砸了,现在我想放弃我所做的所有更改,并从存储库中提取最新版本。我对 GitHub 比较陌生,使用的是 GitShell。

git pull命令是否足够? 我是否需要做一些额外的操作来丢弃在本地进行的更改? 有人能帮忙吗?

210975 次浏览

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 .

Run the below commands

git log

From this you will get your last push commit hash key

git reset --hard <your commit hash key>

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.

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

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

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.

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 .

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.