Throw away local commits in Git

git delete local branch
git rebase
git revert commit
git reset
git remove specific commit
git commit
git delete commit from remote
git undo last commit keep changes

Due to some bad cherry-picking, my local Git repository is currently five commits ahead of the origin, and not in a good state. I want to get rid of all these commits and start over again.

Obviously, deleting my working directory and re-cloning would do it, but downloading everything from GitHub again seems like overkill, and not a good use of my time.

Maybe git revert is what I need, but I don't want to end up 10 commits ahead of the origin (or even six), even if it does get the code itself back to the right state. I just want to pretend the last half-hour never happened.

Is there a simple command that will do this? It seems like an obvious use case, but I'm not finding any examples of it.


Note that this question is specifically about commits, not about:

  • untracked files
  • unstaged changes
  • staged, but uncommitted changes

If your excess commits are only visible to you, you can just do git reset --hard origin/<branch_name> to move back to where the origin is. This will reset the state of the repository to the previous commit, and it will discard all local changes.

Doing a git revert makes new commits to remove old commits in a way that keeps everyone's history sane.

Throw away local commits in Git, If you can see only the excess commits you can just do. git reset --hard origin/<​branch_name>. To move back to the origin. This will reset the  git: Your branch and 'origin/master' have diverged - how to throw away local commits? I ran the tips and answers to the question above, but nothing decided. I am following with a progress chart using the sofwae source tree. I close the software and run the commands in the bash git. But there is the same mistake quoted.

Simply delete your local master branch and recreate it like so:

git branch -D master
git checkout origin/master -b master

On undoing, fixing, or removing commits in git, Commit them on the local branch. git commit -am "My descriptive message" This is normally a good thing and it will eventually go away by itself, but if for some  Throwing away local changes in Git workspace, allowing 'git pull' to proceed. Git is a powerful source code revision control system, letting teams large and small track changes between team members. It is widely used and very flexible. However, it's easy to get into an inscrutable state where it's not clear how to proceed.

Try:

git reset --hard <the sha1 hash>

to reset your head to wherever you want to be. Use gitk to see which commit you want to be at. You can do reset within gitk as well.

`git` discard all local changes/commits and pull from upstream · GitHub, I always have the same modified files after fresh cloning and can't discard them. Even git stash && git stash drop don't work! git checkout -- . does also not work! If you can see only the excess commits you can just do . git reset --hard origin/<branch_name> To move back to the origin. This will reset the state of the repository to the previous commit and discards all changes. If you want to make new commits to remove old commits in a way that keeps everyone's history will be the same. this can be done using

Delete the most recent commit:

git reset --hard HEAD~1

Delete the most recent commit, without destroying the work you've done:

git reset --soft HEAD~1

git-reset Documentation, If a file that is different between <commit> and HEAD has local changes, reset is aborted. This commit will get blown away so a throw-away log message is OK. specific - Throw away local commits in git git undo local commit (7) git reset --hard @ {u} * deletes all your local changes on the current branch, including commits. I'm surprised no one has posted this yet considering you won't have to look up what commit to revert to or play with branches.

If you are using Atlassian SourceTree app, you could use the reset option in the context menu.

Resetting, Checking Out & Reverting, Checkout and reset are generally used for making local or private 'undos'. git reset, Commit-level, Discard commits in a private branch or throw away  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.

How To Undo Last Git Commit – devconnected, git reset --soft HEAD~1 $ git status On branch master Your branch is ahead of '​origin/master' by 1 commit. (use "git push" to publish your local  If all you want to do is undo the act of committing, leaving everything else intact, use: git reset --soft HEAD^. If you want to undo the act of committing and everything you'd staged, but leave the work tree (your files intact): git reset HEAD^. And if you actually want to completely undo it, throwing away all uncommitted changes, resetting everything to the previous commit (as the original question asked):

How to Discard Unstaged Changes in Git - Guide, We can get the specific commit id's by running: git log Basically, it can be used to throw away local changes. When you run the following  If you have staged the file, then use git reset. Reset changes the index to match a commit. git reset -- foo.txt. I suspect that using git stash is a popular choice since it's a little less dangerous. You can always go back to it if you accidently blow too much away when using git reset. Reset is recursive by default.

Git :: Remove all local commits, Remove all commits not pushed git checkout master // Make sure you are on correct branch (If not doesn't work) git reset --hard origin/master // Reset local  If you want to throw away all uncommitted changes in your working directory, you should see git-reset, particularly the --hard option. If you want to extract specific files as they were in another commit, you should see git-restore, specifically the --source option.

Comments
  • possible duplicate of Various ways to remove local Git changes
  • git reset --hard <commit hash, branch, or tag> if you want to go to a specific reference other than a remote branch.
  • Just to be clear, if you're not working on master but on another branch, you should run git reset --hard origin/<your-branch-name>
  • This will not only discard local commits, but also throw away everything in your work tree (ie. you local files). If all you want to do is uncommit, but leave your work intact, you should do "git reset HEAD^" ... per stackoverflow.com/questions/2845731/…
  • You may want to do a Fetch after this. This fixed the number-of-commits-waiting-to-be-pushed counter on SourceTree.
  • git reset --hard origin/<branch_name> It will reset the project configuration as well, so take care of this. I have a big .cfg file which was reset to default. I had to spend hours on that again.
  • This works well when backtracking your changes would cost too much time, which happened to me after a couple of rebases.
  • Useful for subtree pull/push problems among team members!
  • This is perfect when you want to restore a branch instead of just master.
  • this is not a good way to delete single local commit. Better to use git reset --hard origin/<branch_name>
  • Maybe that solution will work, but that doesn't mean it's the proper one.
  • Upvoted this b/c it's useful information, but Ben Jackson's answer gets the checkmark for exactly solving what I wanted -- in a way that didn't require me to look up commit hashes. :)