Skip to content

Instantly share code, notes, and snippets.

@moisoto
Created April 27, 2023 17:21
Show Gist options
  • Save moisoto/02ad7feddc4c7c6e83891f2d5249cdd1 to your computer and use it in GitHub Desktop.
Save moisoto/02ad7feddc4c7c6e83891f2d5249cdd1 to your computer and use it in GitHub Desktop.

Undo Branch Changes

This article will show you how to revert changes in your current branch to a previous state.

Going to a previous commit

The most simple scenario is when you want to go back to a previous commit for all files.

A simple way to do this is use the checkout command.

Let's say this is your commit log:

$ git log --oneline
fc55954 (HEAD -> main) Fourth version.
7ab92e1 Third version.
9124e77 Second version.
6fa5293 First version.

And you want to go back to your second commit (9124e77):

$ git checkout 9124e77 .
Updated 1 path from c53a011

$ git status
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   TestFile.txt

$ git commit -m "Revert back to second commit (9124e77)."
[main 1c75f29] Revert back to second commit (9124e77).
 1 file changed, 1 insertion(+), 1 deletion(-)    

Notice when doing a checkout this way we include a dot (.) at the end of the command. This is to tell git to make a global checkout of the current directory and all the sub-directories inside it.

Of course you can restore specific files from the commit by specifying a folder name or filenames instead of a dot. Also, notice that the files restored from the specified commit will be automaticalled staged so you just need to do a commit and don't need to stage the files manually.

Remember: What you are doing is taking the files from an specific commit and replacing the current files with those. So you will not actually go back to the specified commit. This is important when working with shared remote repositories. When you commit the changes, you will generate a new commit with the new files and you can then push the change without altering the commit history for other people working on the repository.

Undo Last Commit

If changes have been already pushed, or you want to keep old commit (recommended):

git revert HEAD

If changes are still local and you want to leave no trace of previous commit:

# Be careful with this command on public repositories
# Not advisable to reset to a previous commit that have been pushed
# This command will reset the branch to the commit before the HEAD
git reset --hard HEAD~;

Reverting Changes Introduced on a given commit

Sometimes you want to undo changes on a more granular level. Suppose you want to only revert the changes introduced by a specific commit, for this you should use the git revert command.

This command will try to perform several tasks:

  • At the working directory: Undo the changes introduced by the specified commit.
  • On the staged snapshot: Stage the modified files currently on the working directory.
  • On the commit history: Commit the staged changes on a new commit.

If there are conflicts that can't be resolved on the modified files, the revert command will not stage the changes and will direct the user to resolve the conflicts the same way as when doing a merge operation. The user will have the three options at this point:

  • Resolve conflicts a run the git revert --continue command.
  • Skip the patch of the conflicting files with git revert --skip
  • Abort the revert process and return everything back to normal by using the git revert --abort command.

In order to see this in action, run the following script (or run the commands manually) and see what happens:

mkdir revert-test
cd revert-test
git init
echo "File for Initial Commit" > FirstFile.txt
echo "Another file for Initial Commit" > AnotherFile.txt
git add FirstFile.txt AnotherFile.txt
git commit -m "Initial commit"
echo "This file will be deteled by a revert to this commit" > WrongFile.txt
echo "This line will be deleted by a revert to this commit" >> FirstFile.txt
git add FirstFile.txt WrongFile.txt
git commit -m "Add WrongFile.txt and Mofidy FirstFile.txt"
echo "This line will remain untouched by a revert to the previous commit" >> AnotherFile.txt
git add AnotherFile.txt
git commit -m "One last commit"
echo "Check the files then run the command: git revert HEAD~"

More information:

This article focuses on the practical use of the Checkout, Revert and Reset commands. However you may want to know exactly how these git commands work.

The following article covers in more detail the workings of these commands: Resetting, Checking Out & Reverting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment