Git

How do I Properly Force a git push?

On Git, when changes are added to project source code files, the “$ git push” command is used to take these added changes to the local repository and update the Git remote repository. Developers use this command to update their Git remote repository to collaborate the Git history with project collaborators. However, they want to update their remote history in some situations. This is where the “$ git push –force” command comes in.

This guide provides the procedure to force a git push properly.

How do I Properly Force a git push?

To force a git push properly, firstly, navigate to the Git local repository, and clone the remote repository to the local repository. Fetch the updated remote repository data and run the “$ git push –force origin <branch-name>” command to push the local branch forcefully to the remote repository.

Now, let’s move ahead and implement the above-stated procedure!

Step 1: Navigate to Local Repository

First, navigate to the Git local repository by executing the “cd” command:

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

Step 2: Clone Remote Repository

To clone the Git remote repository to the Git local repository by specifying its URL:

$ git clone https://github.com/GitUser0422/demo5.git

Step 3: Fetch Remote Repository

Now, fetch the updated remote repository data:

$ git fetch

As you can see in the below-given output, the updated data of the remote repository is fetched successfully:

Step 4: Push Git Branch

To push the local repository branch to the remote repository branch, run the provided command:

$ git push --force origin master

Here, the “–force” option is used to push the branch forcefully to the repository. In our case, the local “master” branch is pushed successfully to the remote repository:

You can also verify the force push operation through the remote hosting service GitHub:

We have offered the easiest procedure to force a git push properly.

Conclusion

To force a git push properly, firstly, move to the Git repository, and clone the Git remote repository to the local repository by executing the “$ git clone <url>” command. Fetch the updated remote repository data and execute the “$ git push –force origin <branch-name>” command to push the local branch forcefully to the remote repository. This guide illustrated the method to force a git push properly.

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.