How to reorder last two commits in git?

前端 未结 4 1135
你的背包
你的背包 2021-02-02 07:09

I want to reorder last two commits in git:

right now I have:

$ git log --oneline -4
1e0ecba (HEAD, my-branch) Fix for T255
82d45dc django_extensions
af3953         


        
4条回答
  •  半阙折子戏
    2021-02-02 07:09

    A little late to the party, but I have written the following answer from the standpoint of understanding how few git commands work. The other answers will get your work done in a shorter way with the help of interactive rebase commands. I had encountered this similar situation and this is how I solved my problem -

    Suppose this is how your last 4 commits look like.

    * ca6807e - (HEAD -> lolo) third (4 seconds ago) 
    | 
    * 61a069c - (master) fourth (18 hours ago) 
    | 
    * f3b0255 - (lol) second (2 days ago) 
    | 
    * c2f5e4f - first (2 days ago) 
    

    Now according to your question you want to make the commit history as follows (Ignore the rest of the commit ids)-

    * 0fe6482 - (HEAD -> master) fourth (5 seconds ago) 
    | 
    * 2c3ba40 - (lol) third (4 minutes ago) 
    | 
    * f3b0255 - (lolo) second (2 days ago) 
    | 
    * c2f5e4f - first (2 days ago) 
    

    To do that follow these steps -

    1. Go to the 3rd last commit

      git reset HEAD~2

      Now your HEAD will point at the 'second' commit

    2. At this point, we will switch our branch from master to lol (look at the first tree diagram)-

      git checkout lol

      Now if you do git status you may find some files are in the untracked section that is because you checked back to a previous commit in the history which which did not have those files in tracking mode but your remote repository has them. So you have to just ignore those files for now. But git will not let you move forward with our approach if we keep those files untracked. So we add all of them by doing git add * followed by git stash which will save those files in a temporary stack (ignoring them temporarily and then allowing us to pop those files from this stack later when we need).

    3. Now we need to move our 'fourth' commit on the current branch 'lol'. To do that we need to use git cherry-pick command. Copy the commit id of the 'fourth' commit which is ca6807e in my case. Now type git cherry-pick ca6807e. Now the tree structure will look something like this -

      * 2c3ba40 - (HEAD -> lol) third (3 seconds ago) 
      |   
      | * 61a069c - (master) fourth (18 hours ago) 
      |/  
      | 
      * f3b0255 - (lolo) second (2 days ago) 
      | 
      * c2f5e4f - first (2 days ago) 
      

    Now you just need to do switch back to master branch and rebase lol branch on it.

    git checkout master
    git rebase lol
    

    And now you have successfully interchanged the last two commits. Here is how the tree structure looks like now -

    * 0fe6482 - (HEAD -> master) fourth (5 seconds ago) 
    | 
    * 2c3ba40 - (lol) third (4 minutes ago) 
    | 
    * f3b0255 - (lolo) second (2 days ago) 
    | 
    * c2f5e4f - first (2 days ago) 
    

提交回复
热议问题