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

3

I would like to know if I can change the commit orders of my branch.

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

    
asked by anonymous 08.12.2016 / 20:55

2 answers

2

I went through a similar problem and the solution that I will go over now has answered me perfectly. I will do as a step-by-step minute to also help other people who at least have the git basics, which is to create, delete, update and toggle branch , " commitar "and others.

At first I created a new branch to not apply commands that might make the use of branch

  • First update the master, afterwards, update your *branch*-antiga from the master and also, from the master, create a *branch*-nova . The goal is to commit commit by commit from *branch*-antiga to *branch*-nova ;
  • In *branch*-antiga use the command: git log --author="Fulano da Silva" --name-only . What does this command do? The git-log shows the commit logs and parameters --author=<pattern> applies a " pattern "in the commit header. In this case you will get the commit's by name and --name-only shows the "path / names" of the changed files. Here is an example of the complete code output 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: Realize that commit is in order. From the latest to the oldest. This order is important for the next step. We will commit commit from commit from the oldest to the newest.

  • For ease, split the terminal screen into two screens. On a screen, run the command from step 2 to list the commit's and the other, change to *branch*-nova and apply the command: git cherry-pick 68e4bde4a83910cb0b4a8df82078e70131ce9280 . What does this command do? The git-cherry-pick applies changes to the current branch of a < in> commit . Change the hash as explained in step 2, from the oldest to the newest.
  • Note: Normally running the git cherry-pick command does not generate conflict, but if it does, you will have to solve it in the arm by analyzing the code that you want to "commit." Each conflict is a particular case and if it happens comment or open a new question in Stack so we can help you. If you do not need to move commit , skip to step 5

  • As I had informed the question and how it was also my case. In a single commit there were minor and major 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 resets the current HEAD to the specified state passed as parameters. In our case the specified state will be HEAD^1 .
  • Note: In a rough way, the git-reset command is the opposite of git add . Our specification HEAD^1 means that we want to change / return the HEAD a commit backwards. This commit is not lost. It comes back as changes that can be "committed." This will allow you to make changes to a commit and "commit it in parts". In our case, it will be possible to separate by commit the low and large complexity. Finally, commit changes as needed.

  • After finishing the current commit , go back to step 3. This was the way I found to solve my problem. I hope it helps you.
  • 28.06.2017 / 17:01
    1

    You can use git reset to return to the moment before you perform your commits. Your code will not be lost, it will only come back to working directory (that moment before adding and commit), and then you can add and commit as little as you need.

    To do this, get the previous commit hash (you can use git log to check this) and pass it as a parameter to the git reset, eg

    git reset 2af93c4f8c7c2eb55900a11eg6cf8184be18e041
    

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

        
    09.12.2016 / 00:02