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 st
If you get your local repo into a complete mess, then a reliable way to throw away local commits in Git is to...
In my experience Eclipse handles the world changing around it quite well. However, you may need to select affected projects in Eclipse and clean them to force Eclipse to rebuild them. I guess other IDEs may need a forced rebuild too.
A side benefit of the above procedure is that you will find out if your project relies on local files that were not put into git. If you find you are missing files then you can copy them in from "my_broken_local_repo" and add them to git. Once you have confidence that your new local repo has everything you need then you can delete "my_broken_local_repo".
git reset --hard <SHA-Code>
This will come in handy if you have made some mistakes on your local copy that you want to make sure doesn't get pushed to your remote branch by mistake.
The SHA-Code can be obtained by looking at webVersion of your git dashboard for the last commit on the branch.
This way you can get synchronized with the last commit on the branch.
You can do git pull
after you have successfully completed the hard reset to confirm nothing new to syn i.e. you get to see the message.
Your branch is up to date with Origin/<Branch Name>
If you are using Atlassian SourceTree app, you could use the reset option in the context menu.
Find the sha1 for the commit you want to revert to:
za$ git reflog
... snip ...
cf42fa2... HEAD@{0}: commit: fixed misc bugs
~
~
cf42fa2... HEAD@{84}: commit: fixed params for .....
73b9363... HEAD@{85}: commit: Don't symlink to themes on deployment.
547cc1b... HEAD@{86}: commit: Deploy to effectif.com web server.
1dc3298... HEAD@{87}: commit: Updated the theme.
18c3f51... HEAD@{88}: commit: Verify with Google webmaster tools.
26fbb9c... HEAD@{89}: checkout: moving to effectif
And then use --mixed flag so that you "reset HEAD and index":
za$ git reset --mixed cf42fa2
Available flags:
za$ git reset -h
-q, --quiet be quiet, only report errors
--mixed reset HEAD and index
--soft reset only HEAD
--hard reset HEAD, index and working tree
--merge reset HEAD, index and working tree
--keep reset HEAD but keep local changes
--recurse-submodules[=<reset>]
control recursive updating of submodules
-p, --patch select hunks interactively
-N, --intent-to-add
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.
On your branch attempt:
git reset --hard origin/<branch_name>
Validate the reversal (to the state, with no local commits), using "git log
" or "git status
" hence.