How do I revert all files to a previous commit using EGit? - eclipse

I am using EGit 2.2.0.20-1212191850-r with GitHub. All of my local files are committed and pushed. There is only the master branch. I would like to permanently revert all of my files to a previous commit (not HEAD~1). How do I do it?
Here's what I have tried:
I opened the History pane, where I see all past commits.
I right-clicked on the earlier commit and selected Reset > Hard.
I see the old version. I try committing the old version but none of the changed files show up in the Commit Changes window, even if I explicitly add them to the index.
I also tried Checkout in step 2, with the same result.
Eclipse git checkout (aka, revert) is not relevant, since it is just about reverting to HEAD, not an earlier commit.
What I'd really like to do is create a new branch from the earlier commit, but I'll settle for reverting.

If you already pushed the commits, it is generally recommended to revert the commits. The reason for that is that revert will add new commits to the history instead of replacing the history, which makes it possible for other people to just pull instead of having to rebase on the replaced history.
So in the History view, select the newest commit you want to undo, open the context menu and select "Revert". Repeat for the parent commits. When you're done, push the result.
Also note that "git checkout" and "git revert" are different things, "git revert" creates a new commit that undos the changes of a previous commit. "git checkout" on the other hand changes the working directory or current branch to a commit. You may be confused because "svn revert" does what "git checkout" does, not "git revert".

Related

Eclipse Egit. Checkout creates new commit. Why?

As I understand GIT, when I checkout on commit, I should get its files copy in my work directory, but no new commit should appear. Though when I use EGit and checkout on commit I see new commit in reflog. Why? As I understand checkout should not create new commit. Right?
Here I right click on commit and choose checkout
Then I get new commit in reflog:
So now I have few commits in my local master, but I never asked to do them.
Git integration for Eclipse - Task focused interface 4.6.1.201703071140-r org.eclipse.egit.mylyn.feature.group Eclipse EGit
You have to work with the History view instead of with the Git Reflog view (see git reflog and toniedzwiedz's answer for details):
Tell the History view which history should be shown: e. g. in Git Repositories view right-click a repository and choose: Show In > History.
In the History view enable the option Show All Branches and Tags (right button in the view toolbar). Otherwise, only commits of the current branch are displayed.
You don't see a new commit in the reflog. What you see is an updated position of HEAD. You changed it to commit 4b0d96a when you checked it out. When you check out another commit, branch or a tag, you'll see yet another entry appear in the reflog.
Try switching between two branches repeatedly and you'll see the same two commit hashes appended to the reflog again and again. This does not mean you're creating new commits. You just see existing commits being logged as recent commits pointed to by the HEAD pointer.
From the git reflog docs:
This command manages the information recorded in the reflogs.
The "show" subcommand (which is also the default, in the absence of any subcommands) shows the log of the reference provided in the command-line (or HEAD, by default). The reflog covers all recent actions, and in addition the HEAD reflog records branch switching. git reflog show is an alias for git log -g --abbrev-commit --pretty=oneline; see git-log for more information.
You may also find this chapter of the Pro Git book interesting. It offers a more comprehensible description of what git reflog does.

Create a hg bookmark after committing a changeset

I cloned a Mercurial repo and made some changes in the checked out code. I then grabbed those changes (7 files) and committed them with hg commit but without having created a bookmark first.
This is the output of my hg summary command:
parent: 8172:b39efc1322fe tip
Made some changes in my feature
branch: default
commit: 7 modified
update: 2 new changesets, 3 branch heads (merge)
phases: 3 draft
These changes won't be ready to be pushed for at least another week, so I want to switch to a new bookmark and work on other parts of the code.
The problem is that I realized that I had made a commit without being in a bookmark, so I'm afraid that after switching to a new bookmark my committed changes could be lost.
Is there an easy way to move these committed changes into a new bookmark?
There is no need to be afraid at all: mercurial won't forget anything you committed unless you explicitly do both: enable history editing extensions and willingly use them in a way that you actually remove one or more changesets.
Mercurial is not git and mercurial does never forget or garbage-collect a changeset when it is not currently checked out and has no name (bookmark) attached to it.
You can show you all your heads using hg heads. And you can always create new bookmarks, attached to an arbitrary changeset by hg bookmark --rev XXX MyBookmark where XXX is the changesetID to which you want to attach the bookmark.
I'm afraid that after switching to a new bookmark my committed changes could be lost.
How can you imagine this? OMFG, you committed your changes to repository (your clone), you have this changeset (8172:b39efc1322fe) in it. When|if you'll svn up to another node, you'll just change parent of your Working Directory, on pull you'll get "2 new changeset" and, as result "3 branch heads" - 2 new changesets + your commit 8172, and this b39efc1322fe will be always one of heads, even you'll up and work in another.
If you are afraid to forget it (how??? really), you can post-fact bookmark this (committed) changeset with hg bookmark -r b39efc1322fe $WIP && hg commit -m "Bookmarked my work" in order to hg up $WIP when it will be time to continue this chunk of changes

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.

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.

How to delete commits with egit?

I just made some bad commits with egit that I would like to delete.
How do I delete commits from egit?
Thanks!
EDIT: I tried a hard reset a few times but it didn't do anything.
EDIT 2: Hard reset does rollback changes indeed, but I want them to completely disappear from the history as if I never made these commits.
RightMouse on your Repository and click on "show in -> history".
You should select the last commit before your last "fetch"...most of the time its the second commit under your current HEAD.
RightMouse on that commit and "reset -> Hard" (will reset all your commits AND local workspace changes to the selected commit).
you should see the up-arrow changing into an down-arrow, meaning that your commits are deleted and that your repository is outdated. Use "fetch" & "rebase" to be up to date.
Note that Egit3.0 in Kepler allows you to hard reset to any treeish expression you want:
But once hard reset, you still need to git push --force after that: if you don't the history of your upstream repo would still list that commit.
You can do a hard reset but be carefull with that !! Here's some more info: Delete commits from a branch in Git