3

What I have done

In local repo:

  1. git checkout -b feature/db/projectImport dev
  2. make changes
  3. git add . and git commit
  4. git push origin feature/db/projectImport (staying in that branch)

Now in GitHub:
5. Click the Compare & pull request button and finally merge with the default merge button, which claim to use --no-ff



Pull request Image pull request


What I want to achieve

  • I don't want the commits messages from the feature branch to appear in the commit messages in the dev branch.
  • When I merge the feature branch into dev, the feature branch will retain all its commits but the dev branch will only have the merge commit. Is this possible???

Related Images from the repo

Feature branch commits: feature branch commits

Dev branch commits: dev branch commits

Note:

  • I am newbie in git. So, my thinking can be wrong. If this is the case, please point out my mistake and tell what is correct.
  • Any suggestion gratefully received. Thanks in advance.
Knowledge Man
  • 309
  • 2
  • 10
  • The imperative keyword here is *squash*. – Jonathon Reinhart Jan 28 '22 at 13:25
  • @JonathonReinhart According to my little knows, `squash` combines commits. But I want to be able to see all the commits when I go to the feature branch and at the same time not want to se those in the dev branch. If I am thinking wrong, can you give me some reference links. – Knowledge Man Jan 28 '22 at 13:28
  • The commits are either present in git or they are not. Being able to "see" them is a function of whatever user interface you're using. To hide them in the dev branch would be disingenuous, and is just not how git works. – Jonathon Reinhart Jan 28 '22 at 13:30
  • @JonathonReinhart If, I use `squash`, will I be able to see the squashed commits anymore. If this is the case, then existence of the feature branch has no meaning, right??? – Knowledge Man Jan 28 '22 at 13:32
  • I think you're indeed looking for squash. The squast operation affects what gets put on the dev branch, or whatever you're merging onto. If you want to see the original commits, you just have to keep the feature branch around. As long as you don't delete it, you'll be able to see what's on it. And the PR will keep a pointer to it, too. – joanis Jan 28 '22 at 13:57

1 Answers1

2

As others have pointed out in the comments, what you are looking for is the squash option when you merge the PR in.

When you squash-merge, that changes what appears on the destination branch, but it does not change what is on the source branch. So on dev, you'll see one commit. But on the feature branch, as long as you don't delete it, you'll continue to see all your original feature commits. And the PR will continue to hold a pointer to that branch.

Now, that's a bit of a funny workflow to me: when I merge a PR in, I systematically delete the feature branch. But your workflow should work fine too.

How to do it

On GitHub

Once you have your PR ready to merge, make sure you merge it in using GitHub's "squash and merge" method:

squash and merge a PR

On the Git CLI

If you're doing the merge on your own computer, the same operation can be accomplished like this:

git checkout dev
git merge --squash feature/feature-name
git push origin dev

The results

With either method, you will get to write a new message for the squashed commit that will get added to dev.

Once you've completed the merge, dev will have one new commit, and branch feature/feature-name will remain unmodified.

joanis
  • 10,635
  • 14
  • 30
  • 40
  • Why is it needed to delete the feature branch? Can the feature branch be of any use later? – Knowledge Man Jan 28 '22 at 14:12
  • 1
    There's no need to delete the feature branch at all. But I just find no use for it myself: once the PR is merged, the main history has the commits I want to keep. But if you want to keep the history of commits *and* squash merge, then just keep the feature branch around. – joanis Jan 28 '22 at 14:14
  • But I think after a while you'll find your repo unwieldy, with a lot of feature branches hanging around. – joanis Jan 28 '22 at 14:16
  • I was creating a workflow for my own project actually. I have two long lived branches `main` & `dev`. Then I have short lived branches `feature`, `bug`, `hotfix`, etc. The `main` branch always remains production ready. And I will delete the last feature branch when I merge `dev` into `main`. – Knowledge Man Jan 28 '22 at 14:21
  • In the above workflow, should my main branch have the commits from the dev branch when being merged, or should I apply `squash` when merging `dev` into `main`. Asking for suggestions. – Knowledge Man Jan 28 '22 at 14:24
  • I think merging dev into main should not squash. Each commit in dev, with your workflow, represents a feature and its commit message, which documents what the feature is. That's valuable history that should be preserved in main too. – joanis Jan 28 '22 at 14:33
  • I just now found that, using `squash` also changes the commits in the feature branch. So, the commits I squashed... I cannot get them or they no longer exists in feature too – Knowledge Man Jan 28 '22 at 14:45
  • What I was wanting is: when I merge the `feature` branch into `dev`, the feature branch will retain all its commits but the `dev` branch will only have the merge commit. **is this possible???** – Knowledge Man Jan 28 '22 at 14:47
  • Did you do "squash and merge" when your merged the PR? I just tested it again to confirm, and that operation added one commit to my master branch, and keep my feature branch untouched, with all the commits it previously had. Yes, what you ask is possible. How exactly did you merge? So... don't squash on the feature branch itself, maybe that's what you did. You need to do a squash merge proper. – joanis Jan 28 '22 at 15:00
  • @KnowledgeMan I just edited my answer to say how to do it. If that doesn't work for you, please explain exactly what steps you are following, and what you see that's different from what I describe. – joanis Jan 28 '22 at 15:16
  • Do I get to write the commit message for this merge-commit? – Knowledge Man Jan 28 '22 at 15:44
  • Also, then what is the `--no-ff` is for??? – Knowledge Man Jan 28 '22 at 15:47
  • Here is an excellent answer to that question: https://stackoverflow.com/a/6701322/3216427 – joanis Jan 28 '22 at 16:41
  • And yes, when you create the squash merge, you will be prompted to edit the message before you commit. – joanis Jan 28 '22 at 16:42
  • Can you answer this: [link](https://stackoverflow.com/questions/70897377/possible-issue-after-doing-squash-and-merge) - very much related to this question – Knowledge Man Jan 28 '22 at 16:58
  • Just got to it, and I see @TTT answered it for you. My advice would be similar. – joanis Jan 28 '22 at 20:19
  • The problem is solved. – Knowledge Man Jan 28 '22 at 20:24