

This can effectively clean the commit-graph in a branch.

#Git rebase force push how to
We've seen how to use Git interactive rebase to squash commits. Instead, we can just execute the command git rebase -i 29976c5. So, we don't have to count how many commits we need to squash. Now let's say we would like to squash all commits and rebase onto the commit 29976c5 with the message: BugFix #1. However, counting a larger X number can be a pain when we have quite a lot of commits in our branch. We've learned that the command git rebase -i HEAD~X is pretty straightforward to squash the last X commits. * cbd350d 23:26:19 +0200 Init commit (Kai Yuan)Īs the slog output shows, we've squashed the last four commits into one new commit, f9a9cd5. Now here's what we'll see if we check the Git commit log once again: $ git slog

Successfully rebased and updated refs/heads/master. If we save the change and exit the editor, Git will do the rebase following our instructions: $ git rebase -i HEAD~4ġ file changed, 1 insertion(+), 1 deletion(-) There's a detailed guideline on how to control each commit and commit message in the commented lines that follow.įor example, we can change the pick command of commits into s or squash to squash them:

So, this is what we should run: git rebase -i HEAD~4Īfter we execute the command, Git will start the system default editor (the Vim editor in this example) with the commits we want to squash and the interactive rebase help information:Īs we can see in the screenshot above, all four commits we want to squash are listed in the editor with the pick command. For example, git push origin +feature will force a push to the feature branch. It's worth mentioning that force push to a public repository could be a dangerous operation, as it may overwrite others' commits.įurther, when we really want to do a force push, we should make sure only to force push the required branch.įor example, we can set the fault property to current so that only the current branch will be pushed/force-pushed to the remote repository.Īlternatively, we can force push to only one branch by adding a “+” in front of the refspec to push. Moreover, if we've squashed already pushed commits, and we would like to publish the squashed result, we have to do a force push. So, in this case, these are the last four commits: * ac7dd5f. It's worth mentioning that when we say “the last X commits,” we're talking about the last X commits from the HEAD.
