
I hope this blog will make your developer life a bit easier.
Git rollback last commit update#
Example: git status On branch b Changes not staged for commit: (use 'git addYour changes will stay locally so you are safe. That was easy wasn’t it? Thanks to git and it’s great and flexible design. Git To rollback the last commit: git reset HEAD It will revert your last commit. Now we are done and you can safely push the reverted changes contained in commit D. Git utilizes local clones of central repositories to bring more effective collaboration. Git is a widely used Version Control System (VCS) known for its versatility. Now switch back to master in forced mode and revert the isolated changes. Revert the Last Commit in Git Updated Thursday, March 9, 2023, by Nathaniel Stickman Create a Linode account to try this guide. In this example the commit B‘s id is 823bd88 as you can see in the commit message above (point 3). Revert the isolated changes contained in commit B’ on top of Cįirst make a copy the commit id of B’, we need it soon.Changes to file2 and file4Ģ file changed, 5 insertions(+), 2 deletion(-) $ git commit -m 'Changes to file2 and file4' $ git add someDir/file2 someDir/someOtherDir/file4 No changes added to commit (use "git add" and/or "git commit -a")Ĭreate a commit that only contains the changes you want to revert on top of commit C. " to discard changes in working directory) Since the working directory contains the state of commit B and the index is resetted to A a git status will show you the changes that commit C contains but are not committed yet. That gives us the opportunity to create a new commit that only contains the changes we want to revert on top of commit C. Will show you all changes introduced in commit B as unstaged changes. Now the index’s state is the state of the previous commit, but the working directory still contains the files of commit B. Checkout the commit that contains the changes you want to revert in detached HEAD state.Since the commits B and C are already pushed we should not rewrite them, but we don’t need to rewrite them as I will show you now.

Git rollback last commit how to#
So the question is how to isolate changes into an own commit or split a commit in multiple commits? Now if you want to revert the changes to file2 and file4 of commit B but not the other changes you can’t simply do a git revert B. But if you can isolate the changes into a single commit you can use git revert. Commit C is based on commit B and is already pushed to the remote repository, because origin/master and master refer to the same commit. Commit B contains changes to the files file1, file2, file3, file4. In the next sections I will show you how to do it.Īs you can see the git repository contains 3 commits with the ids A, B, C. So what should you do if you only want to revert a part of a big commit in a safe manner? Fortunately git provides tools you need to do this. In other words… if commits are fine-grained it is very likely that the changes you want to revert are only contained in one commit. This works well if commits have a high cohesion in matters of the tasks they implement. So all changes contained in this commit will be reverted.

This git command reverts the whole commit. Normally you revert a commit using git revert.
