Git

Force “git push” to Overwrite Remote Files

When developers make changes in the local repository, they need to publish the added local changes to the remote repository for updating the project folder. The “$ git push” command is most commonly used for it. Basically, it is opposed to the “$ git fetch” command utilized to download the updated version of the remote repository. Users can forcefully overwrite the existing remote files with the updated local repository files.

This write-up will briefly explain the procedure to overwrite the remote files forcefully.

How to Force “git push” to Overwrite Remote Files?

To overwrite the remote files by pushing local changes forcefully, follow the steps stated below:

  • Move to the Git local repository.
  • Generate and stage a new file.
  • Update the current repository with the added changes.
  • Check the remote URL list to verify that the local/remote repositories are connected.
  • Execute the “$ git push -f <remote-name> <branch-name>” command.

Here are the above-provided steps for implementation!

Step 1: Navigate to Desired Working Directory

First up, open up a particular Git working directory by providing its path in the given command:

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

Step 2: Make New File

Run the “touch” command to create a new file in the working directory:

$ touch file.txt

Step 3: Push Changes to Tracking Area

Then, push the created file to the tracking area by running the “git add” command:

$ git add file.tx

Step 4: Save Changes Into Git Repository

Now, perform the Git commit to save the added changes to the Git repository:

$ git commit -m "First file added"

Step 5: View Remote URL

Next, run the “git remote” command with the “-v” option to check the list of existing remote URLs:

$ git remote -v

Step 6: Overwrite Remote Files

Lastly, overwrite the remote files by running the “git push” command with the “-f” flag for pushing forcefully, remote and branch name:

$ git push -f origin master

That’s all! We have demonstrated the “git push” method to overwrite the remote files forcefully.

Conclusion

To overwrite the remote files by pushing local changes forcefully, first, go to the Git local repository, generate and stage a new file. Then, update the current repository with the added changes and check the remote URL list to verify that the local/remote repositories are connected. Lastly, run the “$ git push -f <remote-name> <branch-name>” command. This write-up provided the procedure to overwrite the remote files forcefully.

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.