How can I do the equivalent of git rebase -i HEAD~2 in eGit? - eclipse

I know how to rebase onto a specific branch or tag using eGit in Eclipse, but I haven't found a way to rebase onto a specific sha1 using eGit. I'd like to do this in order to squash commits before pushing to upstream (ie, doing git rebase -i HEAD~n where n is some number of commits ago where I want to rebase). Is this possible with eGit? If not, is there another Eclipse plugin I should be using that would provide this functionality?

EGit has rebase, but it does not yet have the interactive rebase you ask for.

I've been successful squashing commits using steps from this post (cut and pasted here):
If I want to squash the last m commits on the current branch then I
select in history the first commit which I don't want to squash
right-click and say "Team->Reset->Soft"
right-click and say "Commit". This commit will contain all the changes of the last m commits together

It is currently available in Eclipse Oxygen, it has it's own wizard for it.
Get the branch you want in the History view
Right-click the commit before the one you want to change
Find Rebase interactive and click it
The Rebase wizard will show up with the commits available to work upon
It works just like the console version, but on a GUI. Even if conflicts arise, it will help you through.

From the history view select the commit you want to rebase to, and create a branch called rebase-point or whatever. Voila, you can now rebase to that branch, and delete it later if you want. I generally prefer using a named branch for things like this anyway, because it minimizes the chance for mistakes on my part. Alternately, consider that origin/master is often the right rebase point if you're squashing commits for a push.

You can also rebase from the Eclipse History view.
Select the repository from the Git Repositories view
Right-click and select Show In History
Select the commit you want to rebase the currently checked out branch on top of
Right-click and select the Rebase on Top of item
Note: You may need to select Show All Branches and Tags first from the History view toolbar to display the commit you are looking for in the table.

Related

Git - Remove commit from history but lesve the code

I am using git on VS2019
I was wondering if there is a way to remove old commits but always keep the latest code. The reason is that when I work on a big change, I do a lot of commits after checking every small step but at the end it is annoying when I click on view history and need to search an old commit to compare the changes. I would like to remove specific commits but leave the changes. In that way I will see on history only the main milestones of the developing process. There is a way to do that??
It sounds like in these scenarios, you're developing a new feature, in which case you should be using a branch+pull request approach to development. During the pull request, you can do a squash merge. It's not all in the command line, but that might be for the best anyway.
If you're already working within a feature branch, then I like VonC's approach.
It seems that you want the git rebase feature.
If you know how many commits you want to squash together, you can use
git rebase -i HEAD~N
You can also do it in interactive mode:
git rebase --interactive HEAD~N
Be aware that if you merging commits you have already pushed, you may have trouble with your next push.
For more details, see: https://www.internalpointers.com/post/squash-commits-into-one-git
You can follow a workflow similar to "Git better with fixups " from Atul Sharma
For the commits which are incremental improvement for the same step, use git commit --fixup small step, which allows you to "do a lot of commits after checking every small step".
(You can see it in Git Extension in Visual Studio)
But at the end, you can cleanup all those small steps with git rebase --interactive --autosquash <First step commit> (assuming you have not pushed those commits yet)
That way, you end up with a cleaner history.

See summary of multiple commits in SourceTree

I have made multiple code commits in my local repository. How do I see the final summary of those code commits combined before pushing the code to remote repository?
Earlier, I used Tortoise GIT(not available in MAC) where I can select those contiguous commits in show log and then right click->combine to one commit to see all the summary of my code changes before pushing the code to remote repository.
How do I achieve something similar in SourceTree without involving squash or anything which might change my git commit history
If the local commits are continuous commits then you can command+Click the already existing commit and the latest local commit to get the final summary of all those commits between these 2 points.
On windows you can achieve that with both 'CTRL + click' and 'SHIFT + click' on the two commits you want to display all changes between.

How to show history of different branch in EGit for Eclipse?

I want to cherry pick some commits of a specific git branch into my currently checked out branch.
To do that I would like to filter the history view EGit offers to see only that one branch.
I know that there's a toggle button that allows me to switch between the display of the commits of the current branch and all branches (). This allows me to see the commits done in the other branch. Though with all the commits of the other branches shown, this gets confusing quickly.
Similarily, there is a button allowing me to select the input, for which the commits are shown (). Though that button only allow to filter the commits by workspace resources of the currently checked out branch.
There is also a button to pin the current history view (), but it doesn't seem to work when I switch between the two branches, the view still gets refreshed.
So, Is there an option, that allows me to restrict the display of commits to a specific branch? Or is there an option to pin the view, so I can keep seeing the commits of the one branch when I check out the other branch?
This worked for Eclipse Oxygen:
Go to: Git Repositories view -> your repository -> branchA -> right-click and choose Show in > History -> (pin this view)
Do the same with other branch, a new view should be opened
Because I couldn't find an option how to restrict the shown commits to a specific branch, I filed a bug in Eclipse's bug tracker.
The answer there was that the code for the input only distinguishes between repositories and files and it currently disregards branches. So it looks like it is not possible to restrict the display of the commits to a specified branch at the moment.

Going back in time on Github?

How do I go back to a previous build? For example going back to the July 30 state?
There are a number of ways to functionally undo a series of commits in Git. But given that the branch in question is published, and likely shared by several people, I would recommend using git revert here. git revert adds new commits on top of your current branch which undo previous commits. These revert commits can be thought of as mirror images of what was done in previous commits. Something like this should work:
git revert 00bfe1b^..a013402
This should add two commits, one to undo the Aug-9 commit and one to undo the Aug-13 commit.
As a side note, you could have also done this:
git reset --hard HEAD~2
This would have completely nuked the two commits on top of Jul-30. However, this involves rewriting the history of the branch, which isn't desirable if other people may already be sharing this branch with you. In this case, just play it safe and use git revert.
To view (browse the file tree) the repository state at a certain commit given at least the first four characters of commit's hash, the following URL schema for GitHub can be used (at the time of writing):
https://github.com/{username}/{repository name}/tree/{hash}
If you want to hard reset your GitHub repository to a specific commit, do a git reset --hard {hash} in your local (stored on your computer) repository then force push it to a remote repository (like GitHub) via git push --force.
Of course, hard resets are dangerous. You can git revert or git reset and stash unstaged changes, and I'm sure there are other ways to achieve similar effects.

How can I view the history of a single branch in EGit for eclipse

When reviewing code in eclipse using EGit plugin, before merging it to the develop branch, you switch to the branch and see what files changed with the commits on that branch. For this I would like to see -only- the commits to the current checked out branch. When using Team -> Show in History, I get the complete history across all branches.
How can I view only the commits to the current checked out branch, instead of having to search trough the complete GIT history to find the relevant commits?
Using review tools like Gerrit are not an option.
You can check the filter button in the History View:
All Branches
This toggle activates the "All Branches" mode.
By default, only those commits are shown in the commit log that can be reached from the currently checked out commit, i.e. the Commit Graph ends with the currently checked out commit and newer commits are not shown.
If this button is down, all commits will be shown in the commit log.
This is illustrated in the following picture from our example. The branch "beforeSplit" is currently checked out; by activating the toggle, the newer branches will become visible:
And you need to combine that with:
"Additional Refs" toggles the visibility of certain Refs created during actions like fetch, rebase, merge, for example FETCH_HEAD, ORIGIN_HEAD... This can be helpful to remove clutter from the history view.
You might need to un-check the "Additional Ref" option. please see the attached image.