~/git/git-how-create-pull-request-with-no-history-commits.md

Git - How to create a Pull Request with no history of commits

3 min readJune 11, 2020by Gorav Singal#git
// TL;DR

Use git rebase or similar techniques to squash multiple intermediate commits into a single commit before creating a pull request, keeping PR history clean for reviewers.

Git - How to create a Pull Request with no history of commits

Introduction

If you working on a github project in a team. Consider you have created a branch from Master branch, and did few commits as we normally do.

When the feature work is done on that branch. You would want to commit that in Master branch. But, when you create a Pull Request (PR) for that merge. Reviewer will see the intermediate commits in your PR. Which might annoy him. And there is no benefit of incorporating that history into Master branch. Although, there is a “Squash and Merge” option. But, anyone can easily forget that because this option is not enabled by default.

So, reviewer would want to cleanup your commit history so that only one commit is visible in the PR.

This post is about create a PR without historical commits. So that, reviewer will only see single commit containing all the file changes.

Method-1 Using Git rebase

Apart from git merge, there is a git rebase option too. Git merge and git rebase both are used to integrate your changes into main branch.

# Switch to your feature branch
git checkout FeatureBranch

# in this case, our main branch is master
git rebase master

If there is no conflicts, this will work. Now, you just to push the changes.

git push -f

Method-2 Using Git rebase with interactive option

We will use interactive way of doing git rebase. Interactive rebasing is used to manipulate your history. And we would want to clean up history.

# Switch to your feature branch
git checkout FeatureBranch
# start interactive rebasing
# in this case, our main branch is master
git rebase -i master

This will open up an editor (vim kind) and few options. See example below:

pick 8472as3 add Test bug-1
pick 92731cf add feature description
pick asf4234 bug fix
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell

It start with your commits list. Now you have to edit this. Note the default: pick keyword in front of each commits. And, below that there are various options. Now, we want to have all the commits into one. Put pick keyword with only one commit, that will be there in final history. Put squash keyword in-place of pick in front of commits. The file looks like:

pick dae2691 add amazing feature
squash 3491879 add test
squash 3fedbd5 fix typo
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell

Save it, and quit editor. On console, it will show some messages. Messages ends with something like:

Successfully rebased and updated refs/heads/FeatureBranch.

Now you have to push your changes. Just run:

git push -f

Now create your PR, and you will see only one commit in history.

Bonus

If your feature branch is forked from another git repo. Use below commands:

git checkout FeatureBranch
git remote add upstream https://github.com/<main_repo>/PROJECT.git
git rebase -i upstream/<main_branch_name>
# after saving editor content.
git push -f

Note: You might see some conflicts, which you need to resolve before pushing changes.

Method-3 How to create PR without history by creating another branch

Assume, You created a branch named: FeatureBranch from Master branch. And you did some commits to it.

Git commits

Now, create another branch from Master branch. Lets name it:

feature_branch_no_history

Now, You have three branches:

  • master
  • feature_branch (your active branch, having commits)
  • feature_branch_no_history (same as Master)

Now, we will do this in two steps:

  1. Now, create a pull request for merging feature_branch into feature_branch_no_history and merge it by using “Squash and Merge” way.
  2. Create another pull request from feature_branch_no_history to master branch.

Git commits

Note: you will see more than one commits in this process too.

Happy code reviewing. Happy reviewer… Happy Coding…

$ subscribe --weekly

One deep dive a week, in your inbox

System design, AI engineering, trading systems and interview prep. Tested code, real numbers. Free, and you can unsubscribe any time.

or read it on gyanbyte.substack.com

Share

Related Posts

A Practical Guide in understanding Git Branch and Conflict resolution during merge

A Practical Guide in understanding Git Branch and Conflict resolution during merge

Introduction In this guide, We will learn about branching, handling conflict…

A Practical Guide on Understanding Git Best Practices

A Practical Guide on Understanding Git Best Practices

Introduction In this post, we will learn about some of Best practices while…

A Practical Guide on how to work with Git log command and history

A Practical Guide on how to work with Git log command and history

Introduction In this post, we will see ways to look at git history logs. For…

A Practical Guide on how to work with Git Basic Commands and workflows

A Practical Guide on how to work with Git Basic Commands and workflows

Introduction In this guide, we will see git basic commands, and fundamentals of…

A Practical Guide on how to to create your own git command alias

A Practical Guide on how to to create your own git command alias

Introduction In this guide, We will learn on how to create some handy command…

A Practical Guide for better understanding Git Diff

A Practical Guide for better understanding Git Diff

Introduction In this guide, We will get basic understanding of various options…

Latest Posts

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

In the previous article I argued that AI is not killing software engineering…

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

A few months back I approved a PR that was almost entirely AI-generated. It…

How to Debug AI-Generated Code: Understand First, Fix Last

How to Debug AI-Generated Code: Understand First, Fix Last

In the last article I wrote about reviewing AI code before it goes in. This one…

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

Every few weeks somebody sends me a scary headline — “AI will replace all…

The Transactional Outbox Pattern: Never Lose an Event Again

The Transactional Outbox Pattern: Never Lose an Event Again

This is one of those patterns that looks like nothing until you get burnt once…

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Here is a question I like to ask in interviews: your bank account shows a…