
We can amend the last commit by running the git commit -amend command.
GIT STASH DELETE ALL SERIES
What if after committing a series of changes, you make some changes that really should have been a part of the last commit? There's a way to undo-or more accurately, amend-the most recent commit. We then passed this hash to the git reset -hard command to restore the repo to that commit. Then note down the ID (SHA-1 hash) of the commit we want to go back to.Īs you can see in the image above, the commit hash when we had 3 pages was 6b6271a. Run git reflog to see a history of all commits made. We want to restore our repo to when we had just three pages.
GIT STASH DELETE ALL HOW TO
Let's create a new page and commit it to see how to do this.Įcho "some new content for the page 4" > page4.txt git add page4.txt git commit -m "create page 4" Fret not! You can easily go back to a previous commit from where you're sure there's no error. Now, let's assume you didn't realize you made an error before you committed. When I'm absolutely sure I won't ever need those changes, I use one of the other options. Which option is best? I mostly use git stash because I can reapply the discarded changes later. Git reset -hard : This also discards all changes permanently. Git checkout - : This works similarly to git stash, except that it discards changes to a file permanently.

However, Git will temporarily save them, in case you want to retrieve them later. Git stash : The git stash command will discard all your untracked files, as well as staged and unstaged modifications. Now you want to restore the repo to how it was before you made the change. Luckily, you realized the problem before making the commit. Let's assume you made the above change in error. Let's make some changes to the third file we created earlier (page3.txt).Įcho "making some changes to page3" > page3.txtĪfter running the above command, run git status. Whether you've staged these changes or not, what matters is that you haven't committed them. The first approach we're going to look at in undoing changes is how to undo changes you've made but not yet committed. You can use these SHA-1 IDs to travel to any commit whenever you want. Each commit is identified by a SHA-1 hash (before the commit message). Git reflog shows a list of all commits that have been made in this repo, as well as some other key events, like when switching between branches ( checkout). When you run the git reflog command, you should have something like this: To read more about their differences, check out this StackOverflow answer. Either of the two will serve us just fine, however, for the scope of this tutorial.

While these two commands serve a similar purpose (viewing history), there are subtle differences in how they work and the result they show. There are two major ways to view the git history of a repo: git reflog and git log. And that's where the Git history comes in handy. We also need a list of possible places and times we can travel to. To be able to travel back and forth in time, we need a way to know where we are. Touch page3.txt & echo "Content for page3" > page3.txt git add page3.txt git commit -m "create page3" Checking Git History Touch page2.txt & echo "Content for page2" > page2.txt git add page2.txt git commit -m "create page2" Let's add a few more files with separate commits so we can have more commits to work with. Now, we have a repo set up with one file added and one commit in the git history. Mkdir git-undo & cd git-undo git init touch page1.txt & echo "Let us put something here" > page1.txt git add page.txt git commit -m "create page1" Setting Things Upįrom my terminal, I'll run the following commands to create the repo folder, add some files and create some commits to work with:

We'll be doing everything from the terminal, so get ready to do some typing. I advise you do the same so you can easily follow along. To make this post practical and relatable, I'll be creating a new git folder to use as a demo. We'll be taking advantage of this timeline to move back and forth as we please. As you know, Git stores snapshots of a repo at different points and then creates a timeline history. So, next time you think you've made a commit in error, you'll have no reason at all to panic. In this post, we'll explore ways to undo changes in Git. For example, maybe you're working on a new feature that isn't ready yet, and then you need to rollback to a previous commit. There are many other valid reasons you'd want to travel in time between different versions of a project. Lucky us! Undoing things in Git isn't just for mistakes. While life might not always present us with a chance to undo our mistakes, Git provides us ample opportunities to do just that. In life, undoing our mistakes is something we've always wished was possible. Time Travel in Your Project: Undo Changes with Git
