Git revert and reset : Files in the repository are not getting rolled back - github

I want my entire repository to roll back to a certain previous commit (that is 20 commits behind my latest commit).
I tried using git reset --hard <commit#> and git revert <commit#>
Both the git commands run successfully with the message - Head is now at <commithead>
But then when I see the code in my repo, I can still see the edits I made in my latest commits. How can I remove all the edits after the particular commit I am reverting to?
Am I missing out on some step here?

After your git reset --hard, use to be sure the git restore command
cd /path/to/repository
git restore -- .
That should restore (as its name implies) the working tree with what is in HEAD (and you just reset HEAD to the right commit)

Related

How to recover lost changes locally

I have a project on GitHub. I made some changes to it and I wanted to apply git push but it showed some error. I googled for solution and someone said to do git stash git pull, so I did those two. In the end all my changes to the projects on my local machine are lost. It became the same project as on GitHub without all the changes.
Tried git fsck --lost-found but didn't work.
git stash saves you changes but resets the state to the last commit. To restore the saved (not commited) changes, simply run git stash apply. (Learn more in the docs.)

get previous commit that got deleted on git repository

Yesterday when I was at work, I was working on my project that i have on my home computer. The problem was when I tried cloning the repo it eclipse would crash. So instead I downloaded it manually and initialized the repo and then force pushed to my home computer. This deleted all the history and files in on the repo. I did some googling and tried reverting the commit but it the force push erased the commit history so I can only go back to the force push commit.
I have a computer at home which hasn't pulled the updates since this mistake and I was able to get its commit SHA. I used that on the web and found the last version its still there but I can't pull that commit because it got "deleted" and won't show in the repos history. Is there a way I can use the SHA from the last good commit and make that the master branch restoring my history?
When changing the HEAD (the working position in the git repository), it tracks your history in the reflog. Executing git reflog in the command line will show your complete history. You can read more about it on git-scm.com
When you have the desired commit SHA, you can do a git checkout with it. Executing git checkout [SHA1] will take you to the desired position in the git history. From there, you can make a new branch from it by running git checkout -b "new_branchname".
If you want to set your current branch to the commit, you can run git reset --hard [SHA1]. This would reset the current branch, but the previous state can however be found in the reflog.
I do NOT recommend doing this as general practice BUT...You should be able to force push from the other computer, thus resetting the repo to its former state. Providing you don't want to keep anything you did while making a giant mess of your git repo yesterday.
Since you know the right version you want to recover, it will make things easier.
In your local repo, use below commands:
git reset --hard <commit sha value>
git push -f
Note: if you execute the commands in the local not initialized (not cloned from remote), you should fetch first by git fetch --all, and then execute the two commands .
Now the version will go back to the latest as you need both locally and remotely.

Revert git reset and push

I've executed the commands
git reset --hard 6eb7d13f32cf110a1122206e58df381c000a987e
git push --force
on my github repository. The problem is that the full SHA I used is incorrect. Now, my repository state is so old. So, I'm trying to revert the repository to the last commit I pushed before the execution of these two commands.
The output of a git reflogis
6eb7d13 HEAD#{0}: reset: moving to 6eb7d13f32cf110a1122206e58df381c000a987e
4d0a0fd HEAD#{1}: pull: Fast-forward
82859b3 HEAD#{2}: commit: including command to install dependencies
e3c3ab8 HEAD#{3}: clone: from https://github.com/<user>/<repository>.git
The first output line points out the mistake I'd like to undo.
Is it possible to revert it? Thanks.
To get back to the state you were in just before the bad reset and push, reset and push again. The commit should still be there, unreferenced commits typically hang around for weeks. Looks like that's 4d0a0fd.
git reset --hard 4d0a0fd
Check everything is ok before pushing!
git push --force
This is why one should always hesitate before pushing, especially a force push. It gives you a moment to check everything is ok before sharing a mistake.

committed without git pull, not pushed due to conflict

I am using netbeans to pull/commit/push to git. I have accidentally make commit before pull request and now it's asking me Rebase/Merge. Either of option gives me error. I tried with following links using Windows git shell
How to undo last commit(s) in Git?
How to revert Git repository to a previous commit?
How to undo last commit
Remove a git commit which has not pushed
I have tried following commands:
git checkout <commit sha key>
git reset --hard <commit sha key>
NOTE: I have commited change but not pushed!
try git reset --soft HEAD~1
when you haven't pushed yet, a soft reset will take all the changes from the latest commit, and place them back onto stage again. in other words, the status of your repo will look exactly as it was the moment before you commited.

Git reset hard and going back

From git gui, I used gitk to git reset -hard to a few commits before my current one, as I needed to test if everything was working before the changes.
Since I even had a few uncommitted changes, I git stash in order to save them and being able to reapply them once going back to my last commit.
The problem is that gitk is not showing the top of my commit tree any more (the top commit is the current one and I don't see any commit above it)
It was sometimes since last time I used git, but I thought I can use git reset -hard to bring the current code to a previous version, and then git reset -hard to the old version.
How can I retrieve all the commit between the old HEAD and the revision I git reset -hard to?
Please tell me there is some kind of way.
I'm using Eclipse as development tool (in case I'll need to use it's cache)
What you did could work if you had first make a new branch, before the first git reset --hard.
Because git reset moved your current branch back, and those commits are no longer referenced by any branch (and not visible anymore)
You need to fallback to the command line, and try a:
git reset --hard ORIG_HEAD
# or
git reset --hard HEAD#{1}
ORIG_HEAD or HEAD#{1} should have the SHA1 you were before the first reset.
If not, git reflog can help (that is what HEAD#{1} should be listed in).
Not, as alluding to in "ORIG_HEAD and FETCH_HEAD from history view in Eclipse", you should be able to see ORIG_HEAD in the "History view" of Eclipse.