How to crush my branch git makes a transition to one branch without rebooting?

I have a local branch, we use git -flow with Pull Requests, and I'm looking to crush a few commits after getting PR feedback.

How can I squash all my commits (e.g. from PR) into one branch?

I assume it will be something like:

git checkout master # For master git pull # Get all branches up-to-date git checkout feature1 # Checkout the branch git checkout -b feature1_squash # Make a copy of the branch git branch -D feature1 # Delete original branch git checkout master # (?) Branch off latest master for safety git checkout -b feature1 # Make new (empty) original branch git merge --squash feature1_squash # Merge with squash into it git push -f feature1 # Push, force will be required 

but I'm not sure.
Using these many steps also seems to be a good argument in favor of using a function to tie it all together and simply pass the branch name as a parameter. Of course, automation will mean that you need to handle errors, exceptions, cross cases, etc.

I do not want to use interactive reboot, because it is a little difficult to do for the beginners that I train. I also do not want to know the number of commits, I just want to do all the ones that exist in this thread.

+7
git git-branch git-checkout git-merge git-squash
source share
3 answers

The answer from VonC is almost there. but knowing that you want to go 3 fixes back is difficult.

you should use merge-base instead

 git reset --soft $(git merge-base YOUR_BRANCH WHERE_YOU_BRANCHED_FROM) git commit 

and edit your post (or use git commit -m instead)

if you are on this branch, you can use HEAD and assuming that you are forked using the wizard, which will be your commands (using origin / master due to the potential obsolescence of the local master)

 git reset --soft $(git merge-base HEAD origin/master) git commit 

If you cannot remember why you are branching, the place where you merge PR back is probably correct

+4
source share

I think that you offer unconventional and more complicated for beginners than using rebase -i for squash.

What is difficult in git rebase -i HEAD~N (with N is the number of commits to return to the distribution)? You just read the list and gouge commits as needed. The Overwrite History guide contains more detailed information, and I think it is great for a new user.

You can always make a demo branch for training. Just make 5-6 commits for the branch for demo purposes and ask the listener to shout commits with interactive permutation, then do PR to prove that they can handle it in a live branch.

+1
source share

An alternative to interactive permutation for squash commits (especially sequential commits on a branch) is git reset --soft .
See " Squash my last X commits together using Git "

 git reset --soft HEAD~3 && git commit --edit -m"$(git log --format=%B --reverse HEAD..HEAD@ {1})" 

This is one of the Practical Use of git reset --soft "that I mentioned earlier.

0
source share

All Articles