Git reset hard and going back - eclipse

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.

Related

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

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)

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.

Does EGit "Revert Commit" permanently delete the original commit?

I'm still learning Git's workflow for doing things, and realized that I was accidentally in the wrong working branch when I committed some files. So using EGit, I listed the commit history and selected Revert Commit from the context menu for a couple of commits.
I have since realized that I wanted those commits, but cannot find the original commits anywhere. It seems as though the original commits were all but deleted. Even doing a full log listing on the entire git repo does not show the original commit or the revert commits.
From my understanding of the EGit docs, revert commit was just supposed to "undo" the changes by creating a new commit ontop of the old one, undoing what it had done. However, it would appear that it actually deleted my original commits.
Am I toast? Did I permanently lose those changes? I'm running Eclipse 3.7 with EGit 2.1.0.20120919.
git revert, as you said, doesn't delete the commits. It only creates a new commit undoing the changes of a certain commit passed to it. So, the original commit is still in the repository. Listing the commits with git log should show the original commit and the commit that undo what the original commit does.
With that, to recover the original content of the commit, you can revert the revert commit or change your repository status to the commit before the revert with git reset --hard hash where hash represents the hash of the commit before the commits that represents your git revert.
Your best bet is to leave eclipse and egit for a while and use the commandline.
There is a tool called git reflog that can show you much more than git log. I suggest you give that a try to see what you have done to the repository.

Git remote branch commit

I have forgot to create local branch and was working with remote. After making some changes, i by mistake using Eclipse Git plugin commit this branch in nowhere(obliviously there wasn't local branch). And after that i switched to master, and lost all changes.
So how i can restore my changes?
Use git reflog, you'll see previous commit's IDs (in order of commit-time, from most recent to older), and you will then be able to rebase on top of them (or use them like any other commit, that is).
The reflog is the last place you can recover commits that cannot be reached from the tip of any (local) branch, and commits will stay there in the limbo until they are eventually garbage collected, which theoretically could happen any time.