Sometimes you make a mistake and want to go back to a previous version. Here's how to rollback changes.

Undo Unstaged Changes

You made changes to a file, saved the file and not yet added the file to the staging area. Then you decide that you don't want to keep these changes.

Here's a quick, real-world example of how to undo the unstaged changes of a file:

# make a repo directory 
mkdir projectrepo && cd projectrepo

# initialize the repo and specify the initial branch name
git init --initial-branch=main

# add a file
touch page1.html && echo "<h1>Header for page 1</h1>" > page1.html

# add the new file to the Staging area
git add page1.html

# create a Commit
git commit -m "create page1"

# make some changes to the page1.html
echo "<p>content for page 1</p>" >> page1.html

# the content of page1.html after changes
cat page1.html

# undo the changes
git checkout page1.html
# or
git restore page1.html

# the content of page1.html after undo changes
cat page1.html
Undo Unstaged Changes

Undo Staged Changes

You made changes to a file, saved the file and added the file to the staging area. Then you decide that you don't want to keep these changes.

Here's a quick, real-world example of how to undo the unstaged changes of a file:

# make a repo directory 
mkdir projectrepo && cd projectrepo

# initialize the repo and specify the initial branch name
git init --initial-branch=main

# add a file
touch page1.html && echo "<h1>Header for page 1</h1>" > page1.html

# add the new file to the Staging area
git add page1.html

# create a Commit
git commit -m "create page1"

# make some changes to the page1.html
echo "<p>content for page 1</p>" >> page1.html

# the content of page1.html after changes
cat page1.html

# add the modified file to the Staging area
git add page1.html

# remove the modified file from the staging area
git reset page1.html
# or
git restore --staged page1.html

# undo the changes
git restore page1.html

# the content of page1.html after undo changes
cat page1.html
Undo staged Changes

Remove Commits and Keep the Changes and Keep the Untracked Files

You made commits. Then you decide that you want to remove several previous commits.

Here's a quick, real-world example of how to undo the unstaged changes of a file:

# make a repo directory 
mkdir projectrepo && cd projectrepo

# initialize the repo and specify the initial branch name
git init --initial-branch=main

# add first file
touch page1.html && echo "<h1>Header for page 1</h1>" > page1.html

# add the first file to the Staging area
git add page1.html

# create first Commit
git commit -m "create page1"

# add second file
touch page2.html && echo "<h1>Header for page 2</h1>" > page2.html

# add the second file to the Staging area
git add page2.html

# create second Commit
git commit -m "create page2"

# add third file
touch page3.html && echo "<h1>Header for page 3</h1>" > page3.html

# add the third file to the Staging area
git add page2.html

# create third Commit
git commit -m "create page3"

# make some changes to the page1.html
echo "<p>content for page 1</p>" >> page1.html

# the content of page1.html after changes
cat page1.html

# add the modified file to the Staging area
git add page1.html

# create forth Commit
git commit -m "content page1"

# list all files
ls

# view the commit history
git log

# remove the last three commits
git reset e1a3b1434e3a146d36bb4ba3f587ba752c5cf48b
# Or
git reset HEAD~3

# list all files and page2.html and page2.html still exist
ls

# the changes of page1.html still exist
cat page1.html

# view the commit history and the three commits are removed
git log
Remove Commits
Remove Commits
Remove Commits

Remove Commits and Undo Changes And Remove the Untracked Files Permanently

You made commits. Then you decide that you want to remove several previous commits and undo the changes and remove the untracked files permanently.

Here's a quick, real-world example of how to undo the unstaged changes of a file:

# make a repo directory 
mkdir projectrepo && cd projectrepo

# initialize the repo and specify the initial branch name
git init --initial-branch=main

# add first file
touch page1.html && echo "<h1>Header for page 1</h1>" > page1.html

# add the first file to the Staging area
git add page1.html

# create first Commit
git commit -m "create page1"

# add second file
touch page2.html && echo "<h1>Header for page 2</h1>" > page2.html

# add the second file to the Staging area
git add page2.html

# create second Commit
git commit -m "create page2"

# add third file
touch page3.html && echo "<h1>Header for page 3</h1>" > page3.html

# add the third file to the Staging area
git add page2.html

# create third Commit
git commit -m "create page3"

# make some changes to the page1.html
echo "<p>content for page 1</p>" >> page1.html

# the content of page1.html after changes
cat page1.html

# add the modified file to the Staging area
git add page1.html

# create forth Commit
git commit -m "content page1"

# list all files
ls

# view the commit history 
git log

# remove the last three commits and Undo the changes
git reset 31207f129056d79913cae19606b273606ab072b8 && git restore .
# Or
git reset HEAD~3 && git restore .
# Or
git reset --hard

# remove untracked files permanently
git clean -df

# list all files
ls

# view content of page1.html
cat page1.html

# view the commit history and the three commits are removed
git log
Remove Commits
Remove Commits
Remove Commits

Undo a Commit (Remove Created Files and Changes in the Commit) Without Remove the Commit History

You made a commit. Then you decide that you want to undo a commit.

Here's a quick, real-world example of how to undo the unstaged changes of a file:

# make a repo directory 
mkdir projectrepo && cd projectrepo

# initialize the repo and specify the initial branch name
git init --initial-branch=main

# add first file
touch page1.html && echo "<h1>Header for page 1</h1>" > page1.html

# add the first file to the Staging area
git add page1.html

# create first Commit
git commit -m "create page1"

# add second file
touch page2.html && echo "<h1>Header for page 2</h1>" > page2.html

# add the second file to the Staging area
git add page2.html

# create second Commit
git commit -m "create page2"

# add third file
touch page3.html && echo "<h1>Header for page 3</h1>" > page3.html

# add the third file to the Staging area
git add page2.html

# create third Commit
git commit -m "create page3"

# make some changes to the page1.html
echo "<p>content for page 1</p>" >> page1.html

# add the modified file to the Staging area
git add page1.html

# create forth Commit
git commit -m "content page1"

# view the commit history 
git log

# undo the third commit and ctrl + X
git revert 1bfe6eb289196beda543c44a4cfdf30eec331475

# view the commit history
git log

# list all files and file page2.html is removed
ls
Undo Commits
Undo Commits
Undo Commits

To recover the removed files and changes in the third commit is by reverting back

# undo the revert
git revert 8d4c1ddf0cb02fcbdbd2820af5ee74594289384f

# list all files and file page2.html is back
ls
Undo Commits