Search code examples
gitrevision-history

How can I use Git to add new commits to the current branch based on a previous commit in that same branch?


Let's say I have 2 commits in my git log. Now I want to go back to the first commit and modify some files and add that as the third commit. When I type in git log I want 3 commits to be there on the same branch. git reset --soft firstcommit and then commiting again wipes my second commit. How do I achieve what I want?


Solution

  • Suppose you are now at thirdcommit.

    You can use git cherry-pick secondcommit to get it back.

    And the log will be

    firstcommit -> thirdcommit -> secondcommit
    

    If you want the log to be like:

    firstcommit -> secondcommit -> thridcommit
    

    you can do :

    git reset --hard secondcommit
    git cherry-pick thridcommit