How to use git merge --squash?

后端 未结 13 1426
夕颜
夕颜 2020-11-22 05:41

I have a remote Git server, here is the scenario which I want to perform:

  • For each bug/feature I create a different Git branch

  • I keep on com

相关标签:
13条回答
  • 2020-11-22 06:00

    Merge newFeature branch into master with a custom commit:

    git merge --squash newFeature && git commit -m 'Your custom commit message';
    

    If instead, you do

    git merge --squash newFeature && git commit

    you will get a commit message that will include all the newFeature branch commits, which you can customize.

    I explain it thoroughly here: https://youtu.be/FQNAIacelT4

    0 讨论(0)
  • 2020-11-22 06:01

    You want to merge with the squash option. That's if you want to do it one branch at a time.

    git merge --squash feature1
    

    If you want to merge all the branches at the same time as single commits, then first rebase interactively and squash each feature then octopus merge:

    git checkout feature1
    git rebase -i master
    

    Squash into one commit then repeat for the other features.

    git checkout master
    git merge feature1 feature2 feature3 ...
    

    That last merge is an "octopus merge" because it's merging a lot of branches at once.

    Hope this helps

    0 讨论(0)
  • 2020-11-22 06:04

    Suppose you worked in feature/task1 with multiple commits.

    1. Go to your project branch (project/my_project)

      git checkout project/my_project
      
    2. Create a new branch (feature/task1_bugfix)

      git checkout -b feature/task1_bugfix
      
    3. Marge with the --squash option

      git merge --squash feature/task1
      
    4. Create a single commit

      git commit -am "add single comments"
      
    5. Push your branch

      git push --set-upstream origin feature/task1_bugfix
      
    0 讨论(0)
  • 2020-11-22 06:06

    Use

    git status 
    

    to check what's going on.

    Then

    git checkout master 
    git merge --squash bugfix
    git add (add which files you want or use wildcard command like ".")
    

    Then

    git commit -m "message"
    

    And now last but not the least

    git push -u origin master
    

    Here origin can be other remote you prefer.

    0 讨论(0)
  • 2020-11-22 06:12

    You can use tool I've created to make this process easier: git-squash. For example to squash all commits on feature branch that has been branched from master branch, write:

    git squash master
    git push --force
    
    0 讨论(0)
  • 2020-11-22 06:15

    What finally cleared this up for me was a comment showing that:

    git checkout main
    git merge --squash feature
    

    is the equivalent of doing:

    git checkout feature
    git diff main > feature.patch
    git checkout main
    patch -p1 < feature.patch
    git add .
    

    When I want to merge a feature branch with 105(!!) commits and have them all squashed into one, I don't want to git rebase -i origin/master because I need to separately resolve merge conflicts for each of the intermediate commits (or at least the ones which git can't figure out itself). Using git merge --squash gets me the result I want, of a single commit for merging an entire feature branch. And, I only need to do at most one manual conflict resolution.

    0 讨论(0)
提交回复
热议问题