Merge multiple transactions into one

This question concerns not only how to accomplish this task, but also whether it is good or bad practice with Git.

We believe that locally I work most of all on the main branch, but I created a thematic branch, which I will call "topical_xFeature". In the process of working on "topical_xFeature" and moving back and forth to do other work on the lead branch, it turns out that I made more than one commit in the "topical_xFeature" branch, but I did not push between each commit.

First , consider this bad practice? Wouldn't it be wiser to stick to one commit per branch per push? In what cases would it be nice to have several commits on a branch before pushing?

Second , what is the best way for me to transfer several commits to the topical_xFeature branch to the main branch for pushing? Isn't it worth it to worry about and just pushing when multiple commits are pushed, or is it less annoying to somehow merge the commits into one and then push? Again, how to do this?

+118
git workflow
Apr 19 '11 at 19:26
source share
6 answers

For your first question, there is nothing wrong with immediately pushing a few commits. Many times you can split your work into several small logical commits, but just push them as soon as you feel that the whole series is ready. Or you can make several commits locally while you are disconnected and you push them all as soon as you reconnect. There is no reason to limit yourself to one latch per push.

I usually think that it’s a good idea to keep each latch a single logical, sequential change that includes everything that it needs to work (so it does not leave your code in a broken state). If you have two commits, but they can lead to code breaking, if you applied only the first, it might be a good idea to crush the second commit in the first. But if you have two commits where each of them makes reasonable changes, then clicking them as separate commits is fine.

If you want to compress multiple commits together, you can use git rebase -i . If you are in the topical_xFeature branch, you run git rebase -i master . This will open an editor window with a bunch of commits listed in the pick prefix. You can change everything except the first to squash , which Git tells you to save all these changes, but put them in the first commit. After you have done this, check out master and merge your function branch:

 git checkout topical_xFeature git rebase -i master git checkout master git merge topical_xFeature 

Alternatively, if you just want to deflate everything in topical_xFeature in master , you can simply do the following:

 git checkout master git merge --squash topical_xFeature git commit 

Which one you choose is up to you. Actually, I wouldn’t worry about having a few small commits, but sometimes you don’t want to worry about additional minor commits, so you just crush them into one.

+127
Apr 19 '11 at 19:52
source share

This is exactly the way I usually follow to combine multiple commits into one commit before pushing the code.

To achieve this, I suggest you use the squash concept provided by GIT.

Follow these steps.

1) git rebase -i wizard ( you can also use a specific commit instead of a wizard )

open the rebase interactive editor where it will show all your commits. Basically, where you need to identify the commits that you want to merge into a single commit.

Imagine that these are your commits and something similar is shown in the editor.

 pick f7f3f6d changed my name a bit pick 310154e updated README formatting and added blame pick a5f4a0d added cat-file 

It is important to note that these commits are listed in reverse order than you usually see them with the log command. So the old commit will be shown first.

2) Change 'pick' to 'squash' for the latest committed changes. something like shown below. At the same time, your last 2 commits will be merged with the first.

 pick f7f3f6d changed my name a bit squash 310154e updated README formatting and added blame squash a5f4a0d added cat-file 

You can also use a short form if you have many commits to combine:

 p f7f3f6d changed my name a bit s 310154e updated README formatting and added blame s a5f4a0d added cat-file 

use 'i' for editing, it will enable the editor to insert. Keep in mind that most of the (oldest) commits cannot be crushed, since there is no previous commit that could be merged with. So this should be selected or "p". Use "Esc" to exit insert mode.

3) Now save the editor using the following command. : WQ

When you save this, you have one commit that represents the changes of all three previous commits.

Hope this helps you.

+62
Nov 28 '12 at 11:58
source share

First : nothing says that you have only one commit for each branch on push: push is a publishing mechanism that allows you to publish a local history (i.e. a collection of commits) with a remote repo.

Second : a git merge --no-ff topical_xFeature will write to master as a single text for your theme before clicking master .
(This way you save topical_xFeature for future evolutions, which you can write to master as one new commit at the next merge --no-ff.
If getting rid of topical_xFeature is the goal, then git merge --squash is the right option, as described in Brian Campbell's answer .)

+11
Apr 19 '11 at 19:51
source share

Switch to the main branch and make sure you are in the know.

 git checkout master 

git fetch it may be necessary (depending on your git config) to receive updates on origin / master

 git pull 

Merge the branch of objects with the main branch.

 git merge feature_branch 

Reset the primary branch.

 git reset origin/master 

Git now considers all changes unspecified. We can add these changes as one commit. Adding will also add untracked files.

 git add --all git commit 

Link: https://makandracards.com/makandra/527-squash-several-git-commits-into-a-single-commit

+8
Jul 25 '16 at 19:36
source share

1) First, select which commit you want everything to be after.

 git reflog 5976f2b HEAD@{0}: commit: Fix conflicts 80e85a1 HEAD@{1}: commit: Add feature b860ddb HEAD@{2}: commit: Add something 

2) Reset to your chosen head (I chose HEAD @ {2})

 git reset b860ddb --soft 

3) git status (just to be sure)

4) Add your new commit

 git commit -m "Add new commit" 

Note: HEAD@{0} & HEAD@{1} now combined into 1 commit, this can be done for several commits.

git reflog should again display:

 git reflog 5976f2b HEAD@{0}: commit: Add new commit b860ddb HEAD@{1}: commit: Add something 
+3
Nov 16 '18 at 10:48
source share

If you’re a newbie like me and can’t imagine what the merger will bring to your plate, this is the simplest explanation.

The simplest description of how squash in git

0
Jul 17 '19 at 7:10
source share



All Articles