How to use git merge --squash?

后端 未结 13 1447
夕颜
夕颜 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:17

    To squash your local branch before pushing it:

    1. checkout the branch in question to work on if it is not already checked out.

    2. Find the sha of the oldest commit you wish to keep.

    3. Create/checkout a new branch (tmp1) from that commit.

      git checkout -b tmp1 <sha1-of-commit>

    4. Merge the original branch into the new one squashing.

      git merge --squash <original branch>

    5. Commit the changes which have been created by the merge, with a summary commit message.

      git commit -m <msg>

    6. Checkout the original branch you want to squash.

      git checkout <branch>

    7. Reset to the original commit sha you wish to keep.

      git reset --soft <sha1>

    8. Rebase this branch based on the new tmp1 branch.

      git rebase tmp1

    9. That's it - now delete the temporary tmp1 branch once you're sure everything is ok.

    0 讨论(0)
  • 2020-11-22 06:18
    git checkout YOUR_RELEASE_BRANCH
    git pull
    git checkout -b A_NEW_BRANCH
    git merge --squash YOUR_BRANCH_WITH_MULTIPLE_COMMITS
    git commit -am "squashing all commits into one"
    git push --set-upstream origin A_NEW_BRANCH
    
    0 讨论(0)
  • 2020-11-22 06:20

    Say your bug fix branch is called bugfix and you want to merge it into master:

    git checkout master
    git merge --squash bugfix
    git commit
    

    This will take all the commits from the bugfix branch, squash them into 1 commit, and merge it with your master branch.


    Explanation:

    git checkout master
    

    Switches to your master branch.

    git merge --squash bugfix
    

    Takes all commits from the bugfix branch and groups it for a 1 commit with your current branch.
    (no merge commit appears; you could resolve conflicts manually before following commit)

    git commit
    

    Creates a single commit from the merged changes.

    Omitting the -m parameter lets you modify a draft commit message containing every message from your squashed commits before finalizing your commit.

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

    if you get error: Committing is not possible because you have unmerged files.

    git checkout master
    git merge --squash bugfix
    git add .
    git commit -m "Message"
    

    fixed all the Conflict files

    git add . 
    

    you could also use

    git add [filename]
    
    0 讨论(0)
  • 2020-11-22 06:24

    I know this question isn't about Github specifically, but since Github is so widely used and this is the answer I was looking for, I'll share it here.

    Github has the ability to perform squash merges, depending on the merge options enabled for the repository.

    If squash merges are enabled, the "Squash and merge" option should appear in the dropdown under the "Merge" button.

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

    If you have already git merge bugfix on main, you can squash your merge commit into one with:

    git reset --soft HEAD^1
    git commit
    
    0 讨论(0)
提交回复
热议问题