Git

Undoing a ‘git push’

Git is an open-source DevOps tracking tool that enables developers to implement their project source code in different versions. Reverting to the previous commit or previous state is another cool feature of Git because it allows programmers to switch back to an earlier state in the repository. After mistakenly pushing all added changes to the remote hosting service, developers can revert to the older state using the “$ git revert <sha-hash>” command.

This write-up will discuss:

How to Push Locally Added Changes to Remote Repositories?

To undo the “git push”, first, users need to push locally added changes to the remote repository. It’s a prerequisite operation. Upon doing so, users can undo a git push by following the given procedure:

  • Navigate to the Git local repository.
  • View the current Repository’s existing content.
  • Check the reference log history.
  • View the remote URL list.
  • Download the modified version of the remote repository.
  • Push locally added changes to the remote hosting service.

So, let’s begin!

Step 1: Access Git Local Repository

First, run the following command by specifying the desired Git local repository and move to it:

$ cd "C:\Users\nazma\Git\Test_4"

Step 2: Display Content List

To show the current repository list of content, run the “ls” command:

$ ls

Step 3: Check Git Repository Reference Log History

Now, execute the “git log .” command to check the Git repository reference log history:

$ git log.

Step 4: Verify Remote URL

To ensure that the remote URL is added, utilize the “git remote -v” command:

$ git remote -v

Step 5: Fetch Remote Origin

Now, download the updated version of the remote repository on the local repository, and use the provided command along with the remote name:

$ git fetch origin

Step 6: Push Local Repository Changes to Remote

Next, execute the “git push” command with the remote and local branch name in which changes are added:

$ git push origin master

Note: The above-stated procedure is required to undo the “git push” operation.

How to Undo a “git push”?

To undo a “git push” operation, all added changes that move to the remote repository revert to a local repository. To do so, follow the below-listed steps.

Step 1: Undo “git push”

To revert the pushed changes, execute the “git revert” command along with the most recent commit SHA hash:

$ git revert b87cd01

After executing the above-listed command, a text file will be opened with a default text editor, where you can add a comment, save, and close the text file:

As a result, you will get the below-provided output which indicates that the added changes are unpushed successfully:

Step 2: Verify Unpush Operation Through Repository Reference Log

Lastly, run the “git log .” command to ensure that all added changes are reverted to the local repository:

$ git log .

As you can see, the unpushed operation is performed successfully:

Step 3: Check List of Content

Execute the “ls” command and check the repository content:

$ ls

That’s all! We have performed the straightforward steps to undo a “git push” operation.

Conclusion

To undo the “git push” operation, first, go to the Git working repository and check the content list. Then, view the repository reference log history and existing remote URL. Next, fetch the modified version of the remote repository and push all locally added changes to the remote repository. Finally, execute the “$ git revert <sha-hash>” command. This write-up demonstrated the method to undo the “git push”.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.