

Sourcetree discard commits series#
There are several ways to "undo" or "redo" a series of commits, depending on the outcome you're after. If the whole concept of rewriting history sounds strange to you, make sure to first revisit this post on Immutable Snapshots - One of Git's Core Concepts to get your bearings. Now, let's see how the accidentally committed file tmp.log can be removed from the history and C1 & C2 redone. Notice that none of the two commits ( C1 & C2) have been pushed remotely, and only exist in the local master branch, allowing us to easily rewrite history as we please. Start case where changes to the file "tmp.log" have been accidentally committed as part of commits "C1" and "C2".


Below is a short linear-history consisting of three commits, where the two latter ( C1 & C2) contain changes to a log file ( tmp.log) that have been accidentally committed. Start caseīefore we look at the different options, let's contextualize the problem with a concrete example. In this post I'll go through three different ways to remove unwanted files from a series of commits using reset, rebase, and filter-branch. Whenever this happens, removing these unwanted files from each commit is necessary – but how can it be done? No one wants to bloat the repository by adding unintentional commit noise, ultimately causing confusion among your team. If you've ever accidentally committed unwanted files, such as logs, binaries, or libraries as part of a series of commits, you know what I'm talking about. 5 min read Photo by Tania Melnyczuk / UnsplashĪs a developer you're occasionally faced with the need to undo (or rather redo) a series of commits – just like the original poster of this Stack Overflow question (with over 10M views).
