-->

How to Reorder Commits (rebase) with TortoiseGit

2019-04-28 18:32发布

问题:

I want to push several single commits to our main git repository. After doing some reading though it sounds like I have to reorder the commits in order to do this, because git will only push all commits up to a specified commit. First, I don't really understand why this isn't just built in to git (I'm a git newbie). Regardless, I need to reorder the commits and I'm using TortoiseGit. I've found how to get to the rebase menu, but I don't know which options to choose. To get to the rebase menu I:

  1. Right clicked on the directory and chose TortoiseGit / Show Log
  2. Browsed to and selected the commit I wanted to reorder.
  3. Chose "Rebase "master" onto this".

I'm presented with a dialog but I don't know what any of the fields mean.

First, there is the branch field, I see master and a few remotes options. I don't have any branches. I cloned a repository and have been working soley from the local clone so far, why is there a branch option?

Second, there is a little button with left and right arrows on top of each other. What is this button for?

Third, there is an upstream field, by default it has the repository's SHA selected. When I click on the field I see FETCH_HEAD, master, and a few remotes options.

I expect to see a list of commits and I can just move my commit up and down. I am able to see a list of commits by checking the Force Rebase option. Is this what I need to do? What am I forcing though, I thought I just wanted to do a plain old rebase?

Again, I'm a git newbie, any help is appreciated.

回答1:

You are on the right track. Force Rebase is the option you need to use. After you see the commits using the Force Rebase option, select a commit and use the Up/Down buttons to re-order your commits. Press the Start rebase button after you have ordered them as desired.

As regard to why you need to select Force Rebase; Rebase is usually done when you want base your current branch off a newer commit. Since your current branch is already based on the commit that you selected, there is nothing to rebase. In your case, you are simply trying to reorder your commits instead of rebasing. There is nothing wrong in using rebase for that. Just need to select Force Rebase explicitly.



回答2:

This is a workaround solution. Here's what I found:

Definition of upstream and downstream: Definition of "downstream" and "upstream"

Definition of FETCH_HEAD: Search for "what is FETCH_HEAD in git" in Google.

Can I use git command line tools with TortoiseGit?: Can I use command-line Git tools and TortoiseGit simultaneously?

How to reorder commits with git: http://blog.dennisrobinson.name/reorder-commits-with-git/

In this blog post he suggests to use:

git rebase -i HEAD~3

I used

git rebase -i <SHA_of_oldest_commit>

instead. This will provide you with a list of commits from the head to SHA_of_oldest_commit. To reorder the commits I just reordered the lines like I wanted them and saved and closed the file.



回答3:

I was not able to reorder the commits in TortoiseGit even with the help of answers above. Finally I end up with installing Source Tree with much better UI where you can reorder history with just one click (Rebase children of Hash interactivelly...) by Down and Up buttons.



回答4:

Here's the full steps from following geoji's answer.

  1. Open TortoiseGit "Show log" at the root of the repository
  2. Right click a commit that isn't the head and select "Rebase "master" onto this..."
  3. On the Rebase dialog that comes up. Change Upstream to "FETCH_HEAD"
  4. Check "Force Rebase"
  5. Your commit history should now show up inside the Rebase dialog
  6. Click a commit you want to move and use the "Up" & "Down" buttons
  7. After the commits are ordered as you want, press "Start Rebase"
  8. It will change the commit hashes for every commit that has to be moved to for the reorder