Table of Contents
Knowing how to do git undo commit before push stops you from documenting changes you no longer need. The most straightforward way to do git undo commit before push is running a
--hard reset in your commit hashes.
You can do that by running the command:
git reset --hard <commit hash>
What if you want to do more than clear the commit history? That is where
git revert, and
git restore come in. Read on to find more simple commands to handle git undo commit before push and related challenges.
Different methods to undo commit before push in git
1. Using git reset command
git reset is a way to undo mistakes in the repo. You can use it to undo commits in the local repo before pushing the changes to a remote repo.
## mixed reset git reset <commit hash> ## soft reset git reset --soft <commit hash> ## hard reset git reset --hard <commit hash>
2. Using git checkout command
git checkout entails switching between target commit, branch, or file versions. Apart from viewing old commits, it helps undo commits before pushing the changes to a remote repo.
## checkout using commit hash git checkout <commit hash> ## checkout using branch git checkout -b <new-branch> <commit hash>
3. Using git revert command
git revert command is a safe way to undo operations. Unlike
git reset --hard, It does not delete commits from the history but creates a new commit by inversing the specified changes.
git revert <commit hash>
4. Using git restore command
git restore helps to remove uncommitted local changes. It discards the file from the staging area without touching its actual modification.
git restore --source = <commit hash> <file>
Setup Lab Environment
Before walking you through the ways to undo commit before push, you should access a terminal.
We will then build a remote repo from scratch. I am creating a Github repo called git-undo.
Let's grab the URL to the remote repo.
Head over to the command line and clone the repo by running the commands:
git clone https://github.com/Stevealila/git-undo.git
cd into the folder and create a text file called
git status on the command line. We have an untracked file.
Let's add the changes to the staging area using
git add file1.txt
Next, we should commit the staged file. We can achieve that by running:
git commit -m "add file1.txt"
git status reveals, "Your branch is ahead of 'origin/main' by 1 commit. and the working tree is clean."
We can synchronize the two repos by running
We can see the commit history by running either of these commands:
## List full history details git log ## full commit hashes, summarized history git log --pretty=oneline ## shortened commit hash and history git log --oneline
Our history has two commits. Now is the time to test git undo before push using the four typical ways.
Example-1: How to undo commit before push using git checkout command
Let us add some words into
echo "adding line 1" > file1.txt
git status shows we have an untracked file.
git add . and
git commit -m "put something in file1".
Let us recheck the commit hashes.
git log --oneline
Assume we want to undo the commits before pushing the changes to the remote repo. We can achieve that in two ways:
Let us undo the commit hash
git checkout 5f5a3d1
git checkout <commit hash> matches the working repo's state to the commit we have done git checkout on.
We can also
git checkout a branch to enable git undo commit before push.
git checkout -b additions 5f5a3d1
Example-2: How to undo commit before push using git reset command
git reset enables us to undo commit before push in three ways:
git reset --soft,
git reset --mixed, and
git reset --hard.
Let us format the history's appearance to have a better look at the commit head.
git log --graph --decorate
git reset --soft HEAD~1stacks the changes in the staging area.
git reset --mixed HEAD~1is the default reset command. It keeps the changes in either staging or working directory.
git reset --hard HEAD~1removes all the changes from the staging areas.
git reset --hard <commit hash> takes all changes to the state they were in at the specified commit hash, leaving untracked files untouched.
We can undo the last commit to our test project as follows:
git reset --hard c2eb392
If we redo
git log --graph --decorate, we realize that one commit got discarded. The head is now in the commit hash we specified when undoing the changes.
git status shows that untracked files were not removed. To get rid of the untracked directories and files, run
git clean -df
git reset --hard <commit hash> is one of the most dangerous ways to perform git undo commit before push because you may not recover the commit history after performing the command.
You are probably wondering, "What if I want to recover commits after discarding them using
git reset --hard?" It is simple.
If not much time has gone after running
git reset --hard command, you can follow these steps to recover the commits:
git reflog to see if the git garbage has not collected changes. My log appears as follows:
git checkout the commit hash whose deletions you want to recover.
git checkout <commit hash>
Make a branch for the changes in the detached head.
And voila, you have your commits back!
Example-3: How to undo commit before push using git revert command
git revert <commit hash> creates new commits to inverse the effects of earlier commits without deleting the existing commits.
Let us check our commit history by running
git log --oneline
We can now run
git revert c2eb392 undo commit before push. It prompts us to enter a commit message by opening a code editor. After closing the editor, let us run
git diff c2eb392 to inspect the changes made.
Let us repeat the process for the hash
4438e09. If we
git log --oneline, we see the number of commits has changed. This time around, we have new commit hashes, as indicated in the screenshot below:
Example-4: How to undo commit before push using git restore command
git restore can help you undo changes to a commit at the file level. Here is how to use it.
git log --oneline shows we have four commits with hashes,
4438e09. Assume we want to move back to when we did the commit
c2eb392. We can do that by typing:
git restore --source=HEAD~2 file1.txt
git status reveals that file1.txt is now untracked.
You have just learned key commands to simplify git undo commit before push. The primary way to do git undo commit before is running
Other crucial commands to ease the process are
git revert, and
git restore. It is your turn to apply what you have learned in this article.