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:
Step 2: Display Content List
To show the current repository list of content, run the “ls” command:
Step 3: Check Git Repository Reference Log History
Now, execute the “git log .” command to check the Git repository reference log history:
Step 4: Verify Remote URL
To ensure that the remote URL is added, utilize the “git remote -v” command:
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:
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:
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:
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:
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:
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”.