Stop Following Everyone on Social Media

If I had a superpower, I would like to read other people’s minds. That was my answer back in the 90s. Today, you don’t need to be a superhero to have this power. We can now read everyone’s thoughts —…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Clean commits without rebasing

It is a good practice to avoid too many commits on a pull request. However, to avoid losing your changes, in case your computer blows up, another good practice is to commit often and push your commits to the cloud.

Also, in the process of working on a ticket, you might have to merge with the base branch every once in a while to get the latest changes and escape conflicts.

Let’s say you are done with your work and you want to merge with the main branch, and in that process, you also want to clean any commits you pushed to remote while working on the feature. In the end, you just want a single commit (maybe a couple of commits) on your pull request. The classic way of doing this is a combination of git merge and git rebase.

I want to present you with an alternative that hopefully makes this process a little bit less painful.

Assuming you have your branch checked out:

To update your branch with main. Main is your base branch when you started working (change to master or develop if that is the case). At this point, you have all the previous commits + a merge commit with the latest changes from the base branch (fix any possible conflicts after this).

To put your changes on top of the base branch. This resets all the commits and keeps the changes you made staged and ready for that final commit. After this, you may need to add any unstaged changes (git add -A).

To commit everything. This will be like all your changes were on a single commit.

To push your changes to the remote branch. The — force-with-lease flag is needed because you are rewriting the commit history on your branch, replacing any commits with a new one.

Who said you can’t rewrite history?

This is basically all the commands above in one line

Let’s suppose that after you went through all the steps above you got a comment on your PR to change something and you don’t want to reset it again.

Change what you need and add them all to be committed with git add -A.

Now you have two options, First option:

Second option:

After one of the options above, you just have to push everything again with the force flag to override the remote commit (git push origin [my-branch] -f).

Difference between force and force with lease

More details here:

Hopefully this you make your life easier if you want to have clean commits. Thanks for reading!

Add a comment

Related posts:

Scrum no es simplemente un proceso

En el Manifiesto por el desarrollo Ágil, la primera línea es “Individuos e interacciones sobre procesos y herramientas”. La forma en que interpreto este principio es que la primera prioridad en el…

INTRODUCING MOONBEAM NETWORK

The uniqueness of every project is in the new innovation it brings or an existing problem that it solves. It all about adding value and bringing about a postive change. This article is going to…

Introduction

I joined TechTeam in the Autumn Quarter of my first-year, hoping to use the technical skills I had picked up throughout high school to work on projects making a real impact in the community. Working…