Squash commits pushed in Git
This article outlines the process of squashing commits that we have pushed to a remote repository. We squash the commits into one to reduce clutter in the repository.
To squash the commits, we run git rebase in interactive mode .
Squash commits pushed in Git
For the sake of simpler context, we will simulate a scenario where we need to squash several commits into one after pushing changes.
First, we will push our current changes to the remote repository.
$ git push origin Dev2.1
This command will push all changes to our remote branch Dev2.1.
We can run git rebase
the command until the 5th commit.
$ git rebase -i HEAD~5
Output:
To squash our commits, we'll replace pick with squash at the start of the commit and complete the rebase.
We can run the git log command to view our commit history.
Attempting to push will result in an error as shown below.
The error is self-explanatory; we won’t discuss it too much.
We need to force the push, but not by using the --force flag, as shown below.
$ git push origin +Dev2.1
Output:
Let's take a quick look at the remote repositories on GitHub.
In our case, we used + before the refspec to force a push to the Dev2.1 branch.
In summary, a normal git push will not do you any good after squashing the commits that have already been pushed to the remote.
You'll get stuck in an annoying loop where Git requires you to pull before you can push. git push origin
Adding a + before the branch in the command will fix the problem.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Git squash all commits
Publish Date:2025/03/31 Views:65 Category:Git
-
In every developer’s life, the word squash is often used while working with the Git distributed control system . This feature in Git is a handy option that developers often use to achieve a neat workflow in their development team. In this
Close the Git commit editor on Windows
Publish Date:2025/03/31 Views:62 Category:Git
-
In this article, we will discuss how to exit the Git commit editor. This can be a little tricky, especially if you are new to Git bash . Let's see how to exit the editor on Windows. Close the Git commit editor on Windows We will look at a t
Understand the Git commit sign-off function
Publish Date:2025/03/31 Views:160 Category:Git
-
In this article, we will explore the Git commit sign-off feature. This feature allows us to add a line containing our full name and email address to our commits. Signature confirming ownership and permission to submit submission. Understand
Add the file to the last commit in Git
Publish Date:2025/03/31 Views:176 Category:Git
-
This article outlines the process of adding a file to the last commit in Git. This comes in handy when you forgot to include a file in the last commit and don't want to create a new file. Let’s get straight to the point. Add the file to t
Git search commit messages using the command line
Publish Date:2025/03/31 Views:157 Category:Git
-
We can format git log the command to show commits with commit messages that match a specified pattern. This makes it easier when you want to find a commit but your repository has hundreds of commits. This article will discuss the process of
Git commit some files in one branch and make them available in another branch
Publish Date:2025/03/31 Views:194 Category:Git
-
This article shows how to commit specific files in one branch and make those files available in another branch. Suppose you have a project and you create a feature branch to make some slight modifications to the code. You modify and add new
Modifying a specific commit in Git
Publish Date:2025/03/31 Views:100 Category:Git
-
This article explains how we can modify a specific commit in Git. We may need to rename, compress, edit, or add files to a commit. The best way is to use the git rebase command in interactive mode . What do you think of this? Modifying a sp
Git overwrites Master with branch
Publish Date:2025/03/31 Views:79 Category:Git
-
Git is used to keep track of the source code we are working with; it also facilitates collaboration and helps us keep our projects in their current state. As we develop new features, their history should be at our fingertips as it is very h
Git Squash Commits
Publish Date:2025/03/31 Views:162 Category:Git
-
We will learn about Git squashing in this tutorial. The basic idea is to merge multiple consecutive commits into one. The main purpose is to compress many commits into a few related commits. Thus, doing so will make the git history look con