Is it possible to change the order of the commits on my branch?

Asked

Viewed 818 times

3

I wonder if there’s a way I can change the commit orders on my branch.

Reason: I made a huge commit, with low changes and great importance to the system (which can kill the system), and now I want to go making the changes in several commits, to separate what has low importance, from what has great importance.

2 answers

2

I went through a similar problem and the solution that I will pass on now has served me perfectly. I’ll do it as a thorough step-by-step to also help other people who at least have the basics of git, which is to create, delete, update, and switch branch’s, "commit" and others.

At first I created a new branch not to apply commands that may prevent the use of branch in the future.

  1. First update the master, then update your *branch*-antiga from the master and also from the master create a *branch*-nova. The aim will be to apply commit for commit of *branch*-antiga in *branch*-nova;
  2. In the *branch*-antigause the command: git log --author="Fulano da Silva" --name-only. What does this command do? The git-log shows confirmation logs and parameters --author=<pattern> applies a "Pattern" in the header of commit. In that case you’ll get the commit’s by name and --name-only shows the "path/names" of the changed files. Here is an example of the output of the complete code in the terminal:

    commit 94d580b6c9480ac23908799681bebc947a3e760f  
    Author: Djalma Manfrin <[email protected]>
    Date:   Fri Jan 6 11:12:03 2017 -0200
    
    nome do commit
    
    caminho/do/arquivo/arquivo_1.php
    caminho/do/arquivo/arquivo_2.php
    caminho/do/arquivo/arquivo_3.php
    

Note: Notice that the commit are in order. From the latest to the oldest. This order is important for the next step. We will apply commit for commit from the oldest to the newest.

  1. To make it easier, divide the terminal screen into two screens. On a screen run the command of step 2 to list the commit’s and the other, switch to the *branch*-nova and apply the command: git cherry-pick 68e4bde4a83910cb0b4a8df82078e70131ce9280. What does this command do? The git-Cherry-pick applies the amendments in branch current of a commit existing. Amend the hash as explained in step 2, from the oldest to the newest.

Note: Usually when executing the command git cherry-pick does not generate conflict, but if it does, you will have to solve in the arm analyzing the code you want "commit". Each conflict is a particular case and if it happens comment or open a new question on Stack so that we can help you. In case it is not necessary to touch the commit, Jump to step 5

  1. As I had informed in the question and as was also my case. In a single commit there were low and large impact changes to the system as a whole. So in that case I had to apply the command: git reset HEAD^1. What does this command do? The git-reset reboots the HEAD current for the specified state passed as parameters. In our case the specified state will be HEAD^1.

Note: In general, the command git-reset is the opposite of git add. Our specification HEAD^1 means that we want to change/return the HEAD one commit back. This commit is not lost. It goes back as changes that can be "commited". This will allow to revert the changes one commit and "commit it in parts". In our case, it will be possible to separate by commit the complexity in low and large. Finally, commit changes as required by.

  1. After finishing the current commit, back to step 3. This is the way I found to solve my problem. I hope it helps you.

1

You can use the git reset to return to the moment before you perform your commits. Your code will not be lost just go back to Working directory (that moment before you add and commit), and then from there you can slowly add and commit as needed.

To do this, get the hash of the commit before yours (you can use git log to verify this) and pass it as a parameter to git reset, for example:

git reset 2af93c4f8c7c2eb55900a11eg6cf8184be18e041

This way it will appear that the last commit was the one of the hash passed in the reset and all your changes will be available to add...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.