Home git How to combine several commits in one

How to combine several commits in one

Author

Date

Category

There is a branch in which 13 commits are consistently. Is there a normal way to combine them?

And in fact, I can go to the main branch and turn into it only the last with the help of Cherry-Pick, because It contains in itself the current version, which is necessary in the end. Although I can make wrong


Answer 1, Authority 100%

Let you want to glue the last three commits (for 13 commits the process looks likewise). To do this, there is a great method using Git Rebase . This command allows you to change the history of commits. The work algorithm looks like this:

  1. Back up. It is not at all necessary, but will help keep nervous cells if something goes wrong. Options:

    1. copy of the directory with files in which the Git repository is deployed.
    2. Git Branch Backup or Git Tag Backup In the last com.
    3. read the help of the reflog and reset and know that the backups are already there.
  2. Get rid of uncomfortable changes (Git Add + Git Commit or Git Stash or something else).

  3. Run Git Rebase -i Head ~ 3 . In response to this, you will receive a “dialog” (file editing window) of the type:

    Pick BCDCA61 Second Commit
    PICK 4643A5F The Third Commit With Cool Stuff
    Pick E0CA8B9 The Last Commit
    # Rebase 48411de..E0CA8B9 ONTO 48411DE
    #
    # Commands:
    # P, Pick = Use Commit
    # R, REWORD = USE COMMIT, But Edit The Commit Message
    # E, Edit = USE Commit, But Stop for Amending
    # S, Squash = Use Commit, But Meld Into Previous Commit
    # F, Fixup = Like "Squash", But Discard This Commit's Log Message
    # X, Exec = Run Command Using Shell
    

    At the same time, the commits are specified in the order of increasing the creation time (the lowest – the most recent).

  4. In “Dialog” from paragraph 2, you need to replace pick on Squash for the two most recent commits (two bottom strings ). In the example above, it should look like this:

    Pick BCDCA61 Second Commit
    Squash 4643A5F The Third Commit With Cool Stuff
    Squash E0CA8B9 THE LAST COMMIT
    # Rebase 48411de..E0CA8B9 ONTO 48411DE
    #
    # ...
    

    After that, you must close this “dialog” (save the editable file). If the VI (default) is used to work with GIT, then this is done by the sequential press of ESC , enter : WQ and press ENTER .

  5. In the next “dialog” you will be offered to specify a header for the resulting commit.

Here here Deployed information on how to overwrite the story in the Git.


Answer 2, Authority 13%

There is such a pretty destructive method (an example of the fusion of the last 12 commits):

# Reset The Current Branch to the Commit Just Before Last 12:
Git Reset --hard Head ~ 12
# Head @ {1} IS WHERE THE BRANCH WAS JUST BEFORE THE PREVIOUS COMMAND.
# This Command Sets The State of the Index to Be As It Would Just
# After a Merge From That Commit:
Git Merge --squash Head @ {1}
# COMMIT THOSE Squasched Changes. THE COMMIT MESSAGE WILL BE HELPFULLY
# Prepopulated With The Commit Messages of All The Squashed Commits:
Git Commit.

It is taken from here: https://stackoverflow.com/questions/ 5189560 / Squash-My-Last-X-COMMITS-TOGETHER-Using-Git


Answer 3, Authority 4%

To combine the two and later commits using Git Reset , you must execute the following commands:

Git Reset --Soft Head ~ N 
Git Commit --Mend.

Instead of N you need to specify the number of commits you want to combine.
After executing the second command, you will need to enter a message (comment) for a new combined commit.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions